Using Gmail and MSMTP To Send Mail From PHP

1. Enabled IMAP access under your Gmail account, to be able to connect to Gmail via something other than its web interface:

Settings - Forwarding and POP/IMAP - IMAP Access - Enable IMAP - Save Changes


2. Set PHP to use MSMTP as “sendmail”:

Edit file php.ini (C:\WampDeveloper\Config\Php\php.ini)

Disable mail()’s default use of a local mail server.

;SMTP = localhost
;smtp_port = 25

* Comment the above lines for SMTP and smtp_port by using the “;” character.

Enable mail()’s use of MSMTP.

sendmail_path = "C:/WampDeveloper/Tools/msmtp/msmtp.exe -d -C C:/WampDeveloper/Tools/msmtp/msmtprc.ini -t --read-envelope-from"

mail.log = "C:/WampDeveloper/Logs/Php/maillog.txt"

* Uncomment the above lines by removing the “;” character. And make sure to update the paths to the correct drive letter.

3. Define your Gmail account information for MSMTP to use (using Gmail’s outgoing mail settings):

Edit file msmtprc.ini (C:\WampDeveloper\Tools\msmtp\msmtprc.ini)

# Default values for all accounts
tls_certcheck off
logfile C:/WampDeveloper/Tools/msmtp/msmtplog.txt

* Make sure to update the logfile path to the correct drive letter.

account Gmail
port 587
auth on
tls on
password mypassword

* Update the above lines with your email address and password.

4. Send a test email:

Create file C:\WampDeveloper\Websites\\webroot\send-email.php

Set the proper “$from” and “$to” email addresses.

  $from    = '';
  $to      = 'someone.else@somewhere.else';

  $subject = 'Test Subject';
  $message = 'Hello. Testing email.';
  $headers =
    'From: ' . $from . "\r\n" .
    'Reply-To: ' . $from . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

  if(mail($to, $subject, $message, $headers))
    echo 'Mail function returned successfully. Mail has probably been sent.';
    echo 'Error! Mail function failed. Mail NOT sent.';

* Make sure that your “From:” email address matches the email address defined for Gmail in msmtprc.ini. You cannot use another email address (Gmail will reject it).

Access URL

Then check your Gmail’s Sent folder, and your receiving account’s Inbox and Spam folder.

5. Debugging MSMTP:

If you do not receive your test email, and your Gmail account does not have a copy of the email in its Sent folder, check MSMTP’s log file for clues (msmtplog.txt).

You can also test sending a basic email directly from the command line (to bypass PHP) to see if MSMTP is working itself (it will output debug info)…

echo Subject: Testing Email. | C:\WampDeveloper\Tools\msmtp\msmtp --debug --file=C:/WampDeveloper/Tools/msmtp/msmtprc.ini --logfile=C:/WampDeveloper/Tools/msmtp/msmtplog.txt -t someone.else@somewhere.else

* Update the above paths, and the “from” ( and “to” (someone.else@somewhere.else) email addresses. This email will only have a subject line, it will not have a body due to the limitations of the command line.

6. Help:

PHP Mail Function
Problems sending mail with POP or IMAP

One thought on “Using Gmail and MSMTP To Send Mail From PHP”

Leave a Reply to Bernard Gineste Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>