在 gmail 中使用 php 的 swiftmailer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3478906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:55:27  来源:igfitidea点击:

Using php's swiftmailer with gmail

phpgmailswiftmailer

提问by andandandand

I'm writing a simple script in which a gmail account is used to send an email to itself.

我正在编写一个简单的脚本,其中使用 gmail 帐户向自己发送电子邮件。

I altered the script from SwiftMailer's reference, but I'm not getting any results. What's wrong?

我从 SwiftMailer 的参考中更改了脚本,但没有得到任何结果。怎么了?

Edit: after further debugging I've found that the statement

编辑:经过进一步调试后,我发现该语句

$result = $mailer->send($message);

causes the code to fail (the echo below it doesn't print).

导致代码失败(它下面的回声不打印)。

Why is this? Just cause the message isn't sent the program crashes? :/

为什么是这样?只是因为消息没有发送程序崩溃?:/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
<?php
    require_once '/var/www/swift/lib/swift_required.php';
    echo 'Mail sent <br />';  

/*  //create the transport
    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587);
      ->setUsername('[email protected]')
      ->setPassword('softrain1234')
    ;
*/

    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587)
      ->setUsername('[email protected]')
      ->setPassword('password')
    ;

    echo 'line 40 <br />';
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance('Wonderful Subject')
      ->setFrom(array('[email protected]' => 'Evaluaciones'))
      ->setTo(array('[email protected]'=> 'A name'))
      ->setBody('Test Message Body')
    ;
    echo 'line 52 <br />';

    $result = $mailer->send($message);
    echo $result;
    echo 'line 58 <br />';

?>
</body>
</html>

The test form:

测试形式:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Test Mail Script</title>
    </head>
    <body>
        <form action="scriptmail.php" method="post">
            <input type="submit"/>
            </table>
        </form>
  </body>
</html>

回答by fullybaked

Don't mean to resurrect an old post, but just in case others are looking for the answer, and because this post came up during my search for a solution despite the age.

并不是要复活一个旧帖子,但以防万一其他人正在寻找答案,因为尽管年龄很大,但在我寻找解决方案的过程中出现了这篇文章。

When using PHP SwiftMailer to connect to Gmail or Google Apps email accounts you need to use the following

当使用 PHP SwiftMailer 连接到 Gmail 或 Google Apps 电子邮件帐户时,您需要使用以下内容

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername($this->username)
  ->setPassword($this->password);

$this->mailer = Swift_Mailer::newInstance($transporter);

This works fine for me. In the original code, you are using port 587 not 465 and you are not specifying the protocol (ssl). Don't know if that bit matters, but for me port 587 failed and 465 worked fine.

这对我来说很好用。在原始代码中,您使用的是端口 587 而不是 465,并且您没有指定协议 (ssl)。不知道那一点是否重要,但对我来说,端口 587 失败了,而 465 工作正常。

Hope that helps.

希望有帮助。

回答by Gene

I found this question doing a google search and used the code in the answer provided by fullybaked. It got me to the point where I could see in the Exceptions thrown that the Google smtp server was responding with an error code indicating password and username not accepted. Upon further investigation I found the following steps. Now it works great and I can continue to flesh out the code for production.

我在谷歌搜索中发现了这个问题,并使用了 fullbaked 提供的答案中的代码。它让我在抛出的异常中看到 Google smtp 服务器正在响应错误代码,指示密码和用户名不被接受。经过进一步调查,我发现了以下步骤。现在它工作得很好,我可以继续充实生产代码。

Not sure when Google made the change but in addition to the configuring your code with your Google username, password and port (465/ssl or 587/tls) you need to do the following steps to be able to use the Google smtp server.

不确定 Google 何时进行更改,但除了使用您的 Google 用户名、密码和端口(465/ssl 或 587/tls)配置您的代码之外,您还需要执行以下步骤才能使用 Google smtp 服务器。

To use the the gmail smtp server with your gmail account you need to:

要将 gmail smtp 服务器与您的 gmail 帐户一起使用,您需要:

1) In Google "Account settings" enable "Access for less secured apps" by setting it to "Allow".

1) 在 Google 的“帐户设置”中,通过将其设置为“允许”来启用“访问安全性较低的应用程序”。

2) In gmail settings under the "Forwarding and POP/IMAP" tab, check the IMAP status, it needs to be enabled. (This will allow the emails sent to be saved in the sent folder.)

2) 在“转发和 POP/IMAP”选项卡下的 gmail 设置中,检查 IMAP 状态,它需要启用。(这将允许发送的电子邮件保存在已发送文件夹中。)

