Rails 利用Gmail在Local发送测试邮件

本文介绍了两种通过Gmail账户配置应用程序以发送邮件的方法。第一种方法是通过修改配置文件设置SMTP参数;第二种方法则利用了Gmail Sender gem简化配置过程。

方法一:

 

config.action_mailer.raise_delivery_errors = true

 

ActionMailer::Base.delivery_method = :smtp

 

ActionMailer::Base.smtp_settings = {

  :address        => "smtp.gmail.com",

  :port           => 587,

  :domain => 'gmail.com', 

  :user_name      => "username",    # 不需要加@gmail.com

  :password       => '',          # 使用单引号

  :authentication => :plain,

  :enable_starttls_auto => true       

}

 

 

出错:Net::SMTPAuthenticationError (530 5.7.0 Must issue a STARTTLS command first. 23sm1922646pzk.0 


解决:

需要安装smtp-tls gem

sudo gem install ambethia-smtp-tls -v '1.1.2' --source http://gems.github.com 

 

 

方法二:

 

I found a simpler way to send a mail through gmail. No configuration required.

step 1: gem install gmail_sender
step 2: in your app create a method in any controller and write:

require 'rubygems'
  require 'gmail_sender' (if you are using rby version 1.8.6, then also install tlsmail)
def sendGmail

g = GmailSender.new("gmail_account_user_name", "gmail_account_password")
  g.attach('/path/to/document.hz') # you can attach any number of files, but there are limits for total attachments size
  g.send("someone@domain.com", "The subject", "The mail body")
end


And that's it!!!! Calll the sendGmail method and you are done.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值