php SwiftMailer 不发送邮件,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10617552/
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-24 22:38:17  来源:igfitidea点击:

SwiftMailer does not send mail, why?

phpemailswiftmailer

提问by PiTheNumber

SwiftMail does not send my email, while mail()does work. Same as here.

SwiftMail 不发送我的电子邮件,但mail()确实有效。和这里一样

I added the EchoLogger but it does not print anything.

我添加了 EchoLogger 但它不打印任何内容。

$message = Swift_Message::newInstance();
$message->setSubject('Test');
$message->setTo( $email );
$message->setFrom(ABSENDER);
$message->setBody($nl_text);

$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);

$logger = new Swift_Plugins_Loggers_EchoLogger();
$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));

$result = $mailer->send($message, $failures);
var_dump($failures);

The email is returned in $failuresbut why?

电子邮件被退回,$failures但为什么?

Update

更新

In Swift_Transport_SimpleMailInvoker::mailI dumped out the parameters and got this:

Swift_Transport_SimpleMailInvoker::mail我转储参数并得到这个:

  $headers =>
  string(208) "Message-ID: <[email protected]>
Date: Wed, 16 May 2012 14:57:44 +0200
From: [email protected]
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
"
  $extraParams =>
  string(15) "[email protected]"

$to, $subject, $bodyis same as I used in mail(). Still no idea what the problem is.

$to, $subject, $body和我在mail(). 仍然不知道是什么问题。

Update #2

更新 #2

This worked fine for me:

这对我来说很好:

mail($email, 'Test', $nl_text, "From: " . ABSENDER);

Disclaimer: This is not a solution but the workaround I used, because I did not have the time to debugger the framework and find a real solution. Feel free to use the information given above to debug yourself and post your solution here. I will gladly accept and upvote it.

免责声明:这不是解决方案,而是我使用的解决方法,因为我没有时间调试框架并找到真正的解决方案。随意使用上面给出的信息来调试自己并在此处发布您的解决方案。我会很乐意接受并赞成它。

采纳答案by VaL

I had the same problem.

我有同样的问题。

According to the documentation swiftmailer.orgby default Swift_MailTransport::newInstance() uses parameters -f%sand that is why you got "[email protected]", to solve that place blank space between -f %s.

根据文档swiftmailer.org默认情况下 Swift_MailTransport::newInstance() 使用参数-f%s,这就是为什么你得到"[email protected]", 来解决-f %s.

Your code now can be looks like that: $message = Swift_Message::newInstance('-f %s');

现在您的代码可以看起来像这样: $message = Swift_Message::newInstance('-f %s');

I solved that problem simply passing nullto __construct(). So my working code looks like that:

我通过简单地传递null给 __construct()解决了这个问题。所以我的工作代码如下所示:

$message = Swift_Message::newInstance(null);

$message = Swift_Message::newInstance(null);

I hope it will help someone.

我希望它会帮助某人。

P.S. I use Postfix to send emails.

PS 我使用 Postfix 发送电子邮件。

回答by Alex

I suggest you use the debugger and step to through the code behind send().

我建议您使用调试器并逐步完成 send() 背后的代码。

If you can not use a debugger you should have a look at the function mailin lib/classes/Swift/Transport/SimpleMailInvoker.php. There is a call to the internal mail function with an shutup operator (′@′). Maybe removing this operator can show you an error message. Also you can use var_dump()to dump the parameters passed there.

如果你不能使用调试器,你应该看看该功能maillib/classes/Swift/Transport/SimpleMailInvoker.php。使用关闭操作符 ('@') 调用内部邮件功能。也许删除此运算符可以向您显示错误消息。您也可以使用var_dump()转储在那里传递的参数。

回答by KutePHP

I was able to fix it. I was using [email protected] in from header and this email account was not created. I created [email protected] and my emails started working. I hope this helps somebody.

我能够修复它。我在标题中使用 [email protected] 并且未创建此电子邮件帐户。我创建了 [email protected] 并且我的电子邮件开始工作。我希望这对某人有帮助。

回答by Mike B

Looks like you're using the default transport mechanism which according to their docsuses mail()by default.

看起来您正在使用默认传输机制,根据他们的文档mail()默认使用。

Serious drawbacks when using this Transport are :

使用此传输时的严重缺点是:

  • Unpredictable message headers
  • Lack of feedback regarding delivery failures
  • Lack of support for several plugins that require real-time delivery feedback
  • 不可预测的消息头
  • 缺乏关于交付失败的反馈
  • 缺乏对需要实时交付反馈的几个插件的支持

source

来源

If I were debugging this, I would find SwiftMailer's implementation of the mail()function and dump the arguments it's passing and compare them to your own working version of mail(). I don't have any experience with SwiftMailer but this seems like a fast option.

如果我正在调试它,我会找到 SwiftMailer 的mail()函数实现并转储它传递的参数并将它们与您自己的mail(). 我对 SwiftMailer 没有任何经验,但这似乎是一个快速的选择。

回答by for_creator

The problem is in Swift_Transport_MailTransport, private $_extraParams = '-f%s'.

问题出在Swift_Transport_MailTransport, private $_extraParams = '-f%s'.

The flag '-f'(see your code: string(15) "[email protected]") will fail in some situations. Code Igniter has the same "bug".

该标志'-f'(请参阅您的代码string(15) "[email protected]":)在某些情况下会失败。Code Igniter 具有相同的“错误”。

回答by Sherif Badawi

I found out on symfony if I just exit the function with "exit()" for example, swiftmailer won't send the mail but if I ended with

我在 symfony 上发现,如果我只是用“exit()”退出函数,例如,swiftmailer 不会发送邮件,但如果我以

return new Response(); it sends fine. so I am not sure.

返回新的响应();它发送罚款。所以我不确定。

回答by tomexsans

This may be really old, Had the same problem i solved it when using a shorterfromemail. seems like the maximum length after the @ sign is 10. that is what i have tested so far.

这可能真的很旧,我在使用较短电子邮件时解决了同样的问题。@ 符号后的最大长度似乎是 10。这是我迄今为止测试过的。

example:

例子:

$sname = '[email protected]';
->setFrom(array($sname => 'Some Header Title here'));