1 | initial version |
To use Gmail SMTP to send emails in Ruby, you can use the following steps:
Install the gem 'mail' by running the following command in your terminal:
gem install mail
Create a new Ruby file and require the 'mail' gem:
require 'mail'
Set up your Gmail account credentials:
options = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "your_username@gmail.com",
:password => "your_password",
:authentication => "plain",
:enable_starttls_auto => true
}
Use the 'mail' gem to create a new email:
Mail.defaults do
delivery_method :smtp, options
end
email = Mail.new do
from 'your_username@gmail.com'
to 'recipient@example.com'
subject 'This is a test email'
body 'Hello World!'
end
Send the email using the 'deliver' method:
email.deliver
And that's it! Your code should be able to send an email using Gmail SMTP.