如何通过Swiftmailer发送邮件?

问题描述 投票:-2回答:2

我尝试通过Symfony Swiftmailer发送邮件,但没有收到任何邮件。

<?php

namespace App\Service;
use Swiftmailer\Swiftmailer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
* Mail Generator
*
*/


class MailGenerator extends AbstractController{


  public function __construct(\Swift_Mailer $mailer)
  {
    $this->mailer = $mailer;
  }


  public function sendMail()
  {

    $message = (new \Swift_Message('Hello Email'))
    ->setFrom('[email protected]')
    ->setTo('[email protected]')
       ->setBody(
           $this->renderView(
               'mail/mail.html.twig',
               ['name' => 'test']
           )
       )
   ;

    $this->mailer->send($message);
    return $this->render('mail/mail.html.twig');
  }

}

但我没有收到任何邮件。我错过了什么?

php symfony swiftmailer
2个回答
1
投票

我使用这个Mailer Class服务来发送邮件(使用Google账户)你可以编辑以下配置文件来使用你自己的smtp配置。

configservices.yaml

parameters:
    mailer:
        smtp_host: 'smtp.gmail.com'
        smtp_port: 587
        smtp_cert: 'tls'
        smtp_username: '[email protected]'
        smtp_password: 'mypassword'

services
    mybase.mailer.services:
        class: App\Services\Mailer
        arguments: ['%mailer%']
        public: true

srcServicesMailer.php

<?php

/**
 * Service that can be used to send email using basic swift mailer transport
 */

namespace App\Services;

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class Mailer
{
    /**
    * @var array $emailConfig
    */
    private $emailConfig;

    /**
     * Mailer constructor
     * 
     * @param ParameterBagInterface $params
     */
    public function __construct(ParameterBagInterface $params)
    {
        $this->emailConfig = $params->get('mailer');
    }

    public function sendMail(array $mailInfo, string $html, array $files = [])
    {
        $emailConfig = $this->emailConfig;

        $smtpHost   = $emailConfig['smtp_host'];
        $smtpPort   = $emailConfig['smtp_port'];
        $smtpCert   = $emailConfig['smtp_cert'];
        $smtpUsername   = $emailConfig['smtp_username'];
        $smtpPassword   = $emailConfig['smtp_password'];

        $transport = (new \Swift_SmtpTransport($smtpHost, $smtpPort, $smtpCert))
            ->setUsername($smtpUsername)
            ->setPassword($smtpPassword)
        ;

        $swiftMailer = new \Swift_Mailer($transport);

        $message = (new \Swift_Message($mailInfo['title']))
            ->setFrom([$smtpUsername => $mailInfo['senderName']])
            ->setTo($mailInfo['sendTo'])
            ->setBody($html, 'text/html');

        foreach ($files as $file) {
            $message->attach(\Swift_Attachment::fromPath($file));
        }

        $swiftMailer->send($message);
    }
}
  • 用例

在另一个服务内

<?php

namespace App\Services;

use App\Services\Mailer;
use Symfony\Component\DependencyInjection\ContainerInterface;

class MyOtherService
{
    /**
     * @var \Mailer $mailer;
     */
    private $mailer;

    /**
     * @var \Twig\Environment $templating;
     */
    private $templating;

    /**
     * @var ContainerInterface $container;
     */
    private $container;

    public function __construct(Mailer $mailer, \Twig\Environment $templating, ContainerInterface $container)
    {
        $this->mailer       = $mailer;
        $this->templating   = $templating;
        $this->container    = $container;
    }

    public function methodName()
    {
        // Logic ....

        $params = [
            "sendTo"    => '[email protected]',
            "title"     => "This is my awesome title",
            "senderName"=> 'My company name',
        ];

        $html = $this->templating->render("path/to-my-email/email-template.html.twig", [
            'some_variable' => 'SOME VARIABLES',
        ]);

        $this->mailer->sendMail($params, $html);
    }
}

或来自控制器

public function sendClientEmail(Request $request, Mailer $mailer): Response
    {
        // My code logics

        $params = [
            "sendTo"    => '[email protected]',
            "title"     => "This is my awesome title",
            "senderName"=> 'My company name',
        ];

        $html = $this->render("path/to-my-email/email-template.html.twig", [
            'some_variable' => 'SOME VARIABLES',
        ])->getContent();

        $mailer->sendMail($params, $html);

        // Rest of my code logics
    }

1
投票

测试在控制台发送邮件。

php bin/console swiftmailer:email:send [email protected] [email protected] --subject=test --body=test

结果会是什么?

© www.soinside.com 2019 - 2024. All rights reserved.