SwiftMailer does not send mail, why?

30,452

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

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

Solution 3

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.

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

Solution 5

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.

Share:
30,452
PiTheNumber
Author by

PiTheNumber

Thanks for your interest in my profile! Here some thoughts: Reputation is like love: The more you give the more you get! I think we all should up vote more often. On answers and on questions. Also I like to quote: "Downvoting should be reserved for extreme cases." http://stackoverflow.com/privileges/vote-down

Updated on July 15, 2020

Comments

  • PiTheNumber
    PiTheNumber almost 4 years

    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.

  • PiTheNumber
    PiTheNumber about 12 years
    I did that (see update). Still I think I go with using mail directly. Thanks for the effort!
  • Mike B
    Mike B about 12 years
    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() :)
  • PiTheNumber
    PiTheNumber over 10 years
    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.
  • kakoma
    kakoma over 8 years
    Interestingly, this worked for me. An in-existent email address in the from field won't be delivered
  • anoldermark
    anoldermark over 6 years
    Yes - (long time Swift user here) VERY annoying it fails with no error without a valid "reply to"
  • Pierre-Olivier Vares
    Pierre-Olivier Vares over 4 years
    From what I recall, the mail is queued, and sent only on kernel termination. So this is normal.