Skip to content

3 ways to send emails with only few lines of code and Gmail - Ruby - Part 2

Posted on:June 21, 2021

We will see how to send a simple email with the help of three different programming languages: Javascript, Ruby and Python Before you start you need to create a Gmail account. Do not forget to accept and allow the “Less secure apps” access in order use your scripts with your Gmail smtp connection. I’ll let you do this on your own, you don’t need a tutorial for this 😜

Ruby 💎

gem install mail
require 'mail'
# Gmail account info
options = {
  address: 'smtp.gmail.com',
  port: 587, # gmail smtp port number
  domain: 'gmail.com',
  user_name: 'youremail@gmail.com',
  password: 'yourpassword',
  authentication: 'plain'
}
Mail.defaults do
  delivery_method :smtp, options
end
Mail.deliver do
  to 'myfriend@yopmail.com'
  from 'youremail@gmail.com'
  subject 'Sending email using Ruby'
  body 'Easy peasy lemon squeezy'
end

Here the final code:

require 'mail'

# Gmail account info
options = {
  address: 'smtp.gmail.com',
  port: 587,
  domain: 'gmail.com',
  user_name: 'youremail@gmail.com',
  password: 'yourpassword',
  authentication: 'plain'
}

Mail.defaults do
  delivery_method :smtp, options
end

Mail.deliver do
  to 'myfriend@yopmail.com'
  from 'youremail@gmail.com'
  subject 'Sending email using Ruby'
  body 'Easy peasy lemon squeezy'
end

puts 'Email sent'

The ease of Ruby 💎

Easy Ruby