使用 Action Mailer 发送电子邮件
Sent emails with Action Mailer
我正在尝试使用 Action Mailer(Ruby 在 Rails 上)向 example@gmail.com 发送电子邮件。正确执行方法 sendactivation 并显示消息 "Email sent"。但是,我从来没有收到任何电子邮件。实际上,输出 "Test" 永远不会被打印出来。我的网络应用托管在 Heroku Cedar-10 上。
class UsersController < ApplicationController
def sendactivation
UserMailer.welcome_email()
render :json => {
:result => "Email sent"
}
end
end
end
class UserMailer < ActionMailer::Base
def welcome_email()
mail(to: "example@gmail.com",
body: "Hello",
content_type: "text/html",
subject: "Already rendered!")
puts "Test"
end
end
这是我在 config/environment/production.rb 上的配置。其实我想用 Office 365 发送它,但我想用 Gmail 帐户调试更容易。
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
user_name: 'example@gmail.com',
password: '########',
authentication: 'plain',
enable_starttls_auto: true }
我做错了什么?我的 Gmail 配置是否需要更改?
答案:除了标记的答案外,我还需要在welcome_email方法中设置"from"地址。
UserMailer.welcome_email.deliver_now
来自 Rails Guide:参见第 2.1.4 节
class SendWeeklySummary
def run
User.find_each do |user|
UserMailer.weekly_summary(user).deliver_now
end
end
end
调用方法传递:
UserMailer.welcome_email.deliver
我正在尝试使用 Action Mailer(Ruby 在 Rails 上)向 example@gmail.com 发送电子邮件。正确执行方法 sendactivation 并显示消息 "Email sent"。但是,我从来没有收到任何电子邮件。实际上,输出 "Test" 永远不会被打印出来。我的网络应用托管在 Heroku Cedar-10 上。
class UsersController < ApplicationController
def sendactivation
UserMailer.welcome_email()
render :json => {
:result => "Email sent"
}
end
end
end
class UserMailer < ActionMailer::Base
def welcome_email()
mail(to: "example@gmail.com",
body: "Hello",
content_type: "text/html",
subject: "Already rendered!")
puts "Test"
end
end
这是我在 config/environment/production.rb 上的配置。其实我想用 Office 365 发送它,但我想用 Gmail 帐户调试更容易。
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
user_name: 'example@gmail.com',
password: '########',
authentication: 'plain',
enable_starttls_auto: true }
我做错了什么?我的 Gmail 配置是否需要更改?
答案:除了标记的答案外,我还需要在welcome_email方法中设置"from"地址。
UserMailer.welcome_email.deliver_now
来自 Rails Guide:参见第 2.1.4 节
class SendWeeklySummary
def run
User.find_each do |user|
UserMailer.weekly_summary(user).deliver_now
end
end
end
调用方法传递:
UserMailer.welcome_email.deliver