I'm trying to send an HTML message while using SMTP authentication to Gmail in PHP. Here is the script that I am using:
require_once "Mail.php";
require_once 'Mail/mime.php';
$from = "Some Name <myemail@gmail.com>";
$to = "Other Name <otheremail@gmail.com>";
$subject = "This is a test";
$crlf = "\n";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "myemail@gmail.com";
$password = "mypass";
$headers = array ('From' => $from,
'Return-Path' => $from,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mime = new Mail_mime($crlf);
$mime->setTXTBody("This is a test email message");
$mime->setHTMLBody($body);
$body = $mime->get();
$headers = $mime->headers($headers);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
Note: the $body
is an HTML table with images and other info.
When I execute the script it fails with the following error:
Failed to set sender: Some name [SMTP: Invalid response code
received from server (code: 555, response: 5.5.2 Syntax error.
c6sm20541406obd.22)]
Here is what I've tried to see what is going wrong:
1. Using the same script using 'Mail' instead of 'smtp' i.e.
$smtp = Mail::factory('Mail');
This works just fine.
2. Using the same script w/o the mime.php, this also works but doesn't allow one to send an HTML email.
Does anybody know how I can combine the two so that I'm still using SMTP authentication and send an HTML message?
EDIT:
Here is the dump of $mime->headers()
:
[MIME-Version] => 1.0
[From] => Some Name
[Return-Path] => Some Name
[Subject] => This is a test
[Content-Type] => multipart/alternative;
boundary="=_8662996a1f586248545d9f01f48e916d"
See the below URL I think it is very help full to you.
How to send an HTML email using SMTP in PHP
The Gmail's SMTP server address is SMTP.GOOGLEMAIL.COM not SMTP.GMAIL.COM.
Therefore Your settings should be:
// ...
$smtp=array();
$smtp['host']='ssl://smtp.googlemail.com';
See another example:-
Send An HTML & Plain Text Email Using An SMTP Server With PHP & PEAR
http://tonyvirelli.com/slider/php-html-email-using-smtp/
Try embracing the names in double quotes, like so:
$from = '"Some Name" <myemail@gmail.com>';
$to = '"Other Name" <otheremail@gmail.com>';
try it and tell us about it.
Update:
Ok, above didn't work, so I just would tell you what it's working for me in my php pages to send emails through smtp gmail.
- I use http://phpmailer.sourceforge.net/
- smtp server set to smtp.gmail.com
- it uses TLS protocol in port 587
so based on that you could try to change your config to:
$host = "tls://smtp.gmail.com";
$port = "587";
you can see here how to send mail through gmail with phpmailer.
Figured it out myself finally.
Thanks to Abid Hussain for providing the link to the example here: http://tonyvirelli.com/slider/php-html-email-using-smtp/
Solution:
- Remove the
'Return-Path' => $from
from the headers array.
- Add
To => $to
to the headers array(without this, the to header of the received email is Undisclosed recipients)
I had this issue and via debugging managed to get all kinds of different errors (below). Strangest thing is that it completely stopped working on about 11/2/2017. I didn't do any updates. Previously it would randomly work so I put in a retry and it always succeeded < 5 tries, until the 2nd. Strange.
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Invalid response code received from server (code: -1, response: )]
fsockopen(): Failed to enable crypto
fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error)
SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
(and so on)
Another used posted this troubleshooting step that helped me get more errors so I could Google more what could be the cause:
$result = fsockopen('ssl://smtp.gmail.com', 465, $error_no,
$error_message, 5);
if($result === false) {
echo "error no:
$error_no error message: $error_message"; echo print_r($result,true);
}else{
echo 'success\n\n';
}
My fix was to add a CA bundle file, since apparantly PHP couldn't verify the cert of Google:
- Download CA bundle https://curl.haxx.se/docs/caextract.html & extract to C:\Program Files\PHP\ssl\cacert.pem
- Specify location in php.ini: openssl.cafile = C:\Program Files\PHP\ssl\cacert.pem