Basic Confusion about ActionMailer RoR - ruby-on-rails

I'm fairly new to Ruby on Rails and actually entirely new to website mailing. In a lot of example tutorials I see a "from" object assigned to, for example, "new#example.com". When I setup the emailing functionality on a localhost the RoR command prompt says that everything finished fine even when I keep "new#example.com" as the from object. Can I actually mail from a localhost port? What would I have to put as my "from" address in order to actually send mail from the my local web application? Just a regular email I have? How would it be authenticated to ensure that the "from" address is actually the real address?
It seems a really fundamental concept and I understand all the model/view/controller actions that have to be done to make it work but I'm confused I guess as to how it actually works

In general the from field can be anything.
Some mail servers may take action if they think that you are claiming to be someone you are not, such as blocking mail or marking it as spam (via mechanisms such as DKIM or SPF). These are done at the domain level, ie the mail server tries to work out whether the server talking to it is allowed to send email claiming to be from #example.com.
Other mail servers mail just silently rewrite your from field if they know who you are, for example if you are talking to the gmail smtp servers and have authenticated as bob then the from field will be set to bob#gmail.com, unless it is already set to an email address gmail knows you own.
By default, in development rails doesn't try and send email at all. For it to send email you need to configure the deluvery_method, usually this involves either setting it to :sendmail (if you have an appropriately configured instance of sendmail running locally) or setting to :smtp and also providing details of an smtp server to use.

Related

Configuring Griddler with SendGrid and Gmail for Work

I am in need of help figuring out a how to set up incbound email with Griddler and Gmail for Work. I've got a Rails 4 that I'm trying to configure to receive incoming emails using Griddler and SendGrid. I followed the instructions on the Griddler readme exactly. Next I created a tunnel to my localhost environment using ngrok.io. I then went into the SendGrid web app and created a webhook to catch emails going to example.com and send them to the url abc123.ngrok.io.
So far, I think I've got the setup correct. Next I set up an MX record in my domains DNS zone file. The host is # and it points to mx.sendgrid.net.
Here is where things get tricky for me. My site has Gmail for Work set up. I'm not super particular on the details, but essentially, I want a way for users to send a message to foo#bar.example.com and have it routed to Griddler, rather than Gmail. The format of the email address is not terribly important except that it must be dynamic in the sense that foo can be replaced with anything and it will still be routed to Griddler, as long as the domain is bar.example.com. If the domain is just example.com, then it should be routed to Gmail.
I've not had much luck finding resources on how to set this up.
Thanks!
You need to configure 2 separate MX records, one for example.com, and one for bar.example.com. Have the subdomain record point to SendGrid, and have the domain use gmail's MX.

How do I make ActiveMailer always mail me?

