I am trying to send an HTML message when using SMTP authentication in Gmail in PHP. Here is the script I'm 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. $bodyis an HTML table with images and other information.
When I execute the script, it fails with the following error:
Failed to set the sender: Some name [SMTP: invalid response code received from the server (code: 555, response: 5.5.2 Syntax error. C6sm20541406obd.22)]
Here is what I was trying to understand what is going wrong: 1. Using the same script, using "Mail" instead of "smtp" ie
$smtp = Mail::factory('Mail');
.
2. script mime.php, , HTML-.
- , , - SMTP HTML?
EDIT:
$mime->headers():
[MIME-Version] => 1.0
[From] => Some Name
[Return-Path] => Some Name
[Subject] => This is a test
[Content-Type] => multipart/alternative;
boundary="=_8662996a1f586248545d9f01f48e916d"