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 hidden or 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 "[email protected]" | |
to "[email protected]" | |
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 hidden or 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"] = "[email protected]" | |
email[:to] = "[email protected]" | |
email.subject = "The Windows Problem" | |
email.body = <<-EMAIL | |
Hi Microsoft, | |
Could you please deprecate Windows? | |
Sincerely, | |
Janko | |
This file contains hidden or 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 "[email protected]" | |
to "[email protected]" | |
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 hidden or 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: '[email protected]', | |
password: "jncdbxqvqmkpkgbk", | |
enable_ssl: true | |
end | |
puts Mail.find(count: 10, order: :desc).map(&:subject) |
This file contains hidden or 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 # => ["[email protected]"] | |
mail.to # => ["[email protected]"] | |
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 hidden or 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;[email protected] | |
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 hidden or 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