PHP – How to Send Mail Using PHP

mailPHP

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?

Best Answer

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)

Related Question