71

I installed Ubuntu 11.04, LAMP using tasksel.

I find that PHP mail() does not work.

I suppose I need to enable it or something? How do I do that?

51

From the mail manual of php:

Requirements

For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.

This means you have to install some sort of mailserver and configure php to use it. Usually this mailserver is postfix in Ubuntu. However - if the php side you are coding will eventually be stored on a hosting service's side (e.g. xmission), a mail server will most likely already be installed there. In that case just test your site online instead of locally.

If you need to test it on your own system or mean to host it on your own home-server than proceed with:

Postfix Installation

Installation: postfix Install postfix / sudo apt-get install postfix During the process you will be asked in which mode you want postfix installed. There are four possible modes:

  • Internet: Your own mail-server.
  • Satellite: An extern mail provider (e.g. Google) will be used for sending and receiving mail. The server will not receive any mail.
  • Smarthost: Mixture between the two. Mail is stored locally but sent through an external mail provider.
  • Local only: Will not concern you. That's a system intern mailserver. You can only send mail from user to user on the system.

The rest of the install options depend on your choice of this general configuration.

Most likely you will choose a satellite install. That means mail will be sent using an extern provider. As smtp-server (outgoing mail server) you will then have to specify your providers smtp. The rest of the options is self explanatory.

Post Installation Configuration

