从 PHP 页面使用 GMail SMTP 服务器发送电子邮件
我尝试从 PHP 页面通过 GMail 的 SMTP 服务器发送电子邮件,但收到此错误:
身份验证失败 [SMTP:SMTP 服务器不支持身份验证(代码:250,响应:mx.google.com 在您的服务中,[98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]
有人可以帮忙吗?这是我的代码:
<?php
require_once "Mail.php";
$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$port = "587";
$username = "[email protected]";
$password = "testtest";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
I am trying to send an email via GMail's SMTP server from a PHP page, but I get this error:
authentication failure [SMTP: SMTP server does no support authentication (code: 250, response: mx.google.com at your service, [98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]
Can anyone help? Here is my code:
<?php
require_once "Mail.php";
$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$port = "587";
$username = "[email protected]";
$password = "testtest";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
使用 Swift mailer,通过 Gmail 凭据发送邮件非常简单:
Using Swift mailer, it is quite easy to send a mail through Gmail credentials:
您的代码似乎未使用 TLS (SSL),即 需要将邮件递送至 Google(并使用端口 465 或 587)。
您可以通过设置
$host = "ssl://smtp.gmail.com";
您的代码看起来可疑地像 此示例引用主机名方案中的 ssl:// 。
Your code does not appear to be using TLS (SSL), which is necessary to deliver mail to Google (and using ports 465 or 587).
You can do this by setting
$host = "ssl://smtp.gmail.com";
Your code looks suspiciously like this example which refers to ssl:// in the hostname scheme.
我不推荐梨邮件。自 2010 年以来就没有更新过。还要阅读源文件;源代码几乎已经过时,以 PHP 4 风格编写,并且已经发布了许多错误/错误(谷歌它)。我正在使用 Swift Mailer。
Swift Mailer 集成到任何用 PHP 5 编写的 Web 应用程序中,提供灵活而优雅的面向对象的方法来发送具有多种功能的电子邮件。
它是免费且开源的,您可以下载 Swift Mailer 并上传到您的服务器。 (功能列表是从所有者网站复制的)。
Gmail SSL/SMTP 和 Swift Mailer 的工作示例在这里......
I don't recommend Pear Mail. It has not been updated since 2010. Also read the source files; the source code is almost outdated, written in PHP 4 style and many errors / bugs have been posted (Google it). I am using Swift Mailer.
Swift Mailer integrates into any web application written in PHP 5, offering a flexible and elegant object-oriented approach to sending emails with a multitude of features.
It is a free and open source you can Download Swift Mailer and upload to your server. (The feature list is copied from owner website).
The working example of Gmail SSL/SMTP and Swift Mailer is here...
自 2022 年 5 月 30 日起,Google将不再支持使用允许您使用用户名和密码登录 Google 帐户的第三方应用程序和设备。
然而,谷歌提供了一个简单的解决方案。
输入由 Google 生成的应用密码,而不是密码。首先,转到设置并启用
两步验证
。然后单击
应用程序密码
。您应该会看到应用程序密码屏幕。 应用密码可让您从不支持两步验证的设备上的应用登录您的 Google 帐户。选择
Mail
作为应用,然后选择设备。就我而言,我选择了其他
,因为我想将我的应用程序部署到云中。完成后,单击
GENERATE
按钮。您将看到生成的应用程序密码。只需复制密码并将电子邮件发送服务中以前的密码替换为生成的密码即可。但您将无法再次看到密码。
就是这样!
As of May 30, 2022, Google will no longer support the use of third-party applications and devices that allow you to sign in to your Google Account with your username and password.
However, there is an easy solution provided by Google.
Instead of a password, input an app password generated by Google. Firstly, go to the settings and enable
2-Step Verification
.Then click on the
App passwords
.You should see the App passwords screen. App passwords let you sign in to your Google Account from apps on devices that don't support 2-Step Verification. Select
Mail
as the app and then select a device. In my case, I choseOther
, because I want to deploy my application to the cloud.Once done, click on the
GENERATE
button. You will see your generated app password.Just copy the password and replace the previous password in your email sending service with the generated one. You won't be able to see the password again though.
That's it!
SwiftMailer 可以使用外部服务器发送电子邮件。
以下示例展示了如何使用 Gmail 服务器:
SwiftMailer can send E-Mail using external servers.
here is an example that shows how to use a Gmail server:
问题中列出的代码需要进行两处更改
SSL 连接需要端口 465。
The code as listed in the question needs two changes
Port 465 is required for an SSL connection.
通过 Gmail 使用 phpMailer 库发送邮件
请从 Github 下载库文件
Send Mail using phpMailer library through Gmail
Please donwload library files from Github
我也有这个问题。我设置了正确的设置并启用了不太安全的应用程序,但它仍然不起作用。最后,我启用了这个https://accounts.google.com/UnlockCaptcha,它对我有用。
I had this problem also. I set the correct settings and have enabled less secure apps but it still did not work. Finally, I enabled this https://accounts.google.com/UnlockCaptcha, and it worked for me.
要在 Ubuntu 中安装 PEAR 的 Mail.php,请运行以下命令集:
To install PEAR's Mail.php in Ubuntu, run following set of commands:
Gmail需要465端口,也是phpmailer的代码
Gmail requires port 465, and also it's the code from phpmailer
我知道这是一个老问题,但它仍然活跃,我看到的所有答案都显示了基本身份验证,但已弃用。以下示例展示了如何使用带有 XOAUTH2 身份验证的 PHPMailer 通过 Google 的 Gmail 服务器使用 SMTP 发送电子邮件:
参考: PHPMailer 示例文件夹
I know this is an old question but it's still active and all the answers I saw showed basic authentication, which is deprecated. Here is an example showing how to send email using SMTP via Google's Gmail servers using PHPMailer with XOAUTH2 authentication:
Reference: PHPMailer examples folder
我有一个针对没有“@gmail.com”后缀的 GSuite 帐户的解决方案。另外,我认为它适用于带有 @gmail.com 的 GSuite 帐户,但还没有尝试过。
首先,您应该有权更改 GSuite 帐户的“允许安全性较低的应用程序”选项。如果您有权限(您可以检查帐户设置 -> 安全性),那么您必须停用“两步因素身份验证”,转到页面末尾并设置为“是”以允许安全性较低的应用程序。就这样。如果您没有更改这些选项的权限,则该线程的解决方案将不起作用。检查https://support.google.com/a/answer/6260879?hl =en 更改“允许较少...”选项。
I have a solution for GSuite accounts that doesnt have the "@gmail.com" sufix. Also I think it will work for GSuite accounts with the @gmail.com but havent tried it.
First you should have the privileges to change the option "allos¿w less secure app" for your GSuite account. If you have the privileges (you can check in account settings->security) then you have to deactivate "two step factor authentication" go to the end of the page and set to "yes" for allow less secure applications. That's all. If you dont have privileges to change those options the solution for this thread will not work. Check https://support.google.com/a/answer/6260879?hl=en to make changes to "allow less..." option.
我尝试了@shasi kanth 提供的建议,但没有成功。我阅读了文档,并进行了一些更改。所以我设法使用此代码通过Gmail发送邮件,其中vendor/autoload.php是由composer通过composer require "swiftmailer/swiftmailer:^6.0"获取的:
I tried the suggestion offered by @shasi kanth, but it didn't work out. I read the documentation and there are few changes made. So I managed to send mail via Gmail using this code, where vendor/autoload.php is got by composer with composer require "swiftmailer/swiftmailer:^6.0":
另外,查看
端口 25 是否工作。
Set
Also, see if port 25 works.