Linux Format

Automated email

-

Before I retired I worked in a Microsoft environmen­t. Using VBA, I could control Outlook to send emails as and when required at the touch of a button. I now wish to replicate that under Ubuntu 16.04—can this be achieved using Thunderbir­d or some other email service and maybe Gambas or C# on Mono? Mike Higgins Sending emails from the command line with Linux is easy and doesn’t need a specific language or mailer. The easiest way is with the mail program, a command line mailer. All you need to do is create the email then feed it to mail e.g. $ mail -s "A test mail" me@example.com <mail.txt where the file mail.txt contains the body of your mail. You can also send content directly without creating an intermedia­te file $ echo "This is a test" | mail -s "A test mail" me@example.com

Whatever mail receives on standard input is used as the mail body, you can also add headers with the -a option and extra cc and bcc recipients with the -c and -b options respective­ly, e.g.: $ cat mailbody | mail -a "X-Mailer: Hacky Script 0.1" -b me@home -s "test" me@work

Any arguments that contain spaces must be quoted to avoid confusing the shell. There are various other options documented in the man page but these are the main ones. Mail needs a local email server running, which is traditiona­l in the *nix world. The server receives the emails and sends them on to their destinatio­n. If you don’t have one installed, you can use a simply forwarding mail daemon,such as nullmailer. This simply listens for local emails and forwards them on to your ISP’s mail server. It should be in most distros’ software repositori­es for installati­on in the usual way. You then tell it where to send the mail by editing, as root, the file /etc/nullmailer/ remotes. This can be as simple as one line: mail.example.com smtp

This tells nullmailer to send all mail through mail.example.com using the SMTP protocol (the standard way of sending mail). Most mail servers require authentica­tion, so a more useful example, for sending through a Gmail account, is: smtp.gmail.com smtp --user=you@gmail.com

--pass=yourpasswo­rd --port=465 --auth-login --ssl As the remotes file contains passwords, it shouldn’t be readable by normal users. Editing as root should not change this but to be on the safe side run: $ chmod 600 /etc/nullmailer/remotes You can also edit the other files in /etc/

nullmailer but this is not essential. The man page has all the details.

This is the easy way to send mail from a terminal or script. If you want to do it from a ‘proper’ programmin­g language, most of them have modules or libraries to handle sending mail, for example Python has smtplib, which does the job with ease.

 ??  ?? You don’t need a GUI mailer to send emails with Linux.
You don’t need a GUI mailer to send emails with Linux.

Newspapers in English

Newspapers from Australia