Ruby Send Mail – SMTP

To send emails from a Ruby application, you can use the mail gem, which is a simple and flexible Ruby library. It uses SMTP (Simple Mail Transfer Protocol) by default, which is the standard protocol for email transmission over the Internet.

Here's a basic tutorial on how to send emails with the mail gem:

Step 1: Install the mail gem

First, you need to install the mail gem. You can do this by running the following command in your terminal:

gem install mail

Step 2: Setup SMTP settings

Next, you'll need to set up the SMTP settings for the mailer. This usually includes the address and port of the SMTP server, as well as authentication details.

Here's an example of how you might do this:

require 'mail'

options = { :address              => "smtp.gmail.com",
            :port                 => 587,
            :user_name            => 'your_username@gmail.com',
            :password             => 'your_password',
            :authentication       => 'plain',
            :enable_starttls_auto => true  }

Mail.defaults do
  delivery_method :smtp, options
end

Note: Make sure to replace 'your_username@gmail.com' and 'your_password' with your actual Gmail username and password. Also, remember that for Gmail, you might need to enable "Less Secure Apps" in your account settings in order for this to work.

Step 3: Compose and send the email

Now you can compose and send an email:

mail = Mail.new do
  from     'sender@example.com'
  to       'receiver@example.com'
  subject  'Hello'
  body     'Hello world!'
end

mail.deliver!

Note: Replace 'sender@example.com' and 'receiver@example.com' with the actual sender and receiver email addresses.

With the Mail.new block, you define the email's from address, to address, subject, and body. The mail.deliver! method is used to send the email.

Remember, this is a very basic example. The mail gem is quite flexible and allows you to do a lot more, including sending HTML emails, adding attachments, and more.

Also, keep in mind that storing sensitive information like usernames and passwords directly in your code is not a good practice. In a real-world application, you should consider using environment variables or some sort of secure configuration management system to handle sensitive data.

  1. Using Net::SMTP in Ruby for email: Send emails using the built-in Net::SMTP library.

    require 'net/smtp'
    
    message = <<MESSAGE_END
    From: Your Name <your_email@example.com>
    To: Destination <destination@example.com>
    Subject: Ruby Email Test
    
    This is a test email sent using Net::SMTP in Ruby.
    MESSAGE_END
    
    Net::SMTP.start('smtp.example.com', 25) do |smtp|
      smtp.send_message(message, 'your_email@example.com', 'destination@example.com')
    end
    
  2. Configuring SMTP settings in Ruby: Configure SMTP settings for sending emails.

    require 'net/smtp'
    
    smtp_settings = {
      address: 'smtp.example.com',
      port: 587,
      domain: 'example.com',
      user_name: 'your_username',
      password: 'your_password',
      authentication: 'plain',
      enable_starttls_auto: true
    }
    
    Net::SMTP.start(smtp_settings[:address], smtp_settings[:port]) do |smtp|
      smtp.send_message(message, 'your_email@example.com', 'destination@example.com')
    end
    
  3. Sending HTML emails in Ruby: Include HTML content in your email.

    message = <<MESSAGE_END
    Content-Type: text/html
    From: Your Name <your_email@example.com>
    To: Destination <destination@example.com>
    Subject: Ruby HTML Email Test
    
    <html>
    <body>
    <p>This is an HTML email sent from Ruby.</p>
    </body>
    </html>
    MESSAGE_END
    
  4. Attaching files to emails in Ruby: Attach files to your email using MIME.

    require 'mail'
    
    mail = Mail.new do
      from    'your_email@example.com'
      to      'destination@example.com'
      subject 'Ruby Email with Attachment'
    
      body 'This is the email body.'
    
      add_file '/path/to/attachment.pdf'
    end
    
    mail.delivery_method :smtp, address: 'smtp.example.com', port: 587
    mail.deliver
    
  5. Ruby email authentication with SMTP: Authenticate with SMTP using credentials.

    require 'net/smtp'
    
    smtp = Net::SMTP.new('smtp.example.com', 587)
    smtp.enable_starttls
    smtp.start('example.com', 'your_username', 'your_password', :plain) do |smtp|
      smtp.send_message(message, 'your_email@example.com', 'destination@example.com')
    end
    
  6. Ruby Action Mailer for sending emails: Use Rails' Action Mailer for more advanced email functionality.

    # In a Rails application
    class UserMailer < ApplicationMailer
      def welcome_email(user)
        @user = user
        mail(to: @user.email, subject: 'Welcome to My Site')
      end
    end
    
  7. Sending plain text emails in Ruby: Include plain text content in your emails.

    message = <<MESSAGE_END
    From: Your Name <your_email@example.com>
    To: Destination <destination@example.com>
    Subject: Ruby Plain Text Email Test
    
    This is a plain text email sent from Ruby.
    MESSAGE_END