首页 > 解决方案 > Symfony 3->4:电子邮件发送功能不起作用

问题描述

我从 Symfony 3.4 更新到 4.0 并检查了操作。
当我检查发送电子邮件的功能时,我在flash中收到了“成功”的消息,但出现了电子邮件未发送的问题。
你有什么主意吗?
由于不支持自动接线,因此可以获得参数等。
它也适用于 Symfony3。

swiftmailer.yaml

swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    delivery_address:   "%mailer_delivery_address%"
    delivery_whitelist: "%mailer_delivery_whitelist%"

StaffController.php

                    $this->get("admin.staffService")->generateUrlKey($staff);
                    $this->get("admin.staffService")->save($staff);
                    $this->get("admin.staffService")->sendInitialSettingMail($staff);
                    $this->get('session')->getFlashBag()->add('success', 'I saved my staff and sent a staff profile initialization email.');

员工服务.php

    public function sendInitialSettingMail($staff)
    {
        $subject = sprintf(
            $this->container->getParameter('initial_setting_mail_subject'),
            $staff->isHq() ? 'App' : $staff->getShop()
        );
        $body = $this->container->get('templating')->render('@AhiSpAdminBundle/Mail/initialSetting.txt.twig', array(
            'staff' => $staff,
        ));
        $from = $this->container->getParameter('ahisp_mail_from');
        $to = $this->getSendMailAddr($staff);

        $mail =  $this->container->get('common.mailService')->createMail($subject, $body, $from, $to);
        $this->container->get('common.mailService')->sendMail($mail);
    }

邮件服务.php

    public function createMail($subject, $body, $from, $to, $cc = null, $bcc = null, $scheduledAt = null)
    {
        $mail = new Mail();
        $mail->setSubject($subject);
        $mail->setBody($body);
        $mail->setFrom(is_array($from) ? json_encode($from) : $from);
        $mail->setTo(is_array($to) ? json_encode($to) : $to);
        $mail->setCc(is_array($cc) ? json_encode($cc) : $cc);
        $mail->setBcc(is_array($bcc) ? json_encode($bcc) : $bcc);
        $mail->setScheduledAt($scheduledAt);
        return $mail;
    }
    public function sendMail(Mail $mail)
    {
        $from = json_decode($mail->getFrom());
        if ($from == null) {
            $from = $mail->getFrom();
        }

        $to = json_decode($mail->getTo());
        if ($to == null) {
            $to = $mail->getTo();
        }
        // Removed
        // $message = \Swift_Message::newInstance()
            //->setSubject($mail->getSubject())

        //Changed in Symfony4
        $message = (new \Swift_Message($mail->getSubject()))
            ->setBody($mail->getBody())
            ->setFrom($from)
            ->setReturnPath(is_array($from) ? $from[0] : $from)
            ->setTo($to)
            ->setCc($cc)
            ->setBcc($bcc);

        try {
            $this->container->get('mailer')->send($message);
        } catch(\Swift_TransportException $e) {
            $this->container->get('logger')->critical($e->getMessage());
        }
    }

服务.yaml

    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    mailer_delivery_address: at-devel@XXX.jp
    mailer_delivery_whitelist:
        - /^.+@XXX\.jp$/

邮件日志

Feb 22 14:55:58 localhost postfix/smtpd[2404]: connect from localhost[127.0.0.1]
Feb 22 14:55:58 localhost postfix/smtpd[2404]: 6E21D2406C0: client=localhost[127.0.0.1]
Feb 22 14:55:58 localhost postfix/cleanup[2407]: 6E21D2406C0: message-id=<8702ef7c0b641a16b9afe7c8d448a1ba@192.168.XX.XX>
Feb 22 14:55:58 localhost postfix/qmgr[1955]: 6E21D2406C0: from=<ahisp@at-test.centsys.jp>, size=1716, nrcpt=1 (queue active)
Feb 22 14:55:58 localhost postfix/smtpd[2404]: disconnect from localhost[127.0.0.1]
Feb 22 14:56:08 localhost postfix/smtp[2400]: 6E21D2406C0: to=<at-devel@XXX.jp>, relay=none, delay=10, delays=0.06/0/10/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=XXX.jp type=MX: Host not found, try again)

版本
symfony/swiftmailer-bundle: 3.3
swiftmailer/swiftmailer 6.1.3

标签: phpsymfonysymfony4

解决方案


推荐阅读