How to use pear mail mime

跟風遠走 提交于 2019-12-13 13:30:21

问题


How can you use pear mail mime with google. I found this which lets you use pear mail with google, but not mail mime: http://globalconstant.scnay.com/2009/11/06/sending-email-through-gmail-using-php/

require_once "Mail.php";
require_once "Mail/mime.php";

$from = "Sender <*******@googlemail.com>";
$to = "Receiver <*******@googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";

$headers = array('From' => $from,
                 'To' => $to,
                 'Subject' => $subject);


$host = "smtp.gmail.com";
$port = 465;
$username = "********@googlemail.com";
$password = "********";

$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html);

$body = $mime->get();
$headers = $mime->headers($headers);

$smtp = Mail::factory("smtp",array("host" => $host,
                      "port" => $port,
                      "auth" => true,
                      "username" => $username,
                      "password" => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";

I keep getting

Failed to add recipient: @localhost [SMTP: Invalid response code received from server (code: 555, response: 5.5.2 Syntax error. f52sm5542930wes.35)]

Edit:

The email is now received, however it turns out like this:

This is a message I sent from <a href=3D"http://www.php.net/">PHP</a> using=
 the PEAR Mail package and SMTP through Gmail. Enjoy!

回答1:


It looks like you have an issue with the email header. I updated your code based on the pear mail doc (http://pear.php.net/manual/en/package.mail.mail-mime.example.php):

require_once "Mail.php";
require_once "Mail/mime.php";

$from = "Sender <*******@googlemail.com>";
$to = "Receiver <*******@googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";

$headers = array('From' => $from,
                 'To' => $to,
                 'Subject' => $subject);


//$host = "smtp.gmail.com";
$host = "ssl://smtp.gmail.com"; // try this one to use ssl
$port = 465;
$username = "********@googlemail.com";
$password = "********";

//$mime = new Mail_mime($crlf);
$mime =  new Mail_mime(array('eol' => $crlf)); //based on pear doc     
$mime->setHTMLBody($html);

//$body = $mime->get();
$body = $mime->getMessageBody(); //based on pear doc above
$headers = $mime->headers($headers);

$smtp = Mail::factory("smtp",array("host" => $host,
                      "port" => $port,
                      "auth" => true,
                      "username" => $username,
                      "password" => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";

It works for me so I hope it will work for you! Cheers, Erez




回答2:


@john: Using the code from the link you posted, modify it like so --

<?php
require_once('Mail.php');
require_once('Mail/mime.php');

$from = 'Sender <sender@gmail.com>';
$to = 'Receiver <receiver@something.com>';
$subject = 'Sent from PHP on my machine';

$text = 'This is a message I sent from <a href="http://www.php.net/">PHP</a> '
      . 'using the PEAR Mail package and SMTP through Gmail. Enjoy!';

$message = new Mail_mime();
$message->setTXTBody(strip_tags($text)); // for plain-text
$message->setHTMLBody($text);
$body = $message->get();

$host = 'smtp.gmail.com';
$port = 587; //According to Google you need to use 465 or 587
$username = 'sender';
$password = 'your_password';

$headers = array('From' => $from,
    'To' => $to,
    'Subject' => $subject);

$smtp = Mail::factory('smtp',
    array(
        'host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo $mail->getMessage();
} else {
    echo "Message sent successfully!";
}

echo "\n";

?>

Update:

Edit:

The email is now received, however it turns out like this:

This is a message I sent from <a href=3D"http://www.php.net/">PHP</a> using=
the PEAR Mail package and SMTP through Gmail. Enjoy!

@john: Update

$body = $mime->get();

to

$body = $mime->get(array('text_charset' => 'utf-8'));

and try again.




回答3:


$body = $mime->get(array('text_charset' => 'utf-8'));

In addition to the above you need html_charset for html emails.

$crlf = "\n";

$body = $mime->get(array('html_charset' => 'utf-8', 'text_charset' => 'utf-8', 'eol' => $crlf));

This will fix the abberations like  in the e-mails.




回答4:


Couldn't comment on StealthyNinja's answer so I posted my own, sorry about that.

The question is also a bit old but I maybe this could be useful to others.

To get rid of all that HTML tags and weird characters you have to prepare your header so the e-mail client can read the e-mail right. Try this AFTER setting your $headers array:

$headers = $message->headers($headers);

It should work ok after that.




回答5:


I have used this code to remove 3D after = sign.

$hdrs = array( 'From'    => $from,
       'To'      => $to,
       'Subject' => $subject  );

$mime =& new Mail_mime();
$mime->setTXTBody($message);

if($htmlMessage==""){
    $htmlMessage=$message;
}

$mime->setHTMLBody($htmlMessage);
if($attachmentIsFile){
    if($attachment!=null)
        $mime->addAttachment($attachment,'application/octet-stream',$attachmentName.extractExtension($attachment));
}else{
    if($attachment!="")
        $mime->addAttachment($attachment,'application/octet-stream',$attachmentName,false);
}
$body = $mime->get(array('text_encoding' => '8bit','html_encoding' => '8bit'));
$hdrs = $mime->headers($hdrs);


来源:https://stackoverflow.com/questions/4535209/how-to-use-pear-mail-mime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!