Unable to send email when view has links - ruby-on-rails

I am using ruby 1.9.3p194 and Rails 3.2.3.
I am facing problem while sending emails whose template has a link in it. I have written following code:
invitation_mailer.rb
def event_invitation(user, event)
#user = user
#event = event
mail(:to => #user.email, :subject => "Invitation to participate in #{#event.name} event")
end
event_invitation.html.haml
Hello,
%br
%br
Your friend #{#event.user.full_name} has invited you to participate in #{#event.name} event. If you want to accept
this invitation, use the following link:
= link_to calendar_index_url, calendar_index_url
%br
%br
#{t('shared.team')}
user.rb
def xyz
...
InvitationMailer.event_invitation(self, event).deliver
end
If I remove the link line in the view, I am able to receive emails but not with the link inside the view. But the log shows that an email has been sent.
LOG
Sent mail to abhimanyu#gmail.com (6117ms)
Date: Fri, 02 Nov 2012 20:59:33 +0530
From: invitation#dev.tld
To: abhimanyu#gmail.com
Message-ID: <5093e6dd6a275_14f733fc536034cd444087#Abhimanyus-iMac.local.mail>
Subject: Invitation to participate in new event event
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hello,
<br>
<br>
Your friend Abhimanyu Kumar has invited you to participate in new event event. If you want to accept
this invitation, use the following link:
http://localhost:3000/calendar
<br>
<br>
Dev Team
Any help to figure out the problem would be appreciated.
Thanks in advance.

Did you specify the ActionMailer default_url_options in your environment config file? Either in config/environments/development.rb or config/environments/production.rb (depending on the environment you're working on), make sure you include the following:
config.action_mailer.default_url_options = { :host => "example.com" }
See more info here: http://api.rubyonrails.org/classes/ActionMailer/Base.html#label-Generating+URLs

Related

Rails Mailer issue with sending attchemnt over mail

I am migrating an application from Rails 2.3 to Rails 3.1, the emails are not working, when i send an email with attachment i see a plain/text email with the encoded pdf content in the email instead of as an attachment.
here is the command i used to send email
ret = UserMailer.return_forms(#customer[:email], #store, id, #customer[:document]).deliver
here is the definition of returns_form, the return_forms is a method in UserMailer class (class UserMailer < ActionMailer::Base)
def return_forms(email, store, order, pdf_document_path)
load_smtp_settings("noreply")
#recipients = email
#subject = "#{business_name}: Return forms"
#body = "Please follow the instructions within the forms to return your merchandise. Thank You."
attachments['free_book.pdf'] = {mime_type: 'application/pdf',content: File.read(pdf_document_path) }
#from = email
#date = Time.zone.now
#headers = {}
end
I see the content as
--
Date: Mon, 10 Aug 2015 16:16:26 +0530
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <55c881028af96_48d43fe94782e9a0658aa#amol-desktop.mail>
Please follow the instructions within the forms to return your merchandise. Thank You.
--
Date: Mon, 10 Aug 2015 16:16:26 +0530
Mime-Version: 1.0
Content-Type: application/pdf;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=free_book.pdf
Content-ID: <55c88102872ba_48d43fe94782e9a0657a0#amol-desktop.mail>
JVBERi0xLjQKJcfsj6IKNyAwIG9iago8PC9MZW5ndGggOCAwIFIvRmlsdGVy
IC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nH1W227bOBB991cM0AWqABZLUvc8
bTZNivTiuLUX2GKzD6xE22pk0aWktOnX75CSbUW+xEhkDufMGc4cjvIDKGFA
zad7puvRmy8JLKuRtYJejnyfghd5MQRBSIGjTY4W1srDkB+xeh4HHocQ92yM
clxzr7NFLD6w7fx6WOtn4gWhD2ewjEfeaZIeuHW0GUYxg3Nwn/rhGaI+vnVt
8wzhHN4PqX+GqQ+3njbTxIez8DhOzhD14dZzUM+j8MD3zxH14K3nIOZReEjp
The return_forms call should have a call for mail() method at the end. Something like:
mail(:from => your_from, :to => your_to, :subject => your_subject, :body => your_body)
It is also a good idea to enable mailer errors in your development.rb to see if any errors appear:
config.action_mailer.raise_delivery_errors = true

Mail to all user

I want to send a mail to all my users when an article is published. I have this code :
Mailer class
class AdminMailer < ApplicationMailer
def notification_mail(user)
#user = user
mail(to: #user.email, subject: '**TEST**')
end
end
Article.rb
after_create :send_email_to_users
def send_email_to_users
User.all.each do |user|
AdminMailer.notification_mail(user).deliver_now
end
end
Configuration
ActionMailer::Base.smtp_settings = {
:user_name => 'simon.m#********.com',
:password => '********',
:address => 'smtp.*******.com',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
This code seems to work because when I post an article I can see on my console that e-mail are send but I didn't receive them into my mailbox.
CONSOLE
AdminMailer#notification_mail: processed outbound mail in 23.1ms
Sent mail to simon.m#*******.fr (30014.1ms)
Date: Mon, 08 Jun 2015 05:21:05 -0700
From: simon.m#******.com
To: simon.m#*********.fr
Message-ID: <557588b117179_ae53f7fcf56e0d0328b8#localhost.localdomain.mail>
Subject: **TEST**
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_557588b114e80_ae53f7fcf56e0d0327be";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_557588b114e80_ae53f7fcf56e0d0327be
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: base64
VW5lIGlkw6llIHZpZW50IGQnw6p0cmUgcHVibGnDqWUKPT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0K
----==_mimepart_557588b114e80_ae53f7fcf56e0d0327be
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<html>
<body>
<!DOCTYPE html>
<html>
<head>
<meta content=3D'text/html; charset=3DUTF-8' http-equiv=3D'Content-Ty=
pe' />
</head>
<body>
<h1>Une id=C3=A9e vient d'=C3=AAtre publi=C3=A9e </h1>
</body>
</html>
</body>
</html>
Anyone know how to make it works ?
Make sure you have deliveries and delivery errors enabled for the environment your app is running in.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
Last time I did this I had to enable less secure apps on my google apps account, it might be the same for you. It's hard to say without any errors. Visit https://www.google.com/settings/security/lesssecureapps to enable that. More info
Make sure Google hasn't blocked your IP. Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue.
Source
Seeing some sort of error would help to narrow this down but hopefully this helps!

Rails + SendGrid Unauthenticated senders not allowed

I'm currently working on sending emails via sendgrid and currently sending emails to new users is working. ...But strangely, very similar emailing options aren't working - the full email including the recipient is displayed in the log but is never received.
Here's what I have:
In a mail.rb file in initializers:
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => configatron.sendgrid.username,
:password => configatron.sendgrid.password,
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
with the username and password being defined elsewhere - they are confirmed to have their correct values
When a new user registers, I call my UserMailer class and do the following:
def welcome_email(user)
#email = user.email
#name = user.full_name
mail(to: #email, subject: "Welcome!")
end
And the worker calls it by simply doing the following:
UserMailer.welcome_email(user).deliver
This much works; I receive the email and that's that.
But when I try to send an email from a different method, magically things break down.
In the SAME UserMailer class:
def request(org, recipient, item)
#org = org
#recipient = recipient
#item = item
mail(to: #org.email, subject: "A new request has been posted!")
end
Using the SAME function (although this time outside of a worker so I can debug easily):
UserMailer.request(org, recipient, item).deliver
The email appears in the terminal perfectly:
Sent mail to mypersonalemail#test.com (4717ms)
Date: Mon, 31 Dec 2012 01:03:35 -0800
From: mysendingemail#test.com
To: mypersonalemail#test.com
Message-ID: <[longhexstring]#localhost.localdomain.mail>
Subject: A new request has been posted!
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_50e154e769903_3c41bba7ec576d5";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_50e154e769903_3c41bba7ec576d5
Date: Mon, 31 Dec 2012 01:03:35 -0800
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <[longhex]#localhost.localdomain.mail>
Hello world!
----==_mimepart_50e154e769903_3c41bba7ec576d5
Date: Mon, 31 Dec 2012 01:03:35 -0800
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <[longhex]#localhost.localdomain.
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<p>
Hello world!
</p>
</body>
</html>
But, I check my inbox on my personal email, the junk, the trash, and received nothing. Even sendgrid has no logs of me ever requesting to send request emails! But the welcome emails are working perfectly fine, and even are logged through sendgrid. I don't see what could cause one group of emails to send but not another and this is really baffling. No obvious errors in syntax jump out at me, and the values I use are of the correct datatype and not nil.
Any help with this strange issue would be extremely appreciated.
The solution ended up being remarkably simple, so for those who find this thread and need help I'll at least post what I did.
In development.rb I did the following to see the error being raised:
config.action_mailer.raise_delivery_errors = true
And got this message:
Net::SMTPFatalError - 550 Cannot receive from specified address <mypersonalemail#test.com>: Unauthenticated senders not allowed
Which is strange considering I thought I was authenticated with the info I put into .env Outputting my configatron.sendgrid.username sure enough is nil. Still trying to figure out why that would be and I'm still not sure how the welcome emails were sent.
Sure enough, changing my smtp settings to be hardcoded values for authentication worked like a charm. Now the question becomes how to get the ENV to not come out nil...
The end solution was simply me being on the wrong localhost. I was using rails's default rails server at port 3000, but foreman start (not sure if by default but in my project's case) was using port 5000 instead.
I had this issue when running code via rake. The problem was that the SENDGRID_USERNAME and SENDGRID_PASSWORD environment variables stored in my .env were not loaded automatically via rake. The solution was to run rake through foreman:
foreman run bundle exec rake namespace:task

Rails 3 - ActionMailer/Mail gem - 'from' address is being over ridden

I have a simple contact form on a site.
There is an email account just to handle the mail being sent through the contact form. This is 'mycompany.noreply#gmail.com'
The problem
When a user fills in the form and sends it, two emails are sent. One confirmation email to the user of the site and the actual filled in form to the company.
The company wants it so when they receive the email from the user, they hit reply and the users email address is automatically in the 'to' field. But when they do that at the moment, what they see is "Senders name 'mycompany.noreply#gmail.com'" instead of "Senders name 'sender_email_address'".
The code
class Notifications < ActionMailer::Base
def enquiry(user)
#user = user
mail(to: "employee#mycompany.com", subject: "website enquiry", from: "#{#user.name} <#{#user.email}>")
end
def no_reply(user)
#user = user
mail(to: #user.email, from: "My Company <mycompany.noreply#gmail.com>", subject: "Message received at mycompany.com")
end
end
Tests
The test for the from field passes.
require "spec_helper"
describe Notifications do
let(:contact_form) { FactoryGirl.build(:contact_form) }
context "enquiry" do
let(:mail) { Notifications.enquiry(contact_form) }
it "renders the headers" do
mail.from.should eq([contact_form.email])
end
end
end
Also, I am using mailcatcher.
Mailcatcher also shows the correct user.email in the from field.
It only seems to be when the actual email is received (using gmail) that the wrong address appears.
Any help appreciated.
EDIT
Default mail settings:
# mail config settings
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'mycompany.noreply#gmail.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true }
mail header from mailcatcher
Date: Sat, 13 Oct 2012 21:08:02 +0100
From: Joe Bloggs <joebloggs#gmail.com>
To: mycomany.noreply#gamil.com
Message-ID: <5079ca229969b_67c23fc061434ed023056#Mark-Charless-iMac-2.local.mail>
Subject: website enquiry
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5079ca226fb17_67c23fc061434ed02277b";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5079ca226fb17_67c23fc061434ed02277b
Date: Sat, 13 Oct 2012 21:08:02 +0100
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <5079ca227ec80_67c23fc061434ed02288a#Mark-Charless-iMac-2.local.mail>
The problem is that you're using the Gmail SMTP server.
As per this question: Rails and Gmail SMTP, how to use a custom from address, you cannot set custom From addresses unless that account is linked to your gmail account.
Alternatives:
Use another SMTP server that will allow you to set your own From addresses.
Create a nice HTML-based email that is sent to employee#mycompany.com with a big button that is a mailto link to the email address of the user who filled in the form, and make sure they don't reply to the automated emails, but use this button within the email.
Gmail does not allow name spoofing. There for it will not use the default from: but it will use the SMTP username.

Rails 3 ActionMailer - no recipent address

i have a problem with my prod actionmailer. I'm hosted on site5 sharing cloud. The problem is i get this error message.
A message that you sent using the -t command line option contained no
addresses that were not also on the command line, and were therefore
suppressed. This left no recipient addresses, and so no delivery could
be attempted.
------ This is a copy of your message, including all the headers. ------
Date: Sat, 17 Sep 2011 21:06:42 +0000
From: noreply#mydomain.com
To: myemail#mydomain.com
Message-ID: <4e750be27b3ef_4a9c159402ee91b039763#themailserver.com>
Subject: test
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
i added default
default :recipient => 'noreply#mydomain.com'
but the mail return me the same error.
my ActionMailer controller
class Emailtousers < ActionMailer::Base
default :from => "noreply#mydomain.com"
default :recipient => "noreplay#mydomain.com"
def plan_notification(resource)
#resource = resource
puts resource
mail(:to => "myemail#mydomain.com", :subject => 'test')
end
end
What i missed?
i followed this to help me http://edgeguides.rubyonrails.org/action_mailer_basics.html
I got this problem using the :sendmail delivery method with exim. Adding this to config/application.rb fixed it for me:
config.action_mailer.sendmail_settings = { :arguments => "-i" }
I found the solution at https://github.com/mikel/mail/issues/70#issuecomment-2639987

Resources