I use ActiveMailer with a 3rd party mailing provider. To develop my app, I want to actually see the emails that come in, as a user would, in my email client.
So in development mode, instead of disabling email, I want my app to send the mails, but change the "to" field so that every email is sent to me. Is that possible?
Update: I want to test the full route my email takes: going through my ESP, arriving in my inbox, viewing it in gmail. I'm not looking to just test that an email is created.
I personally recommend letter_opener by Ryan Bates, however, if you actually want to deliver the mail instead of just viewing it in the browser, there are a number of plugins available that others have already listed. No one, however, has mentioned that you can very easily accomplish this using Interceptors.
Create a new initializer in your config/initializers directory in your Rails app:
# config/initializers/development_mail_interceptor.rb
class DevelopmentMailInterceptor
def self.delivering_email(message)
message.subject = "[#{message.to}] #{message.subject}"
message.to = "YOUR_EMAIL#gmail.com"
end
end
ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
This leverages the power of an interceptor on your app. It doesn't configure anything, but rather changes the envelope on the message, altering the to and subject fields. Replace YOUR_EMAIL with the correct value.
The self.delivering_email(message) method is invoked by ActionMailer. You are hooking into that method and override the message envelope.
Finally, you register that interceptor iff we are currently in the development environment.
Be sure to restart your server, and all your mail (in Development) will actually be sent to your email.
Save yourself some trouble and run MailCatcher. MailCatcher is a simple SMTP server that just grabs outbound email and gives it to you in a simple web interface. Install MailCatcher, add this to your environments/development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :host => 'localhost', :port => 1025 }
Start MailCatcher when you start your Rails server (or use Foreman or something similar to deal with it), and then go to http://localhost:1080/ to see the email that your application is sending out.
You may consider checking out something like MockSMTP (OS X); instead of modifying your "to" fields, you instead set the mail server for dev mode to the "fake" SMTP server created by the app, and from then on ALL emails (sent to anyone) go instead to the app.
I've never used it myself, but I remember seeing that the devs at 37signals use it.
On other operating systems, you may consider one of the following projects:
letter_opener by Ryan Bates - popup a new browser window when an email is sent
MailCatcher (mentioned by mu is too short) - runs a fake SMTP server and a web-based interface for viewing mail sent to it
mailtrap - similar to MockSMTP, has both a mock SMTP server and also a separate viewer program
As much as I like this answer, I went with a different option.
Use the mail_safe gem! As well as providing the functionality from sethvargo's answer, it doesn't require any work other than adding the gem, and it automatically figures out who to email from their .gitconfig.
One important note that I rarely saw mentioned when researching this is that you must use deliver, not deliver!. The latter doesnt call interceptors (though apparently it still calls observers, if that's helpful).

Is this even possible? sending email with RoR with different FROM

I am building an application using Ruby on Rails. I want to do something that I am not even sure is possible;
I have a mailer that is working, however I want to enable users to send emails using their own email address in the FROM parameter. Its almost as if the ":from" parameter has no effect on the email sent.
I'm a bit of a noob when dealing with email servers so please be as detailed as possible. I doubt there is a smtp mail server set up on my hosting account, so if I need to do something like install smtp on my hosting account please be as descriptive as possible.
You are able to set the From: field to whatever you want, theoretically, but in practice you are often limited as to what you can put in there. Many email providers will automatically replace the From address with your own regardless so that you can't masquerade as someone else.
You're probably intending to do something like this:
From: Example Customer Name <name#example.com>
Also keep in mind that sending email from arbitrary domains will result in a very high chance of being flagged as spam since you are most likely not listed as as a host authorized to send for those domains which is typically implemented with SPF.
The best practice is to set the address to be something like this:
From: Example Customer Name <you#yourdomain.name>
That way you're not spoofing your actual email address, only the associated label, which is not typically verified.

handle bounced email - ActionMailer

I have a rails app and I am using ActionMailer to send email but now I need to know if the email is delivered or what?
Do anyone has an idea of how to handle sent emails status(e.g bounced, delivered) ?
thanks.
Email service providers use a technique called variable envelope return path. The idea is to encode a unique key for each message into the (envelope) return address so that when a destination smtp server returns email as a bounce you can tie it to the originating message.
If it sounds complex, it is. It gets harder if you want to track response rates, which links were clicked, opens, use Domain Keys, etc. Note that it requires you to set up or configure an SMTP server for handling returned mail.
There are a number of services that provide this all to you on a Software As Service basis. We use socketlabs and are very happy with them. Industrial strength and all. I've also heard of people using Postmark in the Ruby community.

Sendmail vs SMTP

A rails application in production should use smtp or sendmail to send mails?
SMTP is the protocol that is used by nearly all Internet hosts to send mail. This protocol is spoken by sendmail. Sendmail determines where to send your message and how.
Some mail programs (most, today) will connect directly to a mail server and speak SMTP to it. However, the "traditional" method - and arguable the better method - is to let sendmail do it.
There are two reasons for this: 1) nearly every program in UNIX that does what sendmail does is designed to be a drop-in replacement (this includes Postfix and Exim for instance); and 2) sendmail or its replacement was designed to handle mail and nothing else - by using sendmail you don't have to design a SMTP client.
The Mutt email client for UNIX is one email client that still refuses to talk SMTP directly to a mail server; a good (technical) description is on the wiki.
If you have a choice (on UNIX anyway) of talking SMTP directly or using sendmail, use sendmail - especially on servers.
As NDP already mentioned, they both work fine - that is, if your volume of messages doesn't exceed a certain amount.
For example, if your application can talk SMTP to either the local SMTP server (on IP 127.0.0.1, Port 25) or a server in the same subnet (i.e., over a low-latency link), and that server does not use any content filters before it queues a message, you will usually be able to submit a lot more mails over SMTP in a shorter time.
A useful link for Postfix may be General Mail Delivery Performance Tips - note the quote saying
Submit mail via SMTP instead of /usr/sbin/sendmail.
However, on modern hardware, if you don't plan to submit more than about 10 messages per second, you shouldn't notice any real difference.
Your question is incorrect - SMTP stands for Simple Mail Transfer Protocol whereas sendmail is the software piece to send the mail using this protocol.
Use sendmail.
They both will work fine. Action Mailer supports both.
I have used SMTP on several projects successfully. My sense was that this was a little more "standard", but I may be wrong.
I haven't used sendmail. My concern would be that it may be harder to set up in a development environment if you aren't developing directly on Unix/Linux. Where you can talk directly to any SMTP server -- even a remote one, -- you would have to install sendmail on the rails machine to get it to work.
The main problem I run into with email is sending messages asynchronously. Without a local SMTP server, a local sendmail instance is going to be more performant.
Either way, it looks pretty easy to switch if you decide you picked wrong.

Resources