如何使用 Phoenix 框架发送电子邮件

How to send emails with Phoenix framework

使用 Phoenix 框架发送电子邮件的最佳和最方便的方式是什么?

我发现的最佳方法是使用 mailman 包。为了发送测试电子邮件,我使用我的 gmail 帐户使用以下配置 mailman ->

def config do
  %Mailman.Context{
   config: %Mailman.SmtpConfig{ relay: "smtp.gmail.com",                 
                                port: 587,
                                username: "myusername@gmail.com",
                                password: "mypassword",
                                tls: :always },                                            
   composer: %Mailman.EexComposeConfig{}
  }
end

我使用的电子邮件内容如下:

def testing_email do
    %Mailman.Email{
      subject: "Hello Mailman!",
      from: "myusername@gmail.com",
      to: ["myotherusername@gmail.com"],          
      text: "Hello Mate",
      html: Phoenix.View.render_to_string(MyApp.PageView,"index.html", foo: "bar")
      }
end

然后你就做 ->

1) email = MyApp.Mailer.deliver testing_email

2) Task.await(email)

查看 Bamboo — 它是一个优秀的库,具有出色的文档,带有适用于 SendGrid 和 Mandrill 的适配器,并且具有在开发模式下预览本地发送的电子邮件的便捷方式。它与 Phoenix 配合得很好,也支持渲染模板。

尝试几次后,我肯定会选择https://github.com/swoosh/swoosh package. It has the best documentation,测试支持,浏览器中的邮箱预览和星数指向更多用户。