Enviar mail desde la línea de comandos del shell

En esta web nos enseñan cómo hacer una instalación para enviar mails desde la línea de comandos de Linux. Es importante porque también tendremos que seguir esta instalación si lo que queremos es enviar mails desde una aplicación PHP instalada en nuestra Raspberry.

Eso sí, necesitaremos conectar a un SMTP, como por ejemplo el de Gmail, y disponer por lo tanto de un usuario y un password.

http://iqjar.com/jar/sending-emails-from-the-raspberry-pi/

 

Installing and configuring SSMTP

1. Make sure your repositories are up-to-date:

apt-get update

2. Install SSMTP and mail utilitites:

apt-get install ssmtp

apt-get install mailutils

3. Edit the SSMTP configuration file:

nano /etc/ssmtp/ssmtp.conf

a) Mandatory lines:

root=postmaster
mailhub=smtp.gmail.com:587
hostname=raspberrypi
AuthUser=YourGMailUserName@gmail.com
AuthPass=YourGMailPassword
UseSTARTTLS=YES

Be sure to specify the correct GMail user name and password here, otherwise you will get authentication errors.

If the host name of your Raspberry Pi is different from “raspberrypi”, specify your actual host name here instead.

b) Optional lines:

rewriteDomain=your.domain

Specify this if you would like the outgoing emails to appear to be sent from your.domain (instead of from gmail.com).

FromLineOverride=YES

Specify this if you would like SSMTP to leave the From field of the emails untouched. Otherwise it will overwrite the from field with the name of the Linux user which sends the email. The overwriting name is taken from the 5th value in the line that corresponds to the sending user, from the /etc/passwd file. If you plan to send emails from a website (for example from a WordPress plugin) and wish to have nice sender names like “John Doe”, I recommend commenting this line (which is equal to setting the value to NO), otherwise your website will only be able to send emails with less nice sender names, like johndoe@your.domain. In other words, you probably want SSMTP to overwrite the sender field with a nice name taken from the /etc/passwd file.

4. Edit the SSMTP aliases file:

nano /etc/ssmtp/revaliases

This file contains data about the email accounts for existing Linux users in the format local_account:outgoing_address:mailhub[:port]

You should create one line for all the users in your system from which you plan to be able to send emails. For example:

root:root@your.domain:smtp.gmail.com:587
www-data:yourwebpagesname@your.domain:smtp.gmail.com:587

In case you wish to send out emails from a WordPress plugin, you must make sure that you have a line for the user www-data, which is the user under which WordPress runs.

5. Set the permissions of the SSMTP configuration file:

chmod 774 /etc/ssmtp/ssmtp.conf

The  permissions of the file /etc/ssmtp/ssmtp.conf determine  who will be able to send emails from the Raspberry Pi. By default this file is owned by the user root and the group of the file is also root. So if you want other users, like www-data to be able to send emails (which you definitely want if you’re using a WordPress plugin for example to send out emails), then you need to give read rights to the users who are not the owner of the file and not in the group of the file. The above permissions (774) mean that the owner (root) will be able to read/write/execute the file (7), the other users in the root group will be able to do the same (7) and the users which are not in the group will only have the right to read the file (4). For more details type chmod –help.

If you prefer not to allow every user on your system to send emails, then add the www-data user (or the user who you would like to grant permission for sending emails) to the root group and only give the rights to the users in this group:

sudo usermod -a -G root www-data
chmod 770 /etc/ssmtp/ssmtp.conf

Be careful though. Adding the www-data user to the root group might sometimes not be very safe, as it will allow your website to do many things on your system.

6. Nice sender names:

If you would like your website (WordPress for example) to be able to send emails which appear to be sent from a person with a nice name like “John Doe” or “Your super web site” instead of from a simple  sender name like you@your.domain, then you need to make sure that in the /etc/ssmtp/ssmtp.conf file the FromLineOverride line is commented (#) or set to NO and you need to give a nice name to the www-data user. You can do this by editing the passwords file:
nano /etc/passwd

Find the line corresponding to www-data and set the fifth value in it to a nice name like “Your super website”.

Sending emails from command line

Once you’re done with the above setup and configuration process, you can send emails very easily from command line, which is great because you can also put the sending into Bash scripts, which can be called by other applications running on your Pi. A simple example looks like this:

echo “Test text” | mail -s “Test Mail” targetperson@example.com

The text “Test text” is sent to the email address targetperson@example.com (you may also specify multiple addresses separated by spaces) with the subject “Test Mail”. For more options type mail –help.

Sending emails from PHP scripts

If you would also like to be able to send out emails from PHP scrips (which is the case if you plan to send emails from your website, perhaps from a WordPress plugin like Subscribe2), then you need to configure PHP to find the mail sending application.

1. Edit your PHP configuration file:

nano etc/php5/apache2/php.ini

Find (F6) the line which contains sendmail_path and set it to the appropriate value:

sendmail_path = /usr/sbin/sendmail -t -i

2. To test if PHP is indeed able to send out emails, create a file named mailtest.php and put the following code into it:

<?php
$to = “targetperson@example.com”;
$subject = “PHP Test mail”;
$message = “This is a test email”;
$from = “you@your.domain”;
$headers = “From:” . $from;
mail($to,$subject,$message,$headers);
echo “Mail Sent.”;
?>

Test by calling your script from command line:

PHP mailtest.php

Anuncios

Acerca de frandelacal

Ingeniero Técnico por la UPC y profesor de Tecnología por la Salle. Experiencia profesional de un año como autónomo, más 7 años trabajando en el Grupo IBM, en el Grupo HP, en Caixa Penedès y en Generali Seguros.
Esta entrada fue publicada en Sin categoría. Guarda el enlace permanente.