in Sysadmin

Using Google Apps as PHP mailer

Without any configuration a mail server, emails from PHP based applications often end up in the Spam folder in the receivers email or even worse – does not get sent at all. In this article I will explain how to setup Google Apps as your mailer on your Linux box. We will be enabling sendmail and configure it to relay mails using your Google Apps email and also configuring PHP to use sendmail as the default mailer.  Note that this also works if you are using standard Gmail without Google apps.

There are plenty of ways to implement SMTP mailing using PHP. For example in WordPress it exist a Plugin that can be used to configure WordPress to use SMTP instead of the mail() function. There are also large libraries like Swift Mailer that you can use for any custom PHP app. However, all of these only applies to the specific PHP app itself and you would need to store SMTP details within some sort of configuration file or in the database -this is a major security concern.  This article tells you how to configure system-wide configuration so all your web-apps works by just using the standard PHP mail functionality.

Before you standard, make sure you have enabled SMTP on your gmail account, this can be done within the Gmail settings:

Gmail Settings -> Forward and POP/IMAP > IMAP-access -> Activate IMAP

Installing sendmail

Assuming you are on a Debian based Linux (Ubuntu, Debian, etc):

Now we should configure sendmail with the sendmailconfig tool, just use the default answers to everything:

Configuring sendmail

Now we need to tell sendmail that we want to relay all mails that is being sent to your Google Apps email.

We need to let sendmail authenticate with Google Apps Email, so now we create a hash of your Authentication with Google Apps. Change and MyGoogleAppsPassword below before running the command.

Now we need to edit the file (located  /etc/mail/

Add these lines just BEFORE MAILER_DEFINITIONS and save the file.

Restart sendmail service:

Testing Mailing using mail command

We should be able to test that it sends mails by using the mail command, so go ahead and test it (change to your email):

Configure php.ini to use sendmail

The last step is to make sure php uses sendmail when using the mail() function. By default sendmail should be configured, so this step might not be necessary.

  • Remember php.ini is to be found in /etc/php5/cli/php.ini and e.g. /etc/php5/apache/php.ini and if you are using php-fpm /etc/php5/fpm/php.ini. All these files needs to have sendmail_path correct.

Open php.ini and make sure sendmail_path is like this:

Now test emails using the PHP mail function:


Share this post


Write a Comment