send email using RoR - ruby-on-rails

I followed THIS site on how to send an email using RoR. After running the server and calling the method, it shows in the console:
Rendered user_mailer/welcome_email.html.erb (1.0ms)
Rendered user_mailer/welcome_email.text.erb (1.0ms)
Sent mail to neeraj.narang11#gmail.com (1128ms)
Date: Sun, 18 Sep 2011 13:44:49 +0530
From: from#example.com
To: neeraj.narang11#gmail.com
Message-ID: <4e75a87912b3b_ed81feda64668f4#Narang-PC.mail>
Subject: Welcome to My Awesome Site
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_4e75a878df4a2_ed81feda646659d";
charset=UTF-8
Content-Transfer-Encoding: 7bit
.......
but I'm not getting any email in my inbox. Could someone please tell me what's the issue here...

I guess you haven't:
config.action_mailer.perform_deliveries = true
in your development.rb file.

Some time ago i`ve played around with Ryan screencast on Action Mailer and everything went just fine. Check it out!

Related

Rails 4 ActionMailer not sending emails in development mode

First, let me specify that I am aware that ActionMailer does NOT send emails by default in development mode.
Also, I took a look at similar questions, and none of them was relevant to my situation.
So, here we go:
I am following this Site Point tutorial by Ilya Bodrov-Krukowski, from March 26, 2015 in order to install and setup Devise on my Rails 4 app.
Because we activated Devise's confirmable module, I need to send emails in development to allow users who signup to activate their account and login.
Whenever I create a new user (through http://localhost:3000/users/sign_up), I do get the following alert:
A message with a confirmation link has been sent to your email
address. Please follow the link to activate your account.
However, I never actually receive the email with the confirmation link.
——————————
UPDATE
In my development.log file, I can see that the email was "sent":
Devise::Mailer#confirmation_instructions: processed outbound mail in 172.1ms
Sent mail to hello#mydomain.com (54.4ms)
Date: Tue, 25 Aug 2015 12:15:15 -0700
From: contact#anotherdomain.com
Reply-To: contact#anotherdomain.com
To: hello#mydomain.com
Message-ID: <55dcbec3a1ef8_33b93fcffc4988f493685#XXXXXX.local.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome hello#mydomain.com!</p>
<p>You can confirm your account email through the link below:</p>
<p>Confirm my account</p>
[1m[35m (0.8ms)[0m commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 560ms (ActiveRecord: 1.7ms)
And, when I copy and paste the link from the log into my browser, then I get the following alert:
Signed in successfully.
So, it seems the only thing that is not working is the actual sending / receiving of the email.
——————————
As recommended in the tutorial, I followed Rails documentation regarding ActionMailer configuration and here is what I have in my config/environments/development.rb file:
config.action_mailer.delivery_method = :sendmail
# Defaults to:
# config.action_mailer.sendmail_settings = {
# location: '/usr/sbin/sendmail',
# arguments: '-i -t'
# }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'hello#mydomain.com'}
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Obviously, I am missing something, but I cannot figure out what (I am very, very junior with Rails).
Any idea?
Mail is not sent in development by default. To enable this you have to set config.action_mailer.perform_deliveries = true in development.rb file which I see you already have.
You may also have to enable config.action_mailer.raise_delivery_errors = true to raise delivery errors. That way, you will be able to see if there are any errors in the email delivery process.
Also, double check your default from if that has a valid email.
Also try switching the delivery method to sendmail:
config.action_mailer.delivery_method = :sendmail
This should solve your issue.
You need a mail server for Action Mailer to send emails.
such as
MailGun
which has a free account and a gem which makes set up easy.
mailgun_rails

Sending email with attachments

I've got a mailer that as follows:
class Payments::LateNoticesMailer < AsyncMailer
def notice(payment_id)
#payment = PaymentDecorator.find(payment_id)
#invoice = #payment.invoice
template = "payments/invoices/#{#payment.made_with_type.downcase}/show"
attachments["#{#payment.invoice_filename}.pdf"] =
WickedPdf.new.pdf_from_string( render_to_string( pdf: #payment.invoice_filename,
formats: [:pdf],
template: template,
layout: "layouts/pdf.html"))
mail to: #payment.payer_email,
from: '"RentingSmart" <no-reply#rentingsmart.com>',
cc: #payment.landlord_email,
subject: "*** Your rent payment of #{#payment.amount_due} is overdue ***"
end
end
which I send using SendGrid. Here's my issue, if I open up the email via Gmail, everything works great, the text of the email is there, and the attachment is attached. However, if I open it up using OSX's Mail.app or on my iPhone, I simply get the following:
This is a multi-part message in MIME format...
Anybody have any tips? I think I am following the Rails guides correctly.
Here is the call that I make Payments::LateNoticesMailer.notice(payment.id).deliver
According to the api docs for ActionMailer::Base, if multiple template types are used, all of them are rendered and the mime-type is automatically set to multipart/alternative.
If you add an attachment, the attachment is placed inside a multipart/mixed container.
First question: Are you rendering other types such as text and html? I would not recommend sending out emails with just a pdf part. Even if the text and html parts simply instruct the recipient to open the attachment, they should be there. Ideally, there would be more information in the text/html parts.
Second, are you trying to view the pdf inline, and not as an attachment?
Can you take a look at the raw source of the email and update your post with the structure you're seeing? There will be an initial mime type set in the header. it will look something like this:
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="--==_mimepart_50596418be947_c7223fec9d834d3874256";
charset=UTF-8
Content-Transfer-Encoding: 7bit
This says the parts to follow are not alternative versions of the same information, but instead instruct the email client to display them distinctly.
Later on in the email, your text and html parts should proceeded by something like:
----==_mimepart_50596418be947_c7223fec9d834d3874256
Date: Wed, 19 Sep 2012 06:20:12 +0000
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_50596418be468_c7223fec9d834d38741a5";
charset=UTF-8
Content-Transfer-Encoding: 7bit
And finally, the encoded pdf part should have a mime header like:
----==_mimepart_50596418be947_c7223fec9d834d3874256
Date: Wed, 19 Sep 2012 06:20:12 +0000
Mime-Version: 1.0
Content-Type: application/pdf;
charset=UTF-8;
filename=terms.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=terms.pdf
With a simple test email I just sent to myself with text, html parts, and a large pdf, I can view the email on my iphone. It shows the html part and an icon that lets me download the pdf.
Some e-mail clients may require an e-mail to have a plain text part in order to display it correctly.
I just ran into this message while converting a Rails app from 2.3 to 3.2. I thought I converted my mailer correctly, but the old version specified content_type: "multipart/mixed" in the options. However, when I removed that, I received the attachments and the HTML and plain-text rendered correctly. I think that having that setting in there overrode whatever Rails does to put in different types of content, which was not what I wanted. Thanks to the original answer for leading me in that direction.

Ruby on rails Devise Send Mail

I've been following this tutorial on how to setup Devise. I'm able to make a new User&Email and hit submit. It says "User succesfully created" and in the server log I can see the email that was just sent with subject, email adress to user, name of user etc and it says "Completed 302 Found in 434ms". I don't know what the problem is though because I don't receive an email, although the user gets saved to the database. I'm using gmail to send email.
This is the server log (Changed email addresses to --- for privacy):
Sent mail to ------#-------.com (389ms)
Date: Fri, 12 Aug 2011 09:11:45 -0500
From: ------#-----.com
To: -------#-------.com
Message-ID: <4e4534a175b12_b3e----6b6f998510d8#ubuntu.mail>
Subject: Registered
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Thank you for registering!
Redirected to http://127.0.0.1:3000/users/4
Completed 302 Found in 434ms
Anyone know how it could be solved or recognize the problem?
Have you checked your config/environment/development.rb file? Make sure that config.action_mailer.perform_deliveries is set to true. See below:
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :sendmail #:smtp
Otherwise, it's likely actually being sent and your email account is dropping the message silently. Turn up the first option above (…raise_delivery_errors = true) to see.

pdf attachment not working rails3

In rails 3 when we attach pdf document in email through action mailer,
that pdf is not come as a attachment in e-mail, its come in body like
Date: Wed, 29 Dec 2010 19:56:12 +0530
Mime-Version: 1.0
Content-Type: application/pdf;
charset=UTF-8;
filename=free_book.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=free_book.pdf
Content-ID: <4d1b450431abc_### #BHUSHANF.mail>
JVBERi0xLjMKJf////8KMSAwIG9iago8PCAvQ3JlYXRvciAoUHJhd24pCi9Q
cm9kdWNlciAoUHJhd24pCj4+CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9Q
YWdlcwovQ291bnQgMQovS2lkcyBbNSAwIFJdCj4+CmVuZG9iagozIDAgb2Jq
Cjw8IC9UeXBlIC9DYXRhbG9nCi9QYWdlcyAyIDAgUgo+PgplbmRvYmoKNCAw
IG9iago8PCAvTGVuZ3RoIDU4OAo+PgpzdHJlYW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gNCAwIFIKPj4KZW5k
b2JqCjYgMCBvYmoKPDwgL0VuY29kaW5nIC9XaW5BbnNpRW5jb2RpbmcKL1R5
cGUgL0ZvbnQKL0Jhc2VGb250IC9IZWx2ZXRpY2EKL1N1YnR5cGUgL1R5cGUx
Cj4+CmVuZG9iagp4cmVmCjAgNwowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAw
MDAwMTUgMDAwMDAgbiAKMDAwMDAwMDA3MSAwMDAwMCBuIAowMDAwMDAwMTI4
IDAwMDAwIG4gCjAwMDAwMDAxNzcgMDAwMDAgbiAKMDAwMDAwMDgxNiAwMDAw
MCBuIAowMDAwMDAwOTk0IDAwMDAwIG4gCnRyYWlsZXIKPDwgL1Jvb3QgMyAw
IFIKL0luZm8gMSAwIFIKL1NpemUgNwo+PgpzdGFydHhyZWYKMTA5MQolJUVP
Rgo=
You probably did not define a view for the action. I had the same problem as you and by creating a view in app/views/test_mailer/view.text.erb the need for a :body was rendered obsolete.
This can happen when the :body tag is not present in the mail function.

Why is ActionMailer randomly not displaying the plain text part of my multipart/alternative emails?

I'm trying to send emails that have both a plain text and html component. There are no attachments, but there are inline images in the HTML part. I'm using the content-type "multipart/alternative". I'm not explicitly setting the parts; I'm relying on the filenames to determine the parts. I'm on rails 2.3.10.
Approximately 60% of the time, the emails that are sent come through with no body, and the HTML part is attached to the message with the name "noname" (in gmail anyway). The rest of the time they send as expected. If I send out 10 emails in 10 seconds, almost always 6 of them will be blank.
When I inspect the logs, the messages that come through correctly show both mime parts in the log and show a boundary value in the content-type declaration.
The blank emails still have a content-type of multipart/alternative, but no boundary, because the blank emails do not render the plain text part, and only show the html part. Looking at the templates being rendered, it's clear that the plain text part of the message is completely skipped.
Why does this sometimes work and often fail? The messages are all being sent with a delayed job.
Log for a "good" email:
Rendered notifications/_comment (0.1ms)
Rendered layouts/_footer (2.1ms)
Rendered layouts/_container (2.3ms)
Rendered shared/attachments/_count (0.6ms)
Rendered layouts/speech/_bubble (21.4ms)
Rendered layouts/speech/_detail (0.8ms)
Rendered notifications/_comment (22.6ms)
Rendered layouts/_footer (2.0ms)
Rendered layouts/_container (2.2ms)
Sent mail to [redacted]
Date: Mon, 8 Nov 2010 11:37:24 -0800
From: "[redacted]" <[redacted]>
Reply-To: "[redacted]" [redacted]
To: [redacted]
Subject: Someone commented on your thing
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=mimepart_4cd85174328d6_1486..fdbe5b8d87fe
--mimepart_4cd85174328d6_1486..fdbe5b8d87fe
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline
[the content of the plain text part]
[the content of the html part]
--mimepart_4cd85174328d6_1486..fdbe5b8d87fe--
Log for the blank email:
Rendered shared/attachments/_count (0.1ms)
Rendered layouts/speech/_bubble (0.6ms)
Rendered layouts/speech/_detail (0.7ms)
Rendered notifications/_comment (1.6ms)
Rendered layouts/_footer (25.9ms)
Rendered layouts/_container (26.1ms)
Sent mail to [redacted]
Date: Mon, 8 Nov 2010 11:37:29 -0800
From: "[redacted]" <[redacted]>
Reply-To: "[redacted]" <[redacted]>
To: [redacted]
Subject: Someone commented on your thing
Mime-Version: 1.0
Content-Type: multipart/alternative; charset=utf-8
[the content of the HTML part]
Note that those emails were sent 5 seconds apart, and both emails (were supposed to) contain the same thing.
I had this issue a while back with DJ and ActionMailer- turned out I was running DJ on two utility servers where I thought it was only one, and my deploy hook only restarted the DJ daemon on one and not the other and that caused a whole bunch of problems like this.
The thing that had my head splitting the most was the 60% thing- where some were going out fine where others were completely deformed, but the DJ daemon being reset on one server and not the other totally explained the inconsistency.
Hope this helps!

Resources