【问题标题】:Can I use Amazon's SES with Symfony2 and the Swiftmailer Bundle?我可以将 Amazon 的 SES 与 Symfony2 和 Swiftmailer Bundle 一起使用吗?
【发布时间】:2012-02-07 22:44:13
【问题描述】:

我在运行 64 位版本的 CentOS 的 Amazon ec2 上托管一个站点。

该网站有一个简单的联系我们表单,提交时需要向多个地址发送电子邮件(非常基本)。

有没有人使用过 Amazon 的 SES 与 Symfony2 和 Swiftmailer Bundle?如果是这样,您是否建议使用 SES 或更传统的电子邮件服务器来执行此类任务?

【问题讨论】:

  • 现在需要您管理退回邮件和投诉,您可以使用 AWS SES Monitor 捆绑包来执行此操作。它还提供了一些有用的命令来自动创建主题,以通过 AWS SNS 获取有关退回、投诉和交付的通知。捆绑包是 github.com/Aerendir/aws-ses-monitor-bundle 。希望这会有所帮助。

标签: amazon-ec2 symfony swiftmailer amazon-ses


【解决方案1】:

可以使用 swiftmailer 库附带的本机 SMTP 传输通过 SES 发送电子邮件。以下示例使用 4.2.2 版本进行了测试。

亚马逊 SES requires usage of TLS encryption.

Swift_SmtpTransport 传输类可以通过传递 tls 作为第三个构造函数参数来配置为使用 TLS 加密:

require_once './vendor/swiftmailer/swiftmailer/lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance(
        'email-smtp.us-east-1.amazonaws.com', 
        25, 
        'tls'
    )
    ->setUsername('AWS_ACCESS_KEY')
    ->setPassword('AWS_SECRET_KEY')
;

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
    ->setFrom(array('example@example.org'))
    ->setTo(array('example@example.org' => 'John Doe'))
    ->setBody('Here is the message itself')
;

// Send the message
$result = $mailer->send($message);

在 Symfony2 中,您可以配置 swiftmailer 服务以使用 TLS 加密:

# app/config/config.yml
swiftmailer:
    transport:  smtp
    host:       email-smtp.us-east-1.amazonaws.com
    username:   AWS_ACCESS_KEY
    password:   AWS_SECRET_KEY
    encryption: tls

直接从安装在 EC2 实例上的邮件服务器发送电子邮件不是很可靠,因为 EC2 IP 地址可能被列入黑名单。建议使用受信任的邮件服务器,因此使用 SES 似乎是个好主意。

【讨论】:

    【解决方案2】:

    我无法通过 Symfony2 通过 SES 发送邮件,因为我在 config.yml 中配置了 spool 选项

    我偶然发现的另一个问题是端口。端口 25 和 587 工作完美,但 465 让我超时。

    使用正确的 SMTP 服务器很重要,起初我使用的是 us-east-1(因为我从示例中复制了它),尽管我的 SMTP 实际上是 email-smtp.eu-west-1.amazonaws .com

    这是我当前的配置:

    parameters:
        mailer_transport: smtp
        mailer_host: email-smtp.eu-west-1.amazonaws.com
        mailer_user: AWS_ACCESS_KEY
        mailer_password: AWS_SECRET_KEY
        mailer_encryption: tls
        mailer_port: 587
    
    swiftmailer:
        transport: %mailer_transport%
        host:      %mailer_host%
        username:  %mailer_user%
        password:  %mailer_password%
        encryption: "%mailer_encryption%"
        port: %mailer_port%
        auth_mode:  login
    

    我通过在命令行上执行以下命令发现了问题:

    php app/console swiftmailer:debug
    

    【讨论】:

    • 看起来使用端口 465 会导致 [Swift_TransportException] Connection could not be established with host email-smtp.us-east-1.amazonaws.com [Connection timed out #110] 更改为端口 587 可以解决问题
    • 感谢587 把戏! 487 我也超时了
    • 谢谢!使用端口587 做到了:)
    • 我刚刚在将旧应用迁移到 ECS 时遇到了这个问题。知道为什么假脱机配置会导致问题吗?还有,谢谢!这也解决了我的问题。
    • @jonnysamps 什么是 ECS?
    【解决方案3】:

    有一个为 swiftmailer 预构建的 SES 传输。非常容易设置:

    https://github.com/jmhobbs/Swiftmailer-Transport--AWS-SES

    【讨论】:

      【解决方案4】:

      如果您可以坚持免费层级限制(每天 2K 条消息),我绝对建议您坚持使用 SES 而不是传统的电子邮件服务器。它简单、易于与大多数平台集成,并且您消除了电子邮件服务器的维护和运营成本(虽然很小,但仍然存在)。当然,使用 SES 时仍然存在数据传输成本,正如您在 Amazon SES pricing 上看到的那样,但这也可能符合您的需求。

      【讨论】:

        【解决方案5】:

        自 2011 年 12 月以来,您可以将 smtp 与 switfmail 一起使用,但在此之前问题是这个捆绑包仍然没有在 EC2 上工作的实现,但已经存在。如果您喜欢使用 switfmail 之类的框架发送电子邮件,您应该拥有密码和密钥,然后执行以下操作:

         require_once 'lib/swift_required.php';
        
          //Create the Transport
          $transport = new Swift_AWSTransport(
            'AWS_ACCESS_KEY',
            'AWS_SECRET_KEY'
          );
        
          //Create the Mailer using your created Transport
          $mailer = Swift_Mailer::newInstance($transport);
        
          //Create the message
          $message = Swift_Message::newInstance()
          ->setSubject("What up?")
          ->setFrom(array('you@yourdomain.com'))
          ->setTo(array('them@theirdomain.com'))
          ->setBody("
        

        要获取您的密钥,请进入 AWS 管理控制台”>“SMTP 设置”>“创建我的 SMTP 凭证”

        你需要安装这个扩展:

        https://github.com/jmhobbs/Swiftmailer-Transport--AWS-SES

        但我重申这只是信息。现在,您应该先在 AWS 管理控制台中验证您的电子邮件帐户,然后应该可以正常工作。

        【讨论】:

          【解决方案6】:

          在较新的 Symfony 版本中,包括对 SES 的支持。您可以简单地传递您的凭据并在配置中设置您的 stmp 主机。

          请参阅 Symfony 3.4Symfony 4.X 的文档

          【讨论】:

            【解决方案7】:

            只需添加“tls”作为第三个参数。工作正常 前任:

            // Create the Transport
            $transport = (new Swift_SmtpTransport('amazon-url', 587, 'tls'))
            ->setUsername('awsusernamexxxxxx')
            ->setPassword('awspasswordxxxxxx');
            // Other codings
            ?>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-12-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-09-08
              • 1970-01-01
              相关资源
              最近更新 更多