Swiftmailer 在本地主机上工作但不在生产中

发布于 2024-10-13 02:32:06 字数 1211 浏览 7 评论 0原文

在我的本地主机(Linux Mint OS)上,Swiftmailer 使用以下代码可以正常工作,但是每当我将其移动到我的服务器时,它就会挂在发送功能上,然后给我一个内部服务器错误。

我目前通过 Google Apps 设置电子邮件,所以 SMTP 可能不适用于这种情况?如果是这样,您建议我如何从 SMTP 更改此设置。

另外,每当我发送这样的电子邮件时,它都会显示用户名区域中的发件人地址。我希望它与“setFrom”函数中的发件人地址一起显示。

//Include the swiftmailer class
require_once 'comm/swiftmailer/swift_required.php';

//Create a message
//Pass it as a parameter when you create the message
$message = Swift_Message::newInstance();
$message->setSubject('My Subject');
$message->setFrom(array('[email protected]' => 'No Reply'));
$message->setTo(array('[email protected]' => 'Me'));
$message->setBody($emailContent, 'text/html');

//Create transport class and email the message
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('useracctname')->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

非常感谢您的帮助!

On my localhost (Linux Mint OS) Swiftmailer is working fine with the following code, but whenever I move it up to my server is just hangs at the send function and then gives me an internal server error.

I currently have my email setup through Google Apps, so maybe SMTP will not work for this situation? If thats the case, how do you suggest I change this from SMTP.

Also, whenever I send an email like this, it is showing up with a from address of the one in the username area. I want it to show up with the from address in the "setFrom" function.

//Include the swiftmailer class
require_once 'comm/swiftmailer/swift_required.php';

//Create a message
//Pass it as a parameter when you create the message
$message = Swift_Message::newInstance();
$message->setSubject('My Subject');
$message->setFrom(array('[email protected]' => 'No Reply'));
$message->setTo(array('[email protected]' => 'Me'));
$message->setBody($emailContent, 'text/html');

//Create transport class and email the message
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('useracctname')->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

Thanks a lot for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有心的人 2024-10-20 02:32:06

这可能是服务器上的端口问题。可以关闭端口 465 以防止垃圾邮件。也可能是服务器的 PHP 版本缺乏 SSL 支持。

但我想知道,是否我没有使用本地邮件服务器,因此它不允许 SMTP?

如果您像您一样明确地寻址 gmail,则您不太可能使用其他传输类型或不同的服务器。

另外,使用其他传输类型之一怎么样?

我认为 SMTP 是让它与 Gmail 一起运行的唯一选择。但是,您的服务器可能正在提供基于 mail() 的服务(显然,发件人地址与 GMail 不同)。在这种情况下,您也许可以使用 Swiftmailer 的邮件传输

This might be a port problem on the server. Port 465 could be closed to prevent spamming. It could also be that the server's version of PHP lacks SSL support.

I am wondering though, could it be that I am not using the local mail server so it wont allow SMTP?

If you address gmail as explicitly as you do, it's very unlikely you're using another transport type or a different server.

Also, what about using one of the other transport types?

I think SMTP is your only option to get it running with Gmail. It could, however, be that your server is providing a mail() based service (obviously with a different sender address than GMail though). In that case, you may be able to use Swiftmailer's mail transport.

画离情绘悲伤 2024-10-20 02:32:06

我遇到了同样的问题:我能够在本地发送 gmail 电子邮件(更新配置后:https: //www.google.com/settings/security/lesssecureapps )但不在生产中。

为了解决我的问题,我从我的生产服务器使用我的生产 IP 登录到我的 Gmail 帐户并回答 gmail 安全问题。

使用生产 IP 登录 Gmail 帐户的解决方案之一是

  1. 打开 ssh 隧道 (ssh -2NfCT -D 5000 [电子邮件受保护]
  2. 将您的浏览器配置为使用该代理(例如 Firefox:首选项 > 高级 > 网络 > 设置 > 手动代理配置 > SOCKS 主机:localhost / Post: 5000 / SOCKS v5 )
  3. 照常登录到您的 gmail 帐户

Symfony 输出的错误是:

app.ERROR: Exception occurred while flushing email queue: Failed to authenticate on SMTP server with username "joe.doe" using 1 possible authenticators [] []

I had the same problem : I was able to send gmail email in local (after updating that configuration : https://www.google.com/settings/security/lesssecureapps ) but not in production.

To fix my problem I've logged to my gmail account with my production ip / from my production server and answer the gmail security question.

One of the solution to log to your gmail account with your production ip, is to

  1. open an ssh tunnel (ssh -2NfCT -D 5000 [email protected])
  2. configure you browser to use that proxy (eg Firefox :to Preferences > Advanced > Network > Settings > Manual proxy configuration > SOCKS Hosts: localhost / Post: 5000 / SOCKS v5 )
  3. log to you gmail account as usual

The error outputed by Symfony was :

app.ERROR: Exception occurred while flushing email queue: Failed to authenticate on SMTP server with username "joe.doe" using 1 possible authenticators [] []
酒绊 2024-10-20 02:32:06

好吧,这是解决方案。

如果您要发送 SMTP 邮件,您应该在服务器上创建电子邮件帐户,然后在您的代码中使用此电子邮件信息。
如果您没有有效的电子邮件帐户来使用它发送电子邮件,服务器将不会发送电子邮件
当你使用“来自”时。
这应该是一个真实的、正确的电子邮件帐户,而不是一个假的。

使用假帐户发送的唯一可能原因是使用 mail() 函数。

well, here is the solution.

if you are sending SMTP mail you should create email account at the server then use this email info at your code.
the server will not send the email if you don't have valid email account to send using it
when you use "from".
this should be a true correct email account a fake one.

the only possible reason to send using fake account is using mail() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文