如何告诉 actionmailer 在 rails 中使用 html.erb 或 text.erb 文件格式

How to tell actionmailer to use html.erb or text.erb file format in rails

我在此处遵循此指南:https://launchschool.com/blog/handling-emails-in-rails 为我的 ruby-on-rails 项目设置邮件程序。

此代码有效:example_mailer.rb

class ExampleMailer < ApplicationMailer
  default from: "philotester5@gmail.com"

  def sample_email(user)
    @user = user


    mg_client = Mailgun::Client.new ENV['api_key']
    message_params = {:from    => ENV['gmail_username'],
                      :to      => @user.email,
                      :subject => 'Sample Mail using Mailgun API',
                      :text    => 'This mail is sent using Mailgun API via mailgun-ruby'}
    mg_client.send_message ENV['domain'], message_params

  end
end

当我在我的控制器中调用 ExampleMailer.sample_email(@user).deliver 时,我收到一封包含文本的电子邮件:'This mail is sent using Mailgun API via mailgun-ruby' 因为它保存在 :text.

现在,我想要发送这个 html.erb 或这个 text.erb 文件,而不仅仅是 :text:

sample_email.html.erb

<!DOCTYPE html>
<html>
<head>
  <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Hi <%= @user.username %></h1>
<p>
 Sie haben folgende Tags ausgewählt:
  <% @user.tag_list.each do |tag| %>
    <%= tag %> <br>
  <% end %>

  <br><br>
  <% @infosall.each do |info| %>
      <%= info.name %><br>
  <% end %><br>
</p>
</body>
</html>

sample_email.text.erb

Hi <%= @user.username %>

Sie haben folgende Tags ausgewählt:
<% @user.tag_list.each do |tag| %>
    <%= tag %>
<% end %>

<% @infosall.each do |info| %>
    <%= info.name %>
<% end %>

我需要做什么才能发送这些文件,而不仅仅是 :text?

sample_email.text.erb 和 sample_email.html.erb 位于:app/views/example_mailer

文件example_mailer.rb位于:app/mailers/example_mailer.rb

提前致谢!

编辑:我删除了 :text 行和 text.erb-file 现在我有这个:

mg_client = Mailgun::Client.new ENV['api_key']
message_params = {:from    => ENV['gmail_username'],
                  :to      => @user.email,
                  :subject => 'Sample Mail using Mailgun API'}
mg_client.send_message ENV['domain'], message_params

但是现在,我得到这个错误:

2016-02-14T13:28:12.483443+00:00 app[web.1]: ExampleMailer#sample_email: processed outbound mail in 150.3ms
2016-02-14T13:28:12.483718+00:00 app[web.1]: Completed 500 Internal Server Error in 495ms (ActiveRecord: 30.2ms)

2016-02-14T13:28:12.485057+00:00 app[web.1]: 
2016-02-14T13:28:12.485054+00:00 app[web.1]: Mailgun::CommunicationError (400 Bad Request):
2016-02-14T13:28:12.485055+00:00 app[web.1]:   app/mailers/example_mailer.rb:19:in `sample_email'
2016-02-14T13:28:12.485056+00:00 app[web.1]:   app/controllers/events_controller.rb:26:in `create'
2016-02-14T13:28:12.485057+00:00 app[web.1]: 

In example_mailer.rb:19:     `mg_client.send_message ENV['domain'], message_params`
In events_controller.rb:26: `ExampleMailer.sample_email(@user).deliver`

我哪里错了?谢谢!

Edit2:我的 smtp 设置:

  config.action_mailer.delivery_method = :smtp
  # SMTP settings for gmail
  ActionMailer::Base.smtp_settings = {
      :port           => 587,
      :address        => "smtp.mailgun.org",
      :domain         => ENV['domain'],
      :user_name      => ENV['username'],
      :password       => ENV['password'],
      :authentication => :plain,
  }

将您的 simple_email 函数更改为如下所示

def sample_email(user)
    @user = user
    mail(to: @user.email, subject: 'Sample Email')
end

你也可以像这样改变上面的函数来定义格式

def sample_email(user)
    @user = user
    mail(to: @user.email, subject: 'Sample Email') do |format|
        format.text
        # or
        format.html
    end
end