Php 如何运行电子邮件功能?

Php 如何运行电子邮件功能?,php,symfony,symfony4,Php,Symfony,Symfony4,我只是从中复制了代码,并做了一些更改 我想运行这个函数,但我不明白的是,第二个参数是什么?我正在我的服务类中使用此函数 public function email($name, \Swift_Mailer $mailer) { $message = (new \Swift_Message('Hello Email')) ->setFrom('send@example.com') ->setTo('my.email@example.com')

我只是从中复制了代码,并做了一些更改

我想运行这个函数,但我不明白的是,第二个参数是什么?我正在我的服务类中使用此函数

public function email($name, \Swift_Mailer $mailer)
{
    $message = (new \Swift_Message('Hello Email'))
        ->setFrom('send@example.com')
        ->setTo('my.email@example.com')
        ->setBody(
            $this->renderView(
            // templates/emails/registration.html.twig
                ':emails:task.twig',
                ['name' => $name]
            ),
            'text/html'
        )
    ;

    $mailer->send($message);

    return $this->render(':emails:task.twig');
}

如果您的意思是\Swift\u从DI容器邮寄其服务。例如,您必须从具有DI的构造函数中获取它。只需请求\Swift\u Mailer并将实例作为第二个参数。链接的代码看起来像位于控制器内。在服务中,您很可能会将邮件程序添加到构造函数中。
// class or controller registered in DI container
class InvoiceMailer
{
    private $mailer;

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

    private function email($name, \Swift_Mailer $mailer)
    {
        $message = (new \Swift_Message('Hello Email'))
            ->setFrom('send@example.com')
            ->setTo('my.email@example.com')
            ->setBody(
                $this->renderView(
                // templates/emails/registration.html.twig
                    ':emails:task.twig',
                    ['name' => $name]
                ),
                'text/html'
            );

         $mailer->send($message);
    }

    public function SendEmail($name)
    {
        $this->email($name, $this->mailer);
    }
}