使用GMAIL邮件服务器从运行PHP中的XAMP的本地主机发送电子邮件

2022-08-30 18:33:43

我尝试使用php mail()函数将电子邮件从localhost发送到我的yahoo电子邮件帐户,返回显示我已成功发送电子邮件,但我没有收到任何电子邮件。我一直在阅读并尝试许多所谓的“简单方法”来发送电子邮件,但结果令人失望,它们都不适合我。以下是代码、配置和错误消息。有人可以用这个启发我吗?谢谢。

断续器代码

<?php
$to      = 'myemail@yahoo.com';
$subject = 'Fake sendmail test';
$message = 'If we can read this, it means that our fake Sendmail setup works!';
$headers = 'From: myemail@egmail.com' . "\r\n" .
           'Reply-To: myemail@gmail.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully!';
} else {
    die('Failure: Email was not sent!');
}
?>

php.ini的配置(我使用的是 gmail 邮件服务器)

SMTP =smtp.gmail.com
smtp_port =587
sendmail_from = myemail@gmail.com
sendmail_path = “\”C:\xampp\sendmail\sendmail.exe\“ -t”

发送邮件的配置.ini

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
debug_logfile=debug.log
auth_username=myemail@gmail.com
auth_password=mypassword
force_sender=myemail@gmail.com

带有端口 587 的发送邮件错误日志中的错误消息

13/10/02 13:36:41 : 必须首先发出 STARTTLS 命令。k4sm129639pbd.11 - gsmtp


答案 1

这是给我答案的链接:

[安装]“用于窗口的假发送邮件”。如果您没有使用 XAMPP,可以在此处下载:http://glob.com.au/sendmail/sendmail.zip

[Modify] the php.ini file to use it (commented out the other lines):

[mail function]
; For Win32 only.
; SMTP = smtp.gmail.com
; smtp_port = 25

; For Win32 only.
; sendmail_from = <e-mail username>@gmail.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

(忽略“仅限Unix”位,因为我们实际上使用的是sendmail)

然后,您必须在安装 sendmail 的目录中配置“sendmail.ini”文件:

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com

要访问受双重验证保护的 Gmail 帐户,您需要创建应用专用密码。(来源)


答案 2

在 php.ini 文件中,取消注释此文件

sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"

和发送邮件中.ini

smtp_server=smtp.gmail.com
smtp_port=465
error_logfile=error.log
debug_logfile=debug.log
auth_username=your@gmail.com
auth_password=yourpassword
force_sender=your@gmail.com
hostname=localhost

配置这个..它会工作...它对我来说工作正常。

谢谢。


推荐