【问题标题】:Sending mail via pear mail通过梨邮件发送邮件
【发布时间】:2014-02-21 04:16:49
【问题描述】:

对不起我的英语。 我正在尝试通过邮件梨包发送电子邮件:

require_once "Mail.php";

$from = '<frommail@gmail.com>';
$to = '<tomail@vacant.lv>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

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

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'frommail@gmail.com',
        'password' => 'pass'
    ));

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

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

//email addresses are changed

但结果我有错误: 无法连接到 ssl://smtp.gmail.com:465 [SMTP:无法连接套接字:权限被拒绝(代码:-1,响应:)] Openssl 已启用。 谢谢。

【问题讨论】:

    标签: php email ssl pear


    【解决方案1】:

    在某个阶段,您可能想要发送 HTML 电子邮件,因此您还需要使用 PEAR 提供的 Mail/mime 包。我也加入了。

    <?php
    require_once "Mail.php";
    require_once "Mail/mime.php";
    
    $from = "<noreply@example.com>";
    $to = "fred@example.com"; // the email address
    $host = "smtp.gmail.com";
    $port = "587";
    // use the first example username if sending from gmail, as full email address 
    // is required
    $username = "fred.flintstone@gmail.com";
    $username = "fred";
    $password = "secure";
    $headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
    $mailbody = "<html><body>...</body></html>";
    
    $mime = new Mail_mime();
    $mime->setHTMLBody($mailbody);
    $body = $mime->get();
    $headers = $mime->headers($headers);
    $smtp = Mail::factory(
        'smtp',array (
            'host' => $host,
            'auth' => true,
            'username' => $username,
            'password' => $password,
            'port' => $port
        )
    );
    
    // send email
    $mail = $smtp->send($to, $headers, $body);
    if (PEAR::isError($mail)) {
        echo($mail->getMessage());
    } else {
        echo "<b><Center>Succesfully sent email to</b>$to</center>";
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 2013-01-11
      • 1970-01-01
      • 2016-05-15
      • 2014-03-05
      • 2014-08-31
      • 2021-03-11
      • 2011-05-05
      • 2017-05-06
      相关资源
      最近更新 更多