3) If after steps 1 and 2 you still throw the Exception Google's smtp server is not accepting the user name and password you need to open a browser, go to gmail and log in, then open another tab in the same browser andgo to this address:
https://accounts.google.com/DisplayUnlockCaptcha

3) 如果在步骤 1 和 2 之后您仍然抛出异常 Google 的 smtp 服务器不接受您需要打开浏览器的用户名和密码,请转到 gmail 并登录,然后在同一浏览器中打开另一个选项卡并转到此地址:https:
//accounts.google.com/DisplayUnlockCaptcha

According to Google you may have to enter a captcha.

4) Immediately send an email from your code as this will allow it to be authorized to use your gmail account.

4) 立即从您的代码发送一封电子邮件,因为这将允许它被授权使用您的 Gmail 帐户。

Hope this helps.

希望这可以帮助。

回答by Richard

I found that what evad said was true, but I had to change his work a little bit for the current version of Swift Mailer. Now it is:

我发现 evad 说的是真的,但我不得不为当前版本的 Swift Mailer 对他的工作进行一些更改。现在它是:

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername('[email protected]')
  ->setPassword('password');

$mailer = Swift_Mailer::newInstance($transporter);

Should work fine.

应该工作正常。

回答by ducin

465 is the port for ssl, 587 is used for tls encryption (look at http://swiftmailer.org/docs/sending.html#encrypted-smtp)

465 是 ssl 的端口,587 用于 tls 加密(查看http://swiftmailer.org/docs/sending.html#encrypted-smtp

回答by Jim W.

The GMail SMTP system has it's issues given the SSL and ports. I find it hard to get it to work with PHP nicely.

鉴于 SSL 和端口,GMail SMTP 系统存在问题。我发现很难让它很好地与 PHP 一起工作。

The best thing I have found, that does work is phpGMailer. You may be able to sift through that code to see how they got it to work, but that has always worked flawlessly for me.

我发现的最好的东西是phpGMailer。您也许可以筛选该代码以查看它们是如何使其工作的,但这对我来说总是完美无缺。

I know this does not address the SwiftMail issue, just figured I would point that out :)

我知道这不能解决 SwiftMail 问题,只是想我会指出这一点:)

回答by Dan Kinchen

I also came here after hours of research. I finally found the true answer. I wanted to send email FROM google mail servers and not from my host with smtp authentication.

经过数小时的研究,我也来到了这里。我终于找到了真正的答案。我想通过 smtp 身份验证从谷歌邮件服务器而不是从我的主机发送电子邮件。

You can trace an email with its full headers inside of gmail, follow this guide.

您可以在 gmail 中跟踪带有完整标题的电子邮件,请按照本指南进行操作。

https://support.google.com/mail/answer/29436?hl=en

https://support.google.com/mail/answer/29436?hl=en

@fullybaked had the right answer for authenticating your email via smtp but you would still be sending via your host.

@fullybaked 有通过 smtp 验证您的电子邮件的正确答案,但您仍然会通过您的主机发送。

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername($this->username)
->setPassword($this->password);

$this->mailer = Swift_Mailer::newInstance($transporter);

@ducin was correct with the different ports and encryption types.

@ducin 对于不同的端口和加密类型是正确的。

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')

OR more modern and more secure, use;

或者更现代、更安全,使用;

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')

If you want to send directly from a gmail server you need to set the authorization mode. Plain is default.

如果您想直接从 Gmail 服务器发送,您需要设置授权模式。普通是默认的。

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
->setAuthMode('login')
->setUsername('[email protected]')
->setPassword('password');

$mailer = Swift_Mailer::newInstance($transporter);

@gene is also correct, you do have to authenticate your app, so follow his instruction.

@gene 也是正确的,您必须验证您的应用程序,因此请按照他的说明进行操作。

Further to prevent your email from landing in the spam folder, you will also have to add dns text records with DKIM (domain keys)

此外,为了防止您的电子邮件进入垃圾邮件文件夹,您还必须使用 DKIM(域密钥)添加 dns 文本记录

https://support.google.com/a/answer/174124?hl=en

https://support.google.com/a/answer/174124?hl=en

SPF records

SPF 记录

https://support.google.com/a/answer/33786?hl=en

https://support.google.com/a/answer/33786?hl=en

and DMARC

和 DMARC

https://support.google.com/a/answer/2466580?hl=en

https://support.google.com/a/answer/2466580?hl=en