dongxikuo5171 2010-12-26 20:02
浏览 29

如何使用梨邮件哑剧

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 = "
";
$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 "
";

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!
  • 写回答

5条回答 默认 最新

  • doukuang1897 2010-12-26 20:35
    关注

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

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行