Home Download Ubuntu

Sending emails from Ubuntu without having a mail server

Let's assume that you want to send emails from your server, but you don't have installed an email server. You can use an external server, like Google mail, which is free. Any other smtp mail server is good. The application which does this magical trick is called ssmtp

Let's install our program:

sudo apt-get install ssmtp


Then we need to edit it's configuration file:

sudo nano /etc/ssmtp/ssmtp.conf


1. The configuration to use Gmail's smtp server is:

hostname=ubuntu
AuthUser=email@gmail.com
AuthPass=password
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES


Explanation:

AuthUser= is the username which authenticates
AuthPass= is the password for the user above
FromLineOverride= is needed to allow the user to specify their own From: address (Gmail does not allow the user to specify their own From: address)
mailhub= is the smtp server through which we are going to send the emails. the port is optionally needed.
UseSTARTTLS= is to make a secure connection. Gmail needs it.
Others settings:
rewriteDomain= where will the mail seem to come from
hostname= the full hostname of the machine

2. To send email by function mail() in php using this external server

Usually the setings above may already work for the function mail() in php, but if it doesn't you need to set sendmail_path in php.ini:

The php.ini file usually is located in php's folder

sudo nano /etc/php5/apache2/php.ini


Press CTRL+W and enter sendmail_path. This is the find function in nano editor.

Delete the line where is sendmail_path (CTRL+K) and paste the following:

sendmail_path = /usr/sbin/ssmtp


Observation:

a. To verify if the server is sending emails, send a test email:

ssmtp recipient_email@example.com


The prompt will expect to enter something.

To: recipient_email@example.com
From: myemailaddress@gmail.com
Subject: test email

hello world!


b. You can check if the email are sent by running the following in a second terminal (ALT+F2):

tail -f /var/log/mail.log


This will "catch" live the emails that are sent. Another method to see the emails that were already been sent:

sudo nano /var/log/mail.log


Resources: tombuntu.com, tuxnetworks.blogspot.com,

3 comments:

kurtdriver said...

Hi, how would you do this in a shell script? The part that I find difficult is "The prompt will expect to enter something.". How would you go about that in a script? Thanks, Kurt

Speedy Gonzales said...

You could actually put the To, From, Subject and Message in a file, named whatever you want. Then run it like this:
ssmtp myemailaddress@gmail.com < to_from_subject_msg_file.txt

and your mail will be sent.

Noël Nachtegael said...

bonjour,

google a bloqué l'utilisation de mon compte pour des raisons de sécurité.

ssmtp: Authorization failed (534 5.7.14 https://support.google.com/mail/answer/78754 jd7sm761198wjb.19 - gsmtp)

J'ai signalé que j'étais à l'origine de la connexion et j'ai également autorisé les application moins sécurisées.

Pensez-vous que google va tenir compte de ma réponse?
Faut-il faire une autre démarche?
Combien de temps cela va-t-il prendre (ggogle va signaler l'appareil comme inconnu pendant 2 semaines)?

Post a Comment