SMTP 之 Ruby 调用示例

使用 Ruby 通过 SMTP 协议发信

# 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!"