#61 Sending Email (revised)
Thanks to Action Mailer, sending email in Rails is easy but there are several gotchas to be aware of. Here I show how to send an email upon submitting a form and the configuration options necessary.
- Download:
- source codeProject Files in Zip (88.7 KB)
- mp4Full Size H.264 Video (18.7 MB)
- m4vSmaller H.264 Video (9.63 MB)
- webmFull Size VP8 Video (11.8 MB)
- ogvFull Size Theora Video (20.9 MB)
I would like to note that if you want use image_tag in a mailer you have to set
config.action_mailer.asset_host = 'www.yourhost.com'
so that image_tags have the right source in the email
it will turn this
into this
since
config.action_mailer.default_url_options
has no effect on image_tagCan someone point me in the right direction on how to set up environment variables in rails?
Where for example would ENV["GMAIL_USERNAME"] = 'secret' be saved?
If you are using POW then you can setup a file called .powenv in the root of your app ie.
You can then use in your app like normal ie.
DOCS: http://pow.cx/manual.html#section_2.2
Amazing tip. Thanks!
Or use the figaro gem.
what Operating System are you using?
I'm on Ubuntu. I thought the environment settings would be saved in a Ruby application config file. I guess I was wrong. I should just save this in ~/.profile? Is that safe? Thanks weblee and murdoch for the help.
That should work. Works for me.
on Unix/Linux based systems remember that environment variables almost always refers to the operating systems is user/session/machine variables, not application configuration files.
Thanks, Ryan. Being able to see the email in the browser sure beats looking at the markup in the logs and imagining how it might look!
Any thoughts on sending multiple e-mails such as a newsletter? A block perhaps?
How about passing an Array<#email> and add them all to the BCC?
You could also iterate over the array in a controller and deliver one at the time. I'd recommend using a service like SendGrid to handle this for you.
How did you get the image on the railscasts.example@gmail.com? Address book or some gravatar-like service? :)
I think he uploaded that image to the gmail account railscasts.example@gmail.com.
I am having issues setting up my mail server (Mac Mini using Lion Server). I get different answers on how to set this up when I call technical support. When I try to send emails it sits until it times out. It is looking for something on my server but I do not know what information to put where. This brings me to my question.
For ease I wanted to use my Gmail smtp settings for my Rails applications. However it appears that Gmail will force the FROM value to the Gmail email address and name (if one is set up) regardless of what the FROM value in the mailer is. Is there any way around this using Gmail? If not are there other email services that are as easy to configure as Gmail is but do not force the FROM value to be the email address? This was not addressed in this railscast.
I'd love to send the email not only to the user but to myself as well. How would you do that?
I have added an interceptor (it sends email to the the admin) but then it stops sending the email to the user for some reason. I don't know why... I posted the question on Stackoverflow: http://stackoverflow.com/questions/11539690/rails-3-mailer-interceptor-conflicting
What is the reason that it sends empty e-mails ??
My setup is sending blank emails too. Did you ever figure it out?
Hi Ryan,
I signed up on Railcasts today and now i feel like a kid in candy store. Too much of exciting stuff.
However, my excitement is bit tampered...I guess am having difficulty finding the right episodes.
After going through this episode, I am able to get email working but stuck at one place.
my config:
ActionMailer::Base.default_url_options = {host: "localhost:3000"}
works perfectly. But how do I make the host dynamic? I will be using heroku to host multiple domains for the same app. So my hosts will be sub1.mydomain.com, sub2.mydomain.com etc.
If you have already covered this in another episode, can you point me to it please.
Regards,
Murty
Just a small note in case others had the same issue i did:
This:
# specify what domain to use for mailer URLs
config.action_mailer.default_url_options - { host: "localhost:3000" }
Should actually be:
# specify what domain to use for mailer URLs
config.action_mailer.default_url_options = { host: "localhost:3000" }
Once i changed the - to an =, it worked fine. Thanks again for a great railscast and gem!
I've followed this tutorial just like it is but it gave me this:
SMTP error: 530 5.5.1 Authentication Required.
i've google it everywhere and finally i could! using the SMTP configuration like this:
I hope it helps,
Best Regards!
add this line to your config
openssl_verify_mode: 'none'
When you use a command like:
does .deliver return a result? That is, is:
valid code, and what, if anything, gets returned. I'm trying to figure out a way to catch an error if the smtp server is down.
Hi I just want to ask on how to configure live email account by Microsoft on
how about the live email account configuration that is similar with gmail smtp configuration?
example
username: myemail@live.com
password: SamplePassword
Thanks!
There is a typo in the ASCIIcast; In the code block for adding the default url options it's specified as "- { host" but should be "= { host".
Thanks for reporting this error and my apologies for the late reply. I have now corrected the ASCIIcast.
Great railscast! I didn't like the gmail option but found this awesome blog post which explains how to start postfix on your mac and send mail from that - no passwords or environment variables required
http://andr.esmejia.com/posts/20-send-real-mail-with-rails-from-your-development-environment-on-mac-os-x
In brief:
in terminal do
sudo postfix start
In config/environments/development.rb :
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => "whatever.com",
}
Worked great on Mac OSX Lion, Rails 3.2
Remember to look in your gmail spam folder for the email (on gmail.com not just Mac Mail app)