使用phpmailer类

时间:2011-06-11 08:57:17

标签: php phpmailer

我使用phpmailer类发送电子邮件。我收到错误“无法执行:/ smtp”下面的代码如下代码。有人可以告诉我这是什么问题。

require '../class.phpmailer.php'; 

try {
    $mail = new PHPMailer(true); //New instance, with exceptions enabled

    $body             = file_get_contents('contents.html');
    echo $body;
    $body             = preg_replace('/\\\\/','', $body); //Strip backslashes

    $mail->IsSMTP();                           // tell the class to use SMTP
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 25;                    // set the SMTP server port
    $mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
    $mail->Username   = "test@gmail.com";     // SMTP server username
    $mail->Password   = "1234567889";            // SMTP server password

    $mail->IsSendmail();  // tell the class to use Sendmail

    $mail->AddReplyTo("rto@gmail.com","First Last");

    $mail->From       = "from@gmail.com";
    $mail->FromName   = "First Last";

    $to = "toemail@gmail.com";

    $mail->AddAddress($to);

    $mail->Subject  = "First PHPMailer Message";

    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->WordWrap   = 80; // set word wrap

    $mail->MsgHTML($body);

    $mail->IsHTML(true); // send as HTML

    $mail->Send();
    echo 'Message has been sent.';
} catch (phpmailerException $e) {
    echo $e->errorMessage();
}

1 个答案:

答案 0 :(得分:4)

使用GMail,您的设置可能不正确;尝试这些(更正的端口和SSL地址):

$mail->Port       =  465;                   // set the SMTP server port
$mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
$mail->Username   = "test@gmail.com";       // SMTP server username
$mail->Password   = "1234567889";   

或者,使用TLS:

$mail->Port       =  587;                   // set the SMTP server port
$mail->Host       = "tls://smtp.gmail.com"; // SMTP server

编辑:删除下面的这一行(您首先提供设置以使用SMTP,然后您告诉它使用并发送通过sendmail,并且您在配置中放置了错误的路径。只需坚持一项服务,使用带有上述设置的SMTP(或下面的edit2)并查看):

删除:

$mail->IsSendmail();  // tell the class to use Sendmail

Edit2:其他选项(如果第一个答案不起作用):

$mail->SMTPSecure = 'ssl'; 
$mail->Host = 'smtp.gmail.com';