Most smtp-servers require a password authentication to send mail. So postfix will need to know that password. Also there are things like encryption to consider (which you'll have to google). This is how you configure postfix using password authentication (sasl):

  • Install libsasl2-modules Install libsasl2-modules and sasl2-bin Install sasl2-bin by clicking the Software Center icons or from terminal using:

    sudo apt-get install libsasl2-2 libsasl2-modules sasl2-bin
    
  • Enable sasl-auth by adding these lines to /etc/postfix/main.cf

     # add to /etc/postfix/main.cf
     smtp_sasl_auth_enable = yes
     smtp_sasl_security_options = noplaintext noanonymous
     smtp_sasl_password_maps = hash:/etc/postfix/sasl_password
    
  • Create a file /etc/postfix/sasl_password with a line like:

     smtp.gmail.com USERNAME@gmail.com:USERPASSWORD
    

    Substitute the actual password, username and smtp-address.

  • Update postfix:

     sudo chmod 600 /etc/postfix/sasl_password # for safety of your smtp password
     sudo postmap hash:/etc/postfix/sasl_password 
     sudo postmap /etc/postfix/sender_canonical
     sudo /etc/init.d/postfix restart   
    

    You might have to circumvent the 'permission denied' bug by chown postfix:postfix /etc/postfix beforehand.

This should do it in most of the cases. Yet some smtp providers require a specific address as the sender or encryption.

Related: PEAR::Mail interface might also be of interest to you.

Gmail (and perhaps other services) may not like it that you are attempting to send mail this way as it could be deemed insecure by their standards, and would block your attempt i.e. nothing will happen on the screen or someplace of the whole process would block your authentication. Also your POP3 must be enabled.

To counter that see here. (If you want to be on the safe side, then create a dummy Gmail account)

  • Sending mail from Ubuntu (local) works. But what if I want my Windows to use this machine to send mail (PHP also)? How do I configure that? – JM at Work Jun 14 '11 at 3:15
  • Not sure I get the question. You can sand from your Ubuntu machine. Now what you want to do is send mal via that Ubuntu machine from your Windows PC via php? Is that correct? – con-f-use May 8 '12 at 13:04
  • 2
    Now the package libsasl2 doesn't exist. I am using 11.04. Its libsasl2-2 as per updated package.. – mythicalcoder Jul 23 '12 at 13:46
60

As already noted, postfix would be an overkill if you just need to send emails from php while developing applications on your home computer.

I personally use very simple and lightweight package ssmtp. It will use any smtp server to send outbound emails. Install it by running:

sudo apt-get install ssmtp

Then edit /etc/ssmtp/ssmtp.conf file, comment out existing mailhub line and add the following lines (this example is for gmail smtp server):

mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
AuthUser=<YOUR-EMAIL>@gmail.com
AuthPass=<YOUR-PASSWORD>

(Provide your gmail username & password. Of course you can use any other SMTP server).

Now make sure that your php.ini has correct sendmail_path. It should read as:

sendmail_path = /usr/sbin/sendmail -t

(From comments of @dima-l and @omar-sabic: You do not have to specify ssmtp here because the installation should have created a symlink for /usr/sbin/sendmail, which correctly points to ssmtp. Otherwise you have to specify sendmail_path = /usr/sbin/ssmtp -t)

Reload apache and your php should be able to send outgoing emails now.

  • 3
    I think you meant sendmail_path = /usr/sbin/ssmtp -t. This worked, thanks – Omer Sabic Dec 18 '13 at 19:02
  • 1
    ssmtp should create symlink for /usr/sbin/sendmail, which correctly points to ssmtp. So, using sendmail_path = /usr/sbin/sendmail -t should work as well – Dima L. Dec 29 '13 at 13:14
  • 2
    More about configuring ssmtp (found some useful notes) : How to Set Up PHP mail in Debian - this question + this link helped to set up the program. – gskema May 19 '15 at 7:22
  • 1
    For gmail consider you have to configure your google account to allow "unsafe apps" for it to work. See support.google.com/accounts/answer/6010255 – aesede Apr 14 '16 at 18:24
  • 1
    I also had to add the following line : FromLineOverride=YES in order to avoid the following error: 550 5.7.60 SMTP; Client does not have permissions to send as this sender – Caumons Aug 17 '16 at 17:20
29

PHP requires an SMTP client to send mail. You could install a full-featured mail transfer agent (MTA) like Postfix to serve this function, but it's overkill if you only need to send mail through PHP. Instead, install msmtp. It's lightweight and much simpler to configure.

Install msmtp

sudo apt-get install msmtp-mta ca-certificates

Configure it

Create a new configuration file:

sudo vi /etc/msmtprc

...with the following configuration information:

# Set defaults.
defaults

# Enable or disable TLS/SSL encryption.
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

# Set up a default account's settings.
account default
host <smtp.example.net>
port 587
auth on
user <username@example.net>
password <password>
from <address-to-receive-bounces@example.net>
syslog LOG_MAIL

You need to replace the configuration data represented by everything within "<" and ">" (inclusive, remove these). For host/username/password, use your normal credentials for sending mail through your mail provider.

Tell PHP to use it

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

Add this single line:

sendmail_path = /usr/bin/msmtp -t
  • Of course, 'sudo vi /etc/php5/conf.d' without a php.ini won't work. In my case (Ubuntu), I had to edit '/etc/php5/apache2/php.ini' and also restart apache 'sudo /etc/init.d/apache2 reload' in order for it to work. – Dejan Dec 11 '12 at 14:01
  • 1
    BTW, you can test your msmtprc config using: cat <<EOF | msmtp <receiver@test.com> Subject: test This is a test! EOF – Dejan Dec 11 '12 at 14:06
  • Would be nice, but it's not working for me. Nothing appears in its log. It seems totally fine, but no dice. – James T Snell Dec 20 '12 at 1:58
  • 2
    To make msmtp available system-wide, install "msmtp-mta". – colan Dec 13 '13 at 21:27
  • 1
    In case you a too literal like me in following instructions, you don't actually keep the < > brackets in the config file. Took me a good 20 minutes to figure that out – de Raad Sep 25 '15 at 1:39
4

Should be a simple fix. You just need to install Postfix which does the actual emailing.

sudo apt-get install postfix

You want to run that from a terminal (and not the software centre) because it needs to ask you a few setup questions. You should be find to just accept all the defaults (local-only, etc).

2

This is an old question and there are worthy answers above, but I needed exim for my environment. Also, confirm you have sendmail installed in the first place ($ which sendmail -- should return something like /usr/sbin/sendmail). I found a good guide at linode.com, but the gist is:

apt-get install exim4-daemon-light mailutils

followed by:

dpkg-reconfigure exim4-config

which will get you to an interactive editor.

1

Great answer, con-f-use.

I have one thing to add about the "dummy gmail account":

This is highly recommended because you have to configure your gmail account to allow less secure applications: https://www.google.com/settings/u/0/security/lesssecureapps Otherwise you will get a "please log in using your web browser and then try again" error in your /var/log/mail.log file.

also I found that I get a timeout error unless I send the gmail smtp sever my root certificate. To do this:

sudo nano /etc/postfix/main.cf

add the following to the bottom of the file

smtp_tls_CAfile = /etc/postfix/cacert.pem

Then copy the thawte root cert to the cacert.pem file:

cat /etc/ssl/certs/thawte_Primary_Root_CA.pem | sudo tee -a /etc/postfix/cacert.pem

Here is the link I used : Configure Postfix to Use Gmail SMTP on Ubuntu | EasyEngine

Hopefully this saves someone a few hair follicles...

0

I found the solutions provided by Dima L. is the most easiest way & lightweight to system.

Just some addition for more clarity on setting. You should comment hostname in /etc/ssmtp/ssmtp.conf file. The sample ssmtp.conf file is as below:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.yourdomain.com
UseSTARTTLS=YES
AuthUser=mail@yourdomain.com
AuthPass=YOUREMAILPASSWORD

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
#hostname=

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.