Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use Gmail SMTP to send emails in Ruby, you can use the following steps:

  1. Install the gem 'mail' by running the following command in your terminal:

    gem install mail
    
  2. Create a new Ruby file and require the 'mail' gem:

    require 'mail'
    
  3. 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
    }
    
  4. 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
    
  5. 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.