DEV Community

Cover image for 3 ways to send emails with only few lines of code and Gmail - Ruby - Part 2
François
François

Posted on • Updated on

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

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 💎

  • We are going to use the Mail gem:
gem install mail
Enter fullscreen mode Exit fullscreen mode
  • Require it at the beginning of your send_email.rb:
require 'mail'
Enter fullscreen mode Exit fullscreen mode
  • Declare Gmail account info:
# 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'
}
Enter fullscreen mode Exit fullscreen mode
  • Initialize Mail delivery_method info:
Mail.defaults do
  delivery_method :smtp, options
end
Enter fullscreen mode Exit fullscreen mode
  • Send email 📧 :
Mail.deliver do
  to 'myfriend@yopmail.com'
  from 'youremail@gmail.com'
  subject 'Sending email using Ruby'
  body 'Easy peasy lemon squeezy'
end
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

The ease of Ruby 💎

Easy Ruby

Table of contents

Top comments (0)