如何使用梨邮件哑剧

时间:2010-12-26 20:02:21

标签: php sendmail

你如何使用谷歌的梨邮件mime。我发现这可以让你使用谷歌邮件,但不是邮件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";

我一直在

  

无法添加收件人:@localhost   [SMTP:收到无效的响应代码   来自服务器(代码:555,回复:   5.5.2语法错误。 f52sm5542930wes.35)]

修改

现在收到了电子邮件,但结果却是这样:

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!

5 个答案:

答案 0 :(得分:4)

看起来您的电子邮件标题存在问题。我根据梨邮件文档(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";

它适合我,所以我希望它对你有用! 干杯, 埃雷兹

答案 1 :(得分:2)

@john:使用您发布的链接中的代码,将其修改为 -

<?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";

?>

<强>更新

  

修改

     

现在收到了电子邮件,但结果却是这样:

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:更新

$body = $mime->get();

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

再试一次。

答案 2 :(得分:1)

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

除上述内容外,您还需要html_charset来发送电子邮件。

$crlf = "\n";

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

这将解决电子邮件中的ab类问题。

答案 3 :(得分:0)

无法对StealthyNinja的答案发表评论,所以我发布了自己的答案,抱歉。

问题也有点旧,但我可能对其他人有用。

要删除所有HTML标记和奇怪的字符,您必须准备标题,以便电子邮件客户端可以正确阅读电子邮件。试试这个 AFTER 设置$ headers数组:

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

之后应该可以正常工作。

答案 4 :(得分:0)

我已经使用此代码删除了= =符号后的3D。

$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);