I'm trying to send emails from my rails application using GMail's smtp server.
The emails I send appear like sent from my own gmail address, while I would like them to be coming from info#myapp.com.
I've configured the thing using the three ways authentication, and my app has got its unique password. Here's my production.rb
config.action_mailer.default_url_options = {
host: 'my ip',
protocol: 'https'
}
config.action_mailer.delivery_method = :smtp
config.action_mailer.asset_host = "https://my ip"
and here's my initializers/smtp.rb
MyApp::Application.configure do
config.action_mailer.smtp_settings = {
enable_starttls_auto: "true",
address: "smtp.gmail.com",
port: "587",
domain: "mydomain.com",
authentication: :plain,
user_name: 'myusername#gmail.com',
password: 'mypassword'
}
end
Is it possible? How could I do that?
I figured my solution,
I have two mailer controllers in my application, one is from devise and sends mail for user registration related stuff, and the other sends emails to me in the from the contacts page.
First let's see the configuration in google. Google allows to send emails with a different sender email, but it requires that you own the mail address, so there's a procedure to follow to verify your email address on google. It's listed here
I used that procedure to validate my email address: no_reply#myapp.com which is the one I want my emails to appear to be from
Once gmail has verified that you own the sender email address I configured devise, I changed in the config/initializers/devise.rb adding the line:
config.mailer_sender = "'myapp_noreply' <no_reply#myapp.com>"
Second I went to configure my app/mailers/notifications_mailer, by adding the line:
default :from => "no_reply#myapp.com"
I tested everything and it worked fine. If I check in the email's headers I can still see my gmail account appearing. These look like this:
Return-Path: <mygmail#gmail.com>
Received: from myapp.com ([2231:7120::f12c:912f:f12e:5123])
by mx.google.com with ESMTPSA id gn1sm32851027wib.14.2014.04.01.05.20.26
for <info#casamaroma.it>
(version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
Tue, 01 Apr 2014 05:20:26 -0700 (PDT)
Sender: Giulio <mygmail#gmail.com>
Date: Tue, 01 Apr 2014 12:20:25 +0000
From: 'myapp_noreply' <no_reply#myapp.com>
Reply-To: 'myapp_noreply' <no_reply#myapp.com>
To: destination#email.com
Message-ID: <533aaf09c5237_2b0b123fe8c3341a#server.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Because the from field is set to the one I need, I don't care too much for the rest.
Related
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
According to my problem I posted here: mailer error in production only I decided to create a small scaffold app to find a solution.
The problem:
I can't send (newsletter)email with smtp in production, but it works perfectly in development.
You can find the app in my github repository.
I just created a contact_messages scaffold and a simple mailer.
The error log after clicking on the submit button to receive email:
Started POST "/contact_messages" for 194.XXX.XX.XXX at 2013-02-26 19:43:59 +0000
Processing by ContactMessagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xx0nxxJ2xwxxJavvZ278EUy2TABjD9CixxNcxDqwg=",
"contact_message"=>{"name"=>"test", "email"=>"test#testemail.com", "message"=>"test1"}, "commit"=>"Create Contact message"}
Rendered contact_mailer/confirmation.text.erb (0.3ms)
Sent mail to test#testemail.com (38ms)
Completed 500 Internal Server Error in 100ms
Errno::ECONNREFUSED (Connection refused - connect(2)):
app/controllers/contact_messages_controller.rb:46:in `create'
The emails get saved, they are listed in the index. So the database should work fine.
I'm using Ubuntu 12.04, Apache, Phusion Passenger, SMTP with Gmail Account.
Could this probably be a server issue, or am I doing something wrong in the app?
I'm using fail2ban and denyhost. Could these tools block smtp?
Any help will be appreciated, thanks!
By default, Rails sends email by connecting to the target SMTP server. Try using sendmail instead. Add this in your config/initializers/production.rb:
config.action_mailer.delivery_method = :sendmail
I solved my problem with a change to a new passwort (api-key) from mandrill. Still don't know what the problem was, because with the old one it worked in development mode...
The working setting:
YourApp::Application.configure do
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:enable_starttls_auto => true,
:user_name => "MANDRILL_USERNAME",
:password => "MANDRILL_PASSWORD", # SMTP password is any valid API key
:authentication => 'login'
}
To exchange data with another system we send the data as an email attachment to a dedicated address. The email is generated using ActionMailer v3.2.12.
The problem is that when the email arrives at its destination, a redundant attachment named ATT00001.c is a part of the email, in addition to the attachment we created. This causes issues with the import routine at the other end.
A big part of the problem is that we know almost nothing about how the email is being handled at the destination . We also dont know what type of email server is in use and dont have access to check what the email actually looks like when it arrives. We can send it to one of our own addresses and it looks fine there.
I know this is not a lot to go on, but perhaps one of you guys have seen these ATT00001-attachments being added to machine generated emails before.
config.action_mailer.smtp_settings
address: smtp.<mailprovider>.com
port: 587
domain: ourdomain.com
authentication: login
user_name: <removed>
password: <removed>
enable_starttls_auto: false
Update:
We've been able to obtain a copy of the problematic email and it shows the email body rendered after the attachment as an attachment of its own.
We've tried setting ActionMailer's parts_order to make sure the attachment is generated after the email body, it did not help.
Update2:
Sending to my gmail account and showing original raw data I get this.
SENT MAIL
in receipt response from recipient to the correct attachment (the autocreated one creates an error log entry)
(...) cut: to from and through email header information
Mime-Version: 1.0
Content-Type: multipart/mixed;
charset=UTF-8
Content-Transfer-Encoding: 7bit
--
Date: Thu, 28 Feb 2013 12:15:23 +0100
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="thefile.mscons"
Content-ID: <512f3c4b6e875_a8f756dcc642fe#bjorns_arch.mail>
VU5BOisuPyAnVU5CK1VOT0M6Mys3MDgwMDAzNDExNzE2OjE0OlRJTUVSKzcw
... many more lines like this ...
ODAwMDUwNTEyMTc6MTQ6VElNRVIrMTMwMjI4OjEyMTUrUE9XRVNUMTMwMjI4
----
This is with body nil in actionmailer
Next is a RESPONSE from the recipient system, sent to my gmail. It's a receipt on the correct attachment (the extra attachement generates an error, flushing their system)
RECEIVED MAIL
(..) unintersting header stuff with addresses
Content-Disposition: attachment;
filename="afilename.txt"
Content-Transfer-Encoding: base64
Content-Type: Application/EDIFACT; charset="iso-8859-1"
Mime-Version: 1.0
Date: Sat, 16 Feb 2013 11:07:10 +0100
From: ediel#example.com
To: ***#gmail.com
Subject: thesubject
Message-ID: <511f5a53.850a700a.2fa0.2a0eSMTPIN_ADDED_BROKEN#mx.google.com>
X-TM-AS-Product-Ver: IMSS-7.0.0.6298-6.8.0.1017-19380.002
X-TM-AS-User-Approved-Sender: Yes
X-Greylist: Sender is SPF-compliant, not delayed by
milter-greylist-4.0 (isp-app27-vm.isp.example.com [213.239.116.46]);
Sat, 16 Feb 2013 11:07:11 +0100 (CET)
X-ExampleIKT-MailScanner-Information: Please contact the ISP for more information
X-ExampleIKT-MailScanner-ID: r1GA7BqD021150
X-ExampleIKT-MailScanner: Found to be clean
X-ExampleIKT-MailScanner-From: ediel#example.com
X-Spam-Status: No
VU5BOisuPyAnVU5CK1VOT0M6Mys3MDgwMDA1MDUxMjE3OjE0OlRJTUVSKzcwODAwMDM0MTE3MTY6
.. more..
pUSU1FUisxJ1VOVCszKzEnVU5aKzErMjAxMzAyMDAyNDg1Nzcn
something suspicious with the Content-Type? Is a new (empty) attachment generated from the stuff prior to -- in the sent email?
I believe this is to do with inline attachments and Exchange server. Some clients, Apple Mail in particular allow you to add inline attachments, that is, a MIME attachment sandwiched in between text/body parts of an email. Exchange server expects that all attachments appear after any text portion of a mail.
Everything after the attachment in your mail gets treated as an attachment, so the body gets stuffed into a file and named as you reported it to be named. Seeing as you're using ActionMailer, see this answer and possible this answer, which explains that you need to switch the order of the lines of code, and possibly play with some other settings.
Our problem is solved, though unfortunately I cant say what caused the redundant attachment. We worked around it by sending a non-multipart email that contained only the attachment. This solution obviously wont work for people who need to send a multipart email.
Sending non-multipart email with an attachment in Rails is not straight forward. You cant use the attachment helper method and a blank body, you need to put the attachment content in the email body and manually specify the disposition.
class MailMan < ActionMailer::Base
def test
attachment_content = "my attachment"
disposition = "attachment; filename=\"test.txt\""
mail(body: attachment_content, content_disposition: disposition)
end
end
I've got Capistrano set up to send emails after deploying my RoR (2.3.8) application. I have a config/cap_mailer.rb file that basically looks like:
ActionMailer::Base.smtp_settings = {
:address => my,
:port => exchange,
:domain => server,
:authentication => settings,
:user_name => are,
:password => here
}
class CapMailer < ActionMailer::Base
def deploy_notification(cap_vars)
recipients cap_vars[:notify_emails]
from 'deploy#my.org'
subject "New app!"
body "Deployed application...blah blah blah"
end
end
Then, in my deploy.rb file, I have the following:
require 'config/cap_mailer.rb'
...
desc "Email recipients of deployment"
task :notify do
puts " * Sending notification email"
set :notify_emails, ["test1#my.org", "test2#my.org", etc.]
CapMailer.deliver_deploy_notification(self)
end
Now this all works fine and dandy......until I put more than 7 email addresses in the :notify_emails array. Up to 7 works fine, but when I put 8 or more (all valid addresses), the email gets screwed up a little bit (still goes through to the first 7, at least). Looking at the email header, it shows that it is cutting off the 8th (and 9th, 10th, ...) address from the 'To:' and putting it in the message body.
HEADER:
thread-index: AcyaZxlga08L9p35QYKJ22aiGG2zeA==
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By ...
Received: from exchange.my.org ([ip address]) by ...; Thu, 3 Nov 2011 14:28:08 -0600
Date: Thu, 3 Nov 2011 14:28:08 -0600
From: deploy#my.org
To: test1#my.org,
test2#my.org,
test3#my.org,
test4#my.org,
test5#my.org,
test6#my.org,
test7#my.org
HEADER:
BODY:
test8#my.org
Message-Id:
<4eb2f95816341_135ff800c21ac130#my_box.local.tmail>
Subject: New app!
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Return-Path: deploy#my.org
X-OriginalArrivalTime: 03 Nov 2011 20:28:08.0494 (UTC)
FILETIME=[19601CE0:01CC9A67]
Deployed application...blah blah blah
BODY:
In addition, the subject does not appear on the email, even though the "error text" in the message body shows the correct subject.
Does anyone have any idea why this is happening? What is it about having 8 or more recipients that breaks it? I've Googled around and can't find anything about ActionMailer having a limit on the number of recipients (even if there were, that's a small limit). Is there something I'm missing? Any help is appreciated! I really need to be able to send to 8 or more recipients.
UPDATE: Setting the recipients directly with an array of 8 or more addresses still breaks things, so this clearly seems like a problem with ActionMailer and not Capistrano.
recipients ["test1#my.org", "test2#my.org", "test3#my.org", "test4#my.org",
"test5#my.org", "test6#my.org", "test7#my.org", "test8#my.org", "test9#my.org"]
The 7th email address is not followed by a comma, which could be the problem. Try passing a string to recipients, like cap_vars[:notify_emails].join(','), with no newlines.
Try passing a string to recipients without commas (there have been reports of commas as a problem), like:
cap_vars[:notify_emails].join(' ')
With all respect some simple things that you might overlook in debugging difficult errors:
If its mail server related install it on another production server? If you not sure it is the mail server acting up looking for another error may be of no help.
Try upgrade rails / ruby ? Perhaps its a bug in the code
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.