繁体   English   中英

如何使用梨邮件哑剧

[英]How to use pear mail mime

如何在Google中使用梨形邮件哑剧。 我发现了这个功能,可让您将梨形邮件与Google一起使用,但不能与邮件哑剧一起使用: 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!

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

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

@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'));

然后再试一次。

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

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

$crlf = "\n";

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

这样可以解决电子邮件中的Â像样的问题。

我已使用此代码在=符号后删除了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);

无法评论StealthyNinja的回答,所以我发表了自己的评论,对此感到抱歉。

这个问题也有点老了,但是我可能对其他人有用。

要摆脱所有HTML标记和怪异字符,您必须准备标题,以便电子邮件客户端可以正确阅读电子邮件。 设置$ headers数组后,请尝试以下操作:

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

之后应该可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM