Ask Your Question
1

How can Ruby use gmail smtp to send emails?

asked 2023-06-04 05:24:03 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-04 05:42:02 +0000

lalupa gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-04 05:24:03 +0000

Seen: 18 times

Last updated: Jun 04 '23