All Products
Search
Document Center

Direct Mail:SMTP - Ruby

Last Updated:Dec 11, 2024

The following example shows how to send an email through SMTP using Ruby.

# Install mail gem first, command: gem install mail

require 'mail'

# Configure the default settings for sending mail
Mail.defaults do
  delivery_method :smtp, {
    :address   => "smtpdm.aliyun.com",  # SMTP server address
    :user_name => "from@example.com",    # Your email address for authentication
    :port      => 465,                   # Port to connect to the SMTP server (465 for SSL)
    :ssl       => true,                  # Enable SSL for secure connection
    # :enable_starttls_auto => false,    # No need for STARTTLS with SSL on port 465
    # :openssl_verify_mode => 'none',     # Change this depending on your verification needs
  }
end

# Create and send the email
mail = Mail.deliver do
  to      'to@example.com'              # Recipient's email address
  from    'from@example.com'            # Sender's email address
  subject 'Hello'                       # Subject of the email
  reply_to  'reply_to@example.com'      # Reply-to address

  text_part do                           # Define the text part of the email
    body 'Testing mail'                 # Body content of the email
  end
end

# Output a success message after sending the email
puts "Email sent successfully!"