Skip to content

Instantly share code, notes, and snippets.

@Evan-R
Forked from HuySokhom/PHP_Setup_STMP_Gmail.md
Created August 20, 2014 10:58
Show Gist options
  • Select an option

  • Save Evan-R/707d8925807f884d240d to your computer and use it in GitHub Desktop.

Select an option

Save Evan-R/707d8925807f884d240d to your computer and use it in GitHub Desktop.

Install ssmtp

sudo apt-get update
sudo apt-get install ssmtp

Check where the binary and the ssmtp.conf file ended up. You’ll need to know the locations for the following steps:

whereis ssmtp
ssmtp: /usr/sbin/ssmtp /etc/ssmtp /usr/share/man/man8/ssmtp.8.gz

Configure ssmtp

Edit /etc/ssmtp/ssmtp.conf

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

# 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.gmail.com:587

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

# The full hostname
hostname=your-full-gmail-address

# 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

UseSTARTTLS=YES
AuthUser=your-gmail-username-here
AuthPass=your-gmail-password-here

Setup your users

Edit `/etc/ssmtp/revaliases`

  You’ll probably want to set up your local user and root for sedning mail:
    
    root:username@gmail.com:smtp.gmail.com:587
    localusername:username@gmail.com:smtp.gmail.com:587

Configure PHP

Edit `/etc/php5/apache2/php.ini`
  Find the [mail function] configuration line:
  
  it must be look like this:
  
  sendmail_path = /usr/sbin/ssmtp -t

sudo service apache2 restart

Send a mail

    $to      = 'your@email.com';
    $subject = 'the subject';
    $message = 'hello world';
    $headers = 'From: webmaster@example.com' . "\r\n" .
    		'Reply-To: webmaster@example.com' . "\r\n" .
    		'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    
    echo 'mail has been send';

HAPPY :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment