SwiftMailer does not send mail, why?

可紊 提交于 2019-12-10 03:19:05

问题


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

I added the EchoLogger but it does not print anything.

$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 $failures but why?

Update

In Swift_Transport_SimpleMailInvoker::mail I dumped out the parameters and got this:

  $headers =>
  string(208) "Message-ID: <1337173064.4fb3a4480b8dd@domain.de>
Date: Wed, 16 May 2012 14:57:44 +0200
From: news@domain.de
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
"
  $extraParams =>
  string(15) "-fnews@domain.de"

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

Update #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.


回答1:


I had the same problem.

According to the documentation swiftmailer.org by default Swift_MailTransport::newInstance() uses parameters -f%s and that is why you got "-fnews@domain.de", to solve that place blank space between -f %s.

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

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

$message = Swift_Message::newInstance(null);

I hope it will help someone.

P.S. I use Postfix to send emails.




回答2:


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

If you can not use a debugger you should have a look at the function mail in 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.




回答3:


I was able to fix it. I was using noReply@mydomain.com in from header and this email account was not created. I created noReply@mydomain.com and my emails started working. I hope this helps somebody.




回答4:


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

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.




回答5:


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

The flag '-f' (see your code: string(15) "-fnews@domain.de") will fail in some situations. Code Igniter has the same "bug".




回答6:


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

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




回答7:


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

example:

$sname = 'noreply@system.com';
->setFrom(array($sname => 'Some Header Title here'));


来源:https://stackoverflow.com/questions/10617552/swiftmailer-does-not-send-mail-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!