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: <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.

Community
  • 1
  • 1
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • Could you get a solution to this ? – KutePHP Feb 27 '13 at 05:50
  • 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. – PiTheNumber Feb 27 '13 at 09:52
  • Nice, you should post that as an answer. – PiTheNumber Feb 28 '13 at 17:16

7 Answers7

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.

Alex
  • 32,506
  • 16
  • 106
  • 171
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 "-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.

Andreas
  • 5,305
  • 4
  • 41
  • 60
VaL
  • 776
  • 1
  • 10
  • 14
6

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.

KutePHP
  • 2,206
  • 6
  • 36
  • 54
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.

Mike B
  • 31,886
  • 13
  • 87
  • 111
  • I did that (see update). Still I think I go with using mail directly. Thanks for the effort! – PiTheNumber May 16 '12 at 13:03
  • 1
    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()` :) – Mike B May 16 '12 at 13:19
1

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".

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • 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. – PiTheNumber Feb 18 '14 at 15:24
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.

Sherif Badawi
  • 90
  • 1
  • 7
-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 = 'noreply@system.com';
->setFrom(array($sname => 'Some Header Title here'));
tomexsans
  • 4,454
  • 4
  • 33
  • 49