sendmail
这是我使用的辅助方法,它使用 Pony在我的 Mac 上开发时使用或在生产时使用sendgrid
on发送电子邮件Heroku
。这工作可靠,我所有的测试电子邮件都会发送到我的各种 gmail 地址。
您的问题可能是您的from
地址无效,而 Google 将其标记为垃圾邮件。另外我注意到你没有设置Content-Type
标题,这通常text/html
是我的情况。
def send_email(a_to_address, a_from_address , a_subject, a_type, a_message)
begin
case settings.environment
when :development # assumed to be on your local machine
Pony.mail :to => a_to_address, :via =>:sendmail,
:from => a_from_address, :subject => a_subject,
:headers => { 'Content-Type' => a_type }, :body => a_message
when :production # assumed to be Heroku
Pony.mail :to => a_to_address, :from => a_from_address, :subject => a_subject,
:headers => { 'Content-Type' => a_type }, :body => a_message, :via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => 25,
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN'] }
when :test
# don't send any email but log a message instead.
logger.debug "TESTING: Email would now be sent to #{to} from #{from} with subject #{subject}."
end
rescue StandardError => error
logger.error "Error sending email: #{error.message}"
end
end