rails 2 actionmailer email address alias - ruby-on-rails

I am working with rails 2 . I wanted to know is there a way in ActionMailer to hide/alias the email address??
Also what should be the best approach for sending bulk emails???
Thanks in advance.

Mailcar
Mailcar 0.1
Mailcar is a dead-simple (ie. not many features) bulk emailer for Ruby on Rails, using ActiveRecord
and ActionMailer.
If you need to send emails to your entire user base and don't want to pay the high prices that most
mass email services charge, then this is the plugin for you!
WARNING!!! Using this without having your SMTP server set up correctly could land your server on
hundreds of blacklists. If that prospects scares you, look at the pay-to-send bulk emailers. It's
expensive, but at least you know you're safe.
WARNING!!! As is, this script is tied tightly to the codebase I wrote it for. It will not work out
of the box. I'm posting it in the hopes that someone else needs something similar and doesn't want to
start from scratch. Please send me patches or pull requests with your upgrades and I'll include them
and add you to the credits.
Installation
To copy the models and create the migration, run:
script/generate mailcar all
Then execute the migration:
rake db:migrate
Example
To send a new bulk email, you must first create the message body file. I use Thunderbird to create an
HTML email, then save it out as HTML.
At that point, you can create the message and send it as follows:
rake mailcar:new_message FROM='me#mydomain.com' SUBJECT='New features on the site' BODY_FILE=/path/to/email/body
rake mailcar:prep_for_send MESSAGE_ID=99
rake mailcar:send MESSAGE_ID=99
If the sending process is ever interrupted, you can resume it with another call to send.
TODO
Add a test suite (wish I knew more about plugin testing...)
Make it easy to give a block (or something) to generate the list of email addresses
Pass emails through ERB to allow for templating / dynamic emails
Add support for multipart emails
Add a cleanup task to remove old messages
Make sending delay configurable

Related

General Guidance on Rails Mailers

I am making an event registration tool in Rails and I am having trouble working out the mailing section. I am using Mailgun API and I've got a generic "Thank you for Registering" email working when the user signs up as well as a contact form submission that comes to my email. Part of the requirements for the application is the ability to send promotional emails (separate from Thank you for Registering emails). These promotional emails are more like (One week reminder) type emails.
So these emails need to be able to be created by the admin setting up the event as this is a general purpose tool. So to save the emails the admin creates, I have a mailings object. So the relationship is a bit like this:
Event has many mailers, registrations, etc. (and those belong to the event). They are nested resources because they are specific to an event. Now I need to bridge the gap of how to go from the mailers created by the admin to sending them to Mailgun. The problem is we will have to have the ability to add recipients because they may want to send to people besides the registrants for the event. So I need to go from the mailing#show (which shows a preview of the mailing and will need to be able to add/remove recipients), loop through all of the recipients, and send the message that is in the mailing.message field.
I am so close to finishing this tool except for this mailing which I cannot wrap my head around. I see a lot of examples that create a mailer but I am not sure if that would work for me since the message are unique and it needs to get the message and subject from the mailer object. Any advice or guidance? I am really struggling to get this part done.
I assume you have a User model with an email column
I would setup an extra model i.e.
class PromoMail < ActiveRecord::Base
has_many :users # recpients
validates :body, presence: true
end
Then add a controller, where admins can create these and insert the Mail content in the body field and add recipients.
Then create a new method in your existing mailer to send the mail with the yielded body to one user.
Then add a action to the forementioned controller to send the PromoMail by looping over the associated users and call the ne mailer metod with each.
Couple of steps here, without going into much detail.
1) Make a rake task which looks for any emails which needs to be sent out, and sends them out. You might need to expand your schema to record whether a mail (or mailing or whatever) has been sent already. The rake task itself shouldn't have much code, it should just call a class method in eg User or Mailing or something.
You'll need to think about how the system can decide which emails need to get sent out. I find that flowcharts can be helpful here: it's going to involve iterating over all users, or all mailings, or something, and applying various logical tests to them. You may find that your current schema isn't up to the job, in which case expand it.
2) Schedule this rake task to be run at regular intervals, eg once a day or once a week. Various scheduled task runners are available, eg cron, or if you're on Heroku you'll need to use their Scheduler tool, for example.

Rails Runner or Rake task to send one-off email to registered users

I'd like to send a one off email to users and, being new-ish to ActionMailer, not sure how to approach a one-off like this? Rails Runner, a Rake task or maybe even the rails console on the production machine?!?
Figure I'd add an action for it to user_mailer.rb. And create an accompanying view in app/views/user_mailer/. Just not sure how to trigger it. I'd like to grab a few users and send to them in batches. Happy to do this manually...as we stagger sending the messages over the next few days/weeks.
Appreciate any suggestions or advice.
If you want to do this as automatedly as possible, I'd take a look at the whenever gem. It lets you automate rake (or runner) tasks as cron jobs. You can add a script somewhere in lib that uses ActionMailer to generate the emails, and schedule when they're sent with whenever/cron.
As far as sending in batches,
I would do something like this:
Users.find_each(:batch_size => n) do |m|
mail = UserMailer.new(m.email_address, message)
mail.deliver
end
Obviously, that's near-pseudocode, but hopefully it puts you on the right track?

Email Digest for Rail Application

I am working on a rails application where people can follow each other and post comments. Currently I use delayed_jobs for people to get notifications when someone follows them or post a comment. My question is how can I collect all the notifications for one user and create an email digest for notifications and send the email at the end of the day. Putting it in another way, can we collate the delayed_jobs?
Delayed_job won't do this for you, but it's straightforward enough to do yourself.
The simplest thing to do would be to write a rake task that you run once a day, and have the task find all the activity for that user that has occurred in the past day, and then send them an email about that activity.
Right now you are sending an email on a set of actions (following event or comment event).
Instead, if your user is digest-enabled, insert a record into a pending-email-digest table, and then daily process the email digests via a cron job.

Sending emails by mailer model

I have a database created using Ruby-on-Rails. What I require is a model (perhaps) where I can input some information in a form and then press a button that sends emails to all the users registered in the database. The email contains the details written in the form. Is that possible? Any help will be very appreciated.
Mike
Yes... gather the email addresses in the database. Use the native mailing class for the server technology you are working with. Build the message body as a string (if its a simple email, otherwise you'll want to populate a template file). Create a mail message for whatever server technology you're working with and send to all the gathered emails with the generated body.
Yes yes yes! Rails delivers some great support when it comes to sending emails! using Mailers (you can generate these) all you would need to do is set it up, I would also advice using a gem like mailchimp to test the sent data.
http://guides.rubyonrails.org/action_mailer_basics.html
this tutorial should help you out alot!
Also use your Terminal to fire the Mailers this will make ur process so much faster.
rails console or script/console
rails 2:
MailerClass.deliver_mailer_subclass(passed_parameters)
rails 3:
MailerClass.mailer_subclass(passed_parameters).deliver
Have fun

Best practices on processing email sent to app specific address in rails?

We would like to implement a feature by which users could send an email to an application specific address and we will parse the message and take certain actions on it, similar to 37signals's backpack (and probably some of their other apps).
If anyone has done something similar, could you fill me in on how you did so? I'm unsure on how to, at a high-level, 'import' the email into the app so that I could process it.
Thank you.
I have recently implemented that exact functionality in rails. I would advice you to look at the 'Receive E-mail Reliably via POP or IMAP' in the the Advanced Rails Recipes book.
I've personally found that the best source for getting this up and running and it explains how to do far better than I can. Good luck which ever way you choose to do it :)
Here's how I fetch from a POP server:
require 'net/pop'
pop = Net::POP3.new('mail.yourdomain.com')
pop.start(account, password)
pop.each_mail do |m|
email = TMail::Mail.parse(m.pop)
email.base64_decode
OttoMailer.process_email_in(email, m.unique_id)
m.delete
end
pop.finish
Why not run a Ruby SMTP mailserver, which will receive the mails via port 25, and then you can parse/interpret etc. as you wish ?
(I say Ruby since that's how you've tagged your question)
An alternative solution is to run procmail (or similar), pattern match on the subject, and then invoke scripts (configured in the .procmailrc file). However that may not scale so well for large volumes of mail.
ActionMailer can receive e-mails as well as send them! I don't remember how this is done but if you look at the documentation you can see it there. But from memory the e-mail gets piped through procmail into a script in the script directory.
There seems to be a book on the subject as well.
Good luck! :)

Resources