Symfony SymfMail-如何发送电子邮件

时间:2018-10-17 15:49:43

标签: php symfony email

我想发送带有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));
}

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

请提供帮助。

更新:

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

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]

2 个答案:

答案 0 :(得分:0)

您的.env配置中有问题!

MAILER_URL=smtp://MY_MAIL_WITHOUT_AT_CHAR_AND_DOMAIN:MY_PASSWORD@EMAILDOMAIN:SSL_PORT

请进行更改,并提供您的电子邮件凭据和配置,以便您可以随其发送电子邮件。

我不是Windows用户,但是根据您的环境,如果您有任何邮件服务器,则会在本地主机(如Web服务器)上对其进行配置。因此只需找到其监听端口即可。

答案 1 :(得分:0)

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