SwiftMailer does not send mail, why?
Asked Answered
N

7

8

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: <[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, $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.

Naquin answered 16/5, 2012 at 11:27 Comment(3)
Could you get a solution to this ?Seize
My solution was to replace it with a simple mail($email, 'Anmeldung', $nl_text, "From: " . ABSENDER); but it does not solve the problem so I got down votes and finally deleted my answer. The problem would require some debugging but I did not had the time for that.Naquin
Nice, you should post that as an answer.Naquin
W
7

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 "[email protected]", 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.

Whisk answered 26/6, 2014 at 9:51 Comment(0)
S
8

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.

Streetcar answered 16/5, 2012 at 12:10 Comment(0)
W
7

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 "[email protected]", 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.

Whisk answered 26/6, 2014 at 9:51 Comment(0)
S
6

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.

Seize answered 31/3, 2013 at 14:50 Comment(2)
Interestingly, this worked for me. An in-existent email address in the from field won't be deliveredLicking
Yes - (long time Swift user here) VERY annoying it fails with no error without a valid "reply to"Scavenger
P
2

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.

Photovoltaic answered 16/5, 2012 at 12:45 Comment(2)
I did that (see update). Still I think I go with using mail directly. Thanks for the effort!Naquin
You've eliminated the possibility of the culprit being in $to, $subject, or $body. Use the exact-same headers that SwiftMailer produced (you SHOULD NOT receive the email). Then, one by one remove each header and re-test until you find the header or headers that are causing it to fail. Repeat with $extraParams if necessary. From there you'll know what the specific problem is and have the ability to search for a better solution than mail() :)Photovoltaic
J
1

The problem is in 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".

Jernigan answered 18/2, 2014 at 15:6 Comment(1)
Thanks, can you add mote information? When does it fail? Link to Code Igniter bug, ... Right now this does not explain what the problem is nor how to fix it.Naquin
S
1

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.

Serviceman answered 15/7, 2017 at 0:4 Comment(1)
From what I recall, the mail is queued, and sent only on kernel termination. So this is normal.Folkway
S
-2

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 = '[email protected]';
->setFrom(array($sname => 'Some Header Title here'));
Seraglio answered 14/5, 2014 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.