Skip to content

Instantly share code, notes, and snippets.

@janko
Created May 27, 2015 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janko/0f9979d3cf8de52b25cc to your computer and use it in GitHub Desktop.
Save janko/0f9979d3cf8de52b25cc to your computer and use it in GitHub Desktop.
Capabilities of the "mail" gem, without ActionMailer (my presentation from our local Ruby meetups)
require "mail"
Mail.defaults do
delivery_method :smtp,
address: "smtp.gmail.com",
port: 587,
user_name: ENV["GMAIL_EMAIL"],
password: ENV["GMAIL_PASSWORD"],
authentication: "plain",
end
Mail.deliver do
from "janko.marohnic@gmail.com"
to "windows@microsoft.com"
subject "Windows problem"
text_part do
body <<-EMAIL
Dear Microsoft,
Could you please deprecate Windows?
Sincerely,
Janko
EMAIL
end
html_part do
content_type "text/html; charset=UTF-8"
body <<-EMAIL
<p>Dear Microsoft,</p>
<p>Could you please deprecate Windows?</p>
<p>
Sincerely,<br>
Janko
</p>
EMAIL
end
end
require "mail"
email = Mail.new
email # => #<Mail::Message:70260121687560, Multipart: false, Headers: >
email["from"] = "janko.marohnic@gmail.com"
email[:to] = "windows@microsoft.com"
email.subject = "The Windows Problem"
email.body = <<-EMAIL
Hi Microsoft,
Could you please deprecate Windows?
Sincerely,
Janko
EMAIL
require "mail"
email = Mail.new do
from "janko.marohnic@gmail.com"
to "windows@microsoft.com"
subject "Windows problem"
body File.read("body.txt")
add_file "/path/to/file.jpg"
add_file filename: "image.png", content: content
end
require "mail"
Mail.defaults do
retriever_method :pop3,
address: "pop.gmail.com",
port: 995,
user_name: 'janko.marohnic@gmail.com',
password: "jncdbxqvqmkpkgbk",
enable_ssl: true
end
puts Mail.find(count: 10, order: :desc).map(&:subject)
require "mail"
mail = Mail.read("message.eml")
mail.from # => ["d.tomaszova@gmail.com"]
mail.to # => ["janko.marohnic@gmail.com"]
mail.subject # => "Povrat poreza"
mail.date.to_s # => "2015-01-28T22:32:28+01:00"
mail.message_id # => "CAL_xHx=By+-Pnp-36siGn630P5wi56Q0BiEcxKLS8fESrctk_A@mail.gmail.com"
mail.body.decoded # => "..."
require "mail"
mail = Mail.read('/path/to/bounce_message.eml')
mail.bounced? #=> true
mail.final_recipient #=> rfc822;mikel@dont.exist.com
mail.action #=> failed
mail.error_status #=> 5.5.0
mail.diagnostic_code #=> smtp;550 Requested action not taken: mailbox unavailable
mail.retryable? #=> false
require "mail"
Mail.defaults do
delivery_method :test
end
Mail::TestMailer.deliveries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment