Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftMailer does not send mail, why?

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.

like image 850
PiTheNumber Avatar asked May 16 '12 11:05

PiTheNumber


People also ask

How Swiftmailer works?

Swift Mailer is a component-based mailing library, which is compatible with PHP web apps. It was created by SensioLabs, a French company famous for its open-source software community commitment and in particular, for the creation of the Symfony framework.

What is Swift emails?

Swift Mailer is a component based library for sending e-mails from PHP applications.


2 Answers

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.

like image 170
Alex Avatar answered Oct 21 '22 13:10

Alex


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.

like image 26
VaL Avatar answered Oct 21 '22 12:10

VaL