Created
May 27, 2015 22:54
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
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> | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 # => "..." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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