Featured image

Email exchange has remained quite popular for several decades now. A few days ago, I was configuring Postfix for email delivery, and to verify everything was working correctly, I wanted to send a test email using the mail CLI utility. Unfortunately, every message sent this way ended up in spam and was naturally ignored. In Ruby, you can use the standard net-smtp client to send emails, or rely on a higher-level mail gem (which is also built on top of net-smtp).

require 'net/smtp'

Net::SMTP.start('mail.example.com', 587, user:, password:) do |smtp|
  smtp.send_message <<~MAIL, 'artem@example.com', 'alex@example.com'
    From: Artem <artem@example.com>
    To: Alex <alex@example.com>
    Subject: Test message (Net::SMTP)

    This is a test message sended from Ruby.
  MAIL
end


require 'mail'

mail = Mail.new do
  from 'Artem <artem@example.com>'
  to 'Alex <alex@example.com>'
  subject 'Test message (Mail gem)'
  body <<~MSG
    This is a test message sended from Ruby.
  MSG
end
mail.delivery_method(
  :smtp, { address: 'mail.example.com', port: 587, user_name:, password: }
)
mail.deliver

How do you send emails from the terminal? What tools do you rely on? Or do you also send emails using the SMTP library in your favorite programming language?