We send out many emails through Rails (and then through Amazon SES). I'd like to now log all of those emails in our database so we know exactly which emails were sent where and all the details. I'd also love to know which class/method sent those emails or some way to tag them.
This must be an extremely common need, so I'm curious if there's a mechanism with Rails to automatically handle this?
mail-logger gem captures the info about the emails sent, and log them to a file.
With a little bit of work it could be customized to write to a database instead.
The gem is overriding delivered_email method to log the details:
class Mail::Logger::Callback
def self.delivered_email(email)
Mail::Logger.logger.info email.inspect
end
end
Related
In my Rails 4 application I already store all outgoing emails in DB using an observer. In config/initializers/all_emails_observer.rb I have roughly the following code:
class AllEmailObserver
def self.delivered_email(message)
sent_email = SentEmail.new(
sender: message.from.join(';'),
recipients: message.to,
subject: message.subject
)
sent_email.body = if message.html_part || message.text_part
message.html_part.blank? ? message.text_part.body.raw_source : message.html_part.body.raw_source
else
message.body.raw_source
end
sent_email.save!
end
end
ActionMailer::Base.register_observer(AllEmailObserver)
Now I need to add another feature to let admins re-send any of the emails from the email log.
There are a couple of solutions that I could see right off hand:
Add code to store all attachments (regular and inline), then implement a method that would generate a new email and re-send it. I'm working on this now.
Store the mailer class, mailer method and arguments (serialized using GlobalId, for example). When the admin needs to re-send an email - generate a new email from scratch. I don't like this method because ideally I want re-sent emails to have exactly the same content (including attachments) as when they were originally sent. Mailers could have bugs/typos fixed since, but I do want all those bugs and typos to be still there when an email is re-sent.
Now that Rails 4.2 is released and ActiveJob is stable, I didn't find the standard way to do this: all queued emails are immediately deleted after they are sent (I just checked delayed_job backend). There must be a way to maybe preserve a completed mailer job in some separate DB table and then re-queue that job when needed.
I'd love to re-use the email serialization mechanism of ActiveJob to achieve my goal if possible, because I don't want to re-write my observer when Rails 6 is released with a completely changed observer API.
What is the better way to enable re-sending of sent emails?
Please note that I already reviewed similar questions here:
Save each email before sending rails 4
How do I create a Mailer Observer
However both of them are about how to store sent emails and not about how to re-send them later as is.
Thank you!
Alex.
I am using the Postmark gem for rails and writing Rspecs tests to make sure the emails are actually being sent when the form is valid.
I want to check that ActionMailer::Base.deliveries.last.to matches what I put in the contact form I'd like to test.
However, ActionMailer::Base.deliveries is always an empty array. Even in production, after an email is successfully sent (and received on the other end), the array is still empty when observed with rails c production.
What am I missing? How can I verify that the email has been sent in my tests?
ActionMailer::Base.deliveries array is a feature of :test delivery mode provided by ActionMailer (docs). If you’ve enabled Postmark gem in your test environment, that array will always be empty.
Unless you’re aiming for full integration testing, you should avoid using external services in your tests and stick to the :test delivery method. If you do that, you might also want to take a look at the email-spec gem, which provides a DSL for testing emails sent with ActionMailer.
In case you actually want to send emails in your tests, you can use Postmark Messages Retrieval API to retrieve a sent message via recipient or message ID and verify its contents. This allows for complete black-box testing if this is what you’re looking for.
I'm currently creating an email app that is able to send emails to many users. However, I want to know whether there are bounced emails. I'm currently using Amazon SES to notify me if the email is bounced. However, I want the bounced email's data to be automatically entered into my Rails application instead of typing it manually based to the mailer daemons I get from Amazon. Is there are way to do so?
If you are willing to pay, this SaaS site called bouncely seems to provide an interface and an api wrapper around SES bounces.
send_email() returns a response object which can be interrogated for the response metadata.
Compare the status of this with the code you are interested in, perhaps a 550.
I couldn't find any clean existing solution for this, so I wrote a gem (email_events) that allows you to put a email event handler method (including for bounce events) right in your mailer class: https://github.com/85x14/email_events. It supports SES and Sendgrid, so should work for you if you still need it.
I wanna log the emails which are being sent from a particular method of the ActionMailer. My ActionMailer consists of many methods which sends email for different reasons, but I to log the emails which are being sent from a particular method and save them in the database.
ActionMailer doesnt allow to do the database operations.
I am newbie to rails ENV,
kindly help me out
You need create your own Delivering system. In this Delivery, you can save your email in your Database and deliver by SMTP or something else.
This method is really not a newbie system. You can create your Delivery like the Mail::SMTP (https://github.com/mikel/mail/blob/master/lib/mail/network/delivery_methods/smtp.rb ) class managing the Delivery system by smtp.
I am creating a ruby on rails application and I want my users to be able to email the application and have the application take in the email and create a issue record in the database. Is there a GEM or Code that can be used to have the rails application get the email and parse out the body and stick it into an issue table.
I don't know if there is a gem to accomplish the entire task, but you don't technically need one. I recently did this, and though working with ruby's IMAP library isn't the most fun/intuitive thing in the world, it gets the job done.
IMAP can be used to programmatically access and interact with an email account. Here's an example straight from my code (somwhat obfuscated to be easier for someone to implement):
require 'net/imap'
imap = Net::IMAP.new("imap.gmail.com", 993, true)
imap.login(CONFIG["username"], CONFIG["password"])
imap.select('INBOX')
imap.search(["NOT", "DELETED"]).each do |mail_id|
mail = TMail::Mail.parse(imap.fetch(mail_id, "RFC822").first.attr["RFC822"])
do_something_cool(mail)
imap.store(mail_id, "+FLAGS", :Deleted)
end
imap.expunge
imap.logout()
imap.disconnect()
In this example, I'm accessing a gmail account with the IMAP library, going to the inbox, and grabbing each mail that hasn't been deleted. The TMAIL gem, though not necessary, makes dealing with email much easier. In my case, I need to delete emails after I parse them, so I add a delete flag to the email and then, when I'm done, I clear the account of all deleted emails.
The next half is parsing the email for the data you want and making records out of it. I leave that part to the implementor.