Phpmailer无法发送邮件

时间:2015-09-07 09:22:40

标签: php phpmailer

我正在尝试使用phpmailer发送邮件。 mail-> Send返回true但邮件未送达

<?php
try {
    require_once('include/class.phpmailer.php');
    $mail = new PHPMailer(true);
    $mail->MailerDebug = true;
    $mail->IsSendmail();
    $mail->From = "library@capitalvia.com";
    $mail->FromName = "Library | CapitalVia";
    $mail->AddAddress("kanhaiya.lal@capitalvia.com", "Kanhaiyalal");
    $mail->AddReplyTo("library@capitalvia.com", "Library | CapitalVia");
    $mail->IsHTML(true);
    $mail->Subject = "Here is the subject";
    $mail->Body = "This is the HTML message body <b>in bold!</b>";
    $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
    if ($mail->Send())
        echo "Message has been sent";
} catch (phpmailerException $e) {
    echo $e->errorMessage();
} catch (Exception $e) {
    echo $e->getMessage();
}
?>

1 个答案:

答案 0 :(得分:-1)

我的第一个猜测是你忘了配置smtp服务器。如果没有提供smtp服务器,则PHPMailer类将无法发送电子邮件。 尝试添加以下内容:

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
// OR this if authentication is needed
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

此信息可在电子邮件应用程序(Outlook,Mail,Thunderbird,...)中的电子邮件帐户的配置/属性中找到。

This是Gmail的一个示例。