无法使用xampp从gmail发送邮件(使用php脚本)

时间:2014-03-13 09:11:08

标签: php email windows-7 xampp sendmail.exe

我在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

请帮我找到错误。

1 个答案:

答案 0 :(得分:0)

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

  

如果您尝试在端口465(使用SSL)上配置SMTP服务器   端口587(使用TLS),但仍然无法发送邮件,请尝试   配置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' ) );
?>

如果您更喜欢使用上面的代码片段,我可以在某个地方追踪图书馆。

相关问题