我在这里遵循本指南:https://launchschool.com/blog/handling-emails-in-rails为我的ruby-on-rails项目设置邮件。
此代码有效:example_mailer.rb
ExampleMailer.sample_email(@user).deliver
当我在控制器中拨打<!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>
时,我会收到一封电子邮件:“这封邮件是通过mailgun-ruby使用Mailgun API发送的”,因为它保存在:text。
现在,我想,这个html.erb或这个text.erb文件是发送的,而不只是:text:
sample_email.html.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 %>
sample_email.text.erb
text.erb-file
我需要做什么,所以发送这些文件,而不仅仅是:text?
sample_email.text.erb和sample_email.html.erb位于:app / views / example_mailer
文件example_mailer.rb位于:app / mailers / example_mailer.rb
提前致谢!
编辑:我删除了:文本行和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`
但现在,我收到了这个错误:
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,
}
我哪里出错了?谢谢!
Edit2:我的smtp设置:
testOnBorrow=true
答案 0 :(得分:1)
更改您的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