0

我在 Windows 7 上使用 XAMPP,但无法使用 php 发送邮件

php.ini 文件部分如下:

SMTP = smtp.gmail.com
smtp_port = 25
sendmail_from = hima***ivast***@gmail.com
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
mail.add_x_header = Off

sendmail.ini 文件部分如下:

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=hima***ivast***@gmail.com
auth_password=************
force_sender=hima***ivast***@gmail.com

请帮我找出错误。

4

1 回答 1

0

如果您在 Gmail 帐户上启用了 SSL,则端口号为 587。我相信另一个是 465,根据帮助文档:https ://support.google.com/mail/answer/78775?hl=en

如果您尝试在端口 465(使用 SSL)和端口 587(使用 TLS)上配置 SMTP 服务器,但仍然无法发送邮件,请尝试将您的 SMTP 配置为使用端口 25(使用 SSL)。

您可能还应该使用 try/catch 块 - 如果 sendmail 因错误而失败,您可以 print_r 或 var_dump 查看异常消息包含的内容。

我应该承认我从未直接使用过 sendmail。我发现 Rmail 库更易于使用:

<?php
    require_once( ROOT . DS . "libs" . DS . "Rmail" . DS . "Rmail.php" );

    // Instantiate a new HTML Mime Mail object
    $mail = new Rmail();

    $mail->setSMTPParams($host = 'smtp.gmail.com', $port = 587, $helo = null, $auth = true, $user = 'gmail address', $pass = 'password');

    // Set the From and Reply-To headers
    $mail->setFrom( "Proper Name <send from email address>" );
    $mail->setReturnPath( "reply email address" );

    // Set the Subject
    $mail->setSubject( 'Email subject/title' );

    $html = 'you email message content';

    // Create the HTML e-mail
    $mail->setHTML( $html );

    // Send the e-mail
    $result = $mail->send( array( 'target email address' ) );
?>

如果您更愿意使用上面的代码片段,我可以在某个地方找到该库。

于 2014-03-13T09:15:21.477 回答