I switched our email service provider to Google last week. So I have to figure out how to send out emails with GMail. I started with the default Net::SMTP settings, but it failed miserable:
~ 530 5.7.0 Must issue a STARTTLS command first. 7sm1765038qwf.45
After playing a while and googled a bit, here is the solution:
1. In your dependencies.rb, add following line:
dependency "tlsmail"
2. Install the gem
bin/thor merb:gem:install
3. Modify your init.rb
4. Over write the mailer
Note: the "Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)"
5. Sending email
Note: use :text instead of :body
Now, you are good to go.
~ 530 5.7.0 Must issue a STARTTLS command first. 7sm1765038qwf.45
After playing a while and googled a bit, here is the solution:
1. In your dependencies.rb, add following line:
dependency "tlsmail"
2. Install the gem
bin/thor merb:gem:install
3. Modify your init.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Merb::BootLoader.after_app_loads do | |
# for the mailer - gmail | |
Merb::Mailer.config = { | |
:host => 'smtp.gmail.com', | |
:port => '587', | |
:user => 'your_name@your_domain', | |
:pass => 'your_pass', | |
:auth => :plain, # :plain, ;login, :cram_md5 | |
:domain => "your_domain" | |
} | |
end |
4. Over write the mailer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mailer | |
# Sends the mail using SMTP. | |
def net_smtp | |
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE) | |
Net::SMTP.start(config[:host], config[:port].to_i, config[:domain], | |
config[:user], config[:pass], config[:auth]) { |smtp| | |
smtp.send_message..... | |
} | |
end | |
end | |
Note: the "Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)"
5. Sending email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def send_email() | |
begin | |
mailer = Merb::Mailer.new( | |
:from => 'your from', | |
# hack: Net::SMTP.send_message does not send msg to cc | |
:to => 'your_to list', | |
:subject => 'your subject', | |
:text => 'content') | |
mailer.deliver! | |
rescue => exception | |
Merb.logger.error(exception) | |
end | |
end | |
Note: use :text instead of :body
Now, you are good to go.