【问题标题】:How to use pear mail mime如何使用梨邮件 mime
【发布时间】:2011-05-30 22:19:34
【问题描述】:

您如何在 google 中使用 pear mail 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!

【问题讨论】:

  • 你能详细说明你的意思吗?
  • @Pekka:我认为@john 的意思是他希望发送 HTML 电子邮件。
  • 抱歉,PHP 代码没有显示出来,该死的 Markdown,它现在就在那里。是的,我想发送一封 HTML 电子邮件。 ^
  • 为什么投反对票?我已经解决了这个问题。

标签: php sendmail


【解决方案1】:

@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】:

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

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

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

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

之后应该可以正常工作了。

【讨论】:

    【解决方案3】:

    您的电子邮件标头似乎有问题。我根据 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";
    

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

    【讨论】:

      【解决方案4】:
      $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));
      

      这将修复电子邮件中的 Â 之类的缩写。

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-01
          • 2013-04-30
          • 1970-01-01
          • 2013-10-27
          • 2014-02-21
          • 2011-10-21
          • 2015-11-20
          • 1970-01-01
          相关资源
          最近更新 更多