簡體   English   中英

Symfony 3.3和Swiftmailer - 由服務器延遲的控制器創建和發送的郵件

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

我正在嘗試使用Swiftmailer從網站發送電子郵件。 由於Swiftmailer嘗試使用我服務器的IP地址而不是localhost作為中繼,因此電子郵件不斷延遲:

Aug  2 14:18:28 picus sm-mta[21171]: v72IIS0I021171: from=<Test@test.com>, size=347, class=0, nrcpts=1, msgid=<91d4a1a70de9fed0a2c04e682e435405@swift.generated>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Aug  2 14:18:28 picus sm-mta[21173]: v72IIS0I021171: to=<person@gmail.com>, 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.

我的Symfony控制器代碼,配​​置和參數 -

相關控制器代碼:

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('person@gmail.com')
        ->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

真正令人沮喪的是,如果我使用bin/console swiftmailer:email:send創建一條消息bin/console swiftmailer:email:send ,然后刷新假脫機( bin/console swiftmailer:spool:send ),它會被正確發送。 只有當我通過我的控制器創建並發送消息時,才會出現問題。

我究竟做錯了什么?

Ooof

我身邊的DNS錯誤導致了這個問題。 也就是說,我忘記將我的MX記錄指向Google的郵件服務器,因此sendmail正在獲取目標地址的example.com部分,並嘗試將其用作smtp中繼,即使我沒有設置郵件服務器。

為所有驚愕道歉。 希望我的答案對於其他人撞牆而言是有用的。

為什么使用Sendmail傳輸而不是SMTP傳輸?

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

嘗試這個:

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: user@example.com
    mailer_password: my_password
    mailer_port: 587
    mailer_encryption: tls

調節器

$message = \Swift_Message::newInstance()
            ->setSubject('Subject')
            ->setFrom(array('user@example.com' => 'My name'))
            ->setTo(array($user->getMail()))
            ->setBcc(array('copy1@example.com', 'copy2@example.com'))
            ->setBody(
                $this->renderView(
                    'template.html.twig',
                    array('vars' => $vars)
                ),
                'text/html'
            );

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

我建議你嘗試這種方法:

    $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);

您可以將其包裝到簡單YouMailService的發送消息中。 或者您可以將此代碼插入控制器。 這就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM