Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email via smtp with Ruby's mail gem?

Tags:

email

ruby

smtp

I am using the mail gem for Ruby https://github.com/mikel/mail

How do I send an email via an smtp server? How do I specify the address and port? And what settings should I use for Gmail?

The README on github only gives examples sending by a local server.

like image 249
Colonel Panic Avatar asked Oct 14 '12 17:10

Colonel Panic


People also ask

How do you send mail in Ruby?

To send the mail you use Net::SMTP to connect to the SMTP server on the local machine and then use the send_message method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these aren't always used to route mail).

How do I use SMTP in Ruby on Rails?

Go to the config folder of your emails project and open environment. rb file and add the following line at the bottom of this file. It tells ActionMailer that you want to use the SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.


Video Answer


1 Answers

From http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp

To send out via GMail, you need to configure the Mail::SMTP class to have the correct values, so to try this out, open up IRB and type the following:

require 'mail'  options = { :address              => "smtp.gmail.com",             :port                 => 587,             :domain               => 'your.host.name',             :user_name            => '<username>',             :password             => '<password>',             :authentication       => 'plain',             :enable_starttls_auto => true  }    Mail.defaults do   delivery_method :smtp, options end 

The last block calls Mail.defaults which allows us to set the global delivery method for all mail objects that get created from now on. Power user tip, you don’t have to use the global method, you can define the delivery_method directly on any individual Mail::Message object and have different delivery agents per email, this is useful if you are building an application that has multiple users with different servers handling their email.

Mail.deliver do        to '[email protected]'      from '[email protected]'   subject 'testing sendmail'      body 'testing sendmail' end 
like image 152
Simone Carletti Avatar answered Oct 11 '22 14:10

Simone Carletti