Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3.3 and Swiftmailer - mail created and sent by controller deferred by server

I'm trying to use Swiftmailer to send emails out from a website. The emails keep getting deferred because Swiftmailer is attempting to use my server's IP address rather than localhost as the relay:

Aug  2 14:18:28 picus sm-mta[21171]: v72IIS0I021171: from=<[email protected]>, size=347, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Aug  2 14:18:28 picus sm-mta[21173]: v72IIS0I021171: to=<[email protected]>, delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=120347, relay=example.com. [my.servers.ip.address], dsn=4.0.0, stat=Deferred: Connection refused by example.com.

My Symfony controller code, config, and parameters -

Relevant controller code:

if ($form->isSubmitted() && $form->isValid()) {
    $data = $form->getData();

    $this->addFlash('success', 'Message sent successfully');

    $data['message'] = str_replace("\n.", "\n..", $data['message']);

    $mail = (new \Swift_Message())
        ->setSubject("[From My Website] - {$data['subject']}")
        ->setFrom($data['email'])
        ->setTo('[email protected]')
        ->setBody("{$data['name']} wrote the following message:\n\n{$data['message']}");

    $this->get('mailer')->send($mail);

    return $this->redirect($this->generateUrl('_home'));
}

config.yml:

# Swiftmailer Configuration
swiftmailer:
    transport: '%mailer_transport%'
    host: '%mailer_host%'
    username: '%mailer_user%'
    password: '%mailer_password%'
    port: '%mailer_port%'
    spool:
        type: file
        path: '%kernel.cache_dir%/swiftmailer/spool'

parameters.yml:

parameters:
    mailer_transport: sendmail
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    mailer_port: null

What's really frustrating is that if I create a message using bin/console swiftmailer:email:send, and then flush the spool (bin/console swiftmailer:spool:send) it is sent properly. It's only when I create and send a message through my controller that there's an issue.

What am I doing wrong?

like image 752
Major Productions Avatar asked Aug 02 '17 18:08

Major Productions


3 Answers

Ooof

It was a DNS error on my side that was causing the problem. Namely, that I forgot to point my MX records to Google's mail servers, so sendmail was taking the example.com portion of the destination address and trying to use it as a smtp relay, even though I didn't have a mail server set up.

Apologies for all the consternation. Hopefully my answer can be useful for others banging their heads against the wall.

like image 58
Major Productions Avatar answered Nov 06 '22 00:11

Major Productions


Why do you use the Sendmail Transport instead of SMTP Transport?

https://swiftmailer.symfony.com/docs/sending.html

Try this:

config.yml

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    port: "%mailer_port%"
    encryption: "%mailer_encryption%"
    spool:     { type: memory }

parameters.yml

parameters:
    mailer_transport: smtp
    mailer_host: smtp.office365.com
    mailer_user: [email protected]
    mailer_password: my_password
    mailer_port: 587
    mailer_encryption: tls

Controller

$message = \Swift_Message::newInstance()
            ->setSubject('Subject')
            ->setFrom(array('[email protected]' => 'My name'))
            ->setTo(array($user->getMail()))
            ->setBcc(array('[email protected]', '[email protected]'))
            ->setBody(
                $this->renderView(
                    'template.html.twig',
                    array('vars' => $vars)
                ),
                'text/html'
            );

$this->get('mailer')->send($message);
like image 5
JGrinon Avatar answered Nov 06 '22 01:11

JGrinon


I can suggest you to try this approach:

    $mailer = $container->get('mailer');
    $spool = $mailer->getTransport()->getSpool();
    $transport = $container->get('swiftmailer.transport.real');

    $sender     = 'your_sender';
    $recipient  = 'your_recipient';
    $title      = 'your_title';
    $body       = 'your_message';
    $charset    = "UTF-8";

    $email = $mailer->createMessage()
        ->setSubject($title)
        ->setFrom("$sender")
        ->setTo("$recipient")
        ->setCharset($charset)
        ->setContentType('text/html')
        ->setBody($body)
    ;

    $send = $mailer->send($email);
    $spool->flushQueue($transport);

You can wrap this into a send message of the simple YouMailService. Or you can insert this code into your controller. This will be enough.

like image 2
staskrak Avatar answered Nov 05 '22 23:11

staskrak