首页 > 解决方案 > Symfony SymfMail - 如何发送电子邮件

问题描述

我想用 symfony symfmail 发送一封电子邮件,但是有问题。我的代码没有抛出错误,但它没有发送任何内容。更糟糕的是,它没有显示我与 smtp 的连接是否正确或任何错误。该怎么办?

这是我的控制器方法:

 /**
 * @Route("/mail")
 */
public function mail(\Swift_Mailer $mailer){
    $message = (new Swift_Message('Wonderful Subject'))
        ->setFrom(['I SET IT!' => 'John Doe'])
        ->setTo(['I SET IT!', 'I SET IT!' => 'A name'])
        ->setBody('Here is the message itself')
    ;

    $result = $mailer->send($message);
    return new Response(var_dump($result));
}

我不知道。也许我只是没有配置好,或者 smtp 没有从本地主机中的 wamp64 配置。

请帮忙。

更新:

当我查看工具栏错误时,我得到了这个代码。是因为我从本地主机发送它吗?

Exception occurred while flushing email queue: Connection could not be established with host localhost [No connection could be made because the target machine actively refused it. #10061]

我的 .env 文件

    # This file is a "template" of which env vars need to be defined for your application
# Copy this file to .env file for development, create environment variables when deploying to production
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=e772aaf55e2eb077062f1d98468e6f59
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
#TRUSTED_HOSTS=localhost,example.com
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=mysql://root:@127.0.0.1:3306/cms
###< doctrine/doctrine-bundle ###

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=smtp://MY_MAIL_WITHOUT_AT_CHAR_AND_DOMAIN:MY_PASSWORD@EMAILDOMAIN:SSL_PORT
#MAILER_URL=smtp://localhost:25?encryption=ssl&auth_mode=login&username=&password=
#MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###

更新:好的,我知道我尝试从控制台发送它的问题在哪里,这就是我得到的:

19:40:53 ERROR     [app] Exception occurred while flushing email queue: 
Connection could not be established with host localhost [No connection could be made because the target machin
e actively refused it.
 #10061]

标签: phpsymfonyemail

解决方案


我还没有这样做,但我发现从本地主机发送电子邮件有问题,因为即使使用 mail() 函数它也不起作用,但在我的网络服务器上它确实起作用。感谢帮助。


推荐阅读