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

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.

Related

Rails 4 - yandex mail is not sending

In rails 4.2.4, I am using gem 'rails_config' for emails feature. In development environment all mails are sending properly but in test environment it is showing an error like Net::SMTPFatalError (553 5.7.1 Sender address rejected: not owned by auth user.):
In setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.yandex.ru",
:port => 465,
:domain => "yandex.ru",
:authentication => :login,
:user_name => "ssstest#yandex.com",
:password => "pwd567#",
:ssl=> true,
:enable_starttls_auto=> true,
:tls=> true
}
In user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "ssstest#yandex.com"
...
end
Server log's output is,
D, [2016-05-06T06:24:25.089270 #23566] DEBUG -- :
Devise::Mailer#confirmation_instructions: processed outbound mail in 954.3ms
I, [2016-05-06T06:24:26.500832 #23566] INFO -- :
Sent mail to sample_demo#yopmail.com (1408.8ms)
D, [2016-05-06T06:24:26.501240 #23566] DEBUG -- : Date: Fri, 06 May 2016 06:24:25 +0000
From: please-change-me-at-config-initializers-devise#example.com
Reply-To: please-change-me-at-config-initializers-devise#example.com
To: sample_demo#yopmail.com
Message-ID: <572c389917f09_5c0e10bb99411990#ip-10-65-178-51.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
How can I fix this issue? Please help me
As the error log indicates, please change the from email address inside config/initializers/devise.rb
config.mailer_sender = 'ssstest#yandex.com'
port: 465
authentication: login
smtp_enable_starttls_auto: true
smtp_openssl_verify_mode: true
As mentioned here
https://docs.gitlab.com/omnibus/settings/smtp.html#yandex
Have you added config.action_mailer.delivery_method = :smtp in the environments/test.rb ? And it is not a good practice to store usernames and passwords right in the email setup. Store them as environment variables.

Sending mail with ActionMailer and Outlook/Mandrillapp SMTP server

I am using ActionMailer to send mails for a 'Contact Us' form in my application.
I am using Mandrill app for sending my emails.These are my settings:
config/environments/development.rb
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:enable_starttls_auto => true,
:user_name => 'SMTP username i.e Mandrill a/c username',
:password => 'API key for development',
:domain => 'example.com',
:authentication => 'plain'
}
config/environments/production.rb
I have removed the line
config.action_mailer.raise_delivery_errors = true
and changed the password for production - which is Mandrill app API key for production.
app/mailers/contactus_mailer.rb
class ContactusMailer < ActionMailer::Base
default :from => "noreply#example.com"
default :to => "help#example.com"
def new_message(message)
#message = message
mail(:subject => "[WebsiteName] #{message.name + " - " + message.email}")
end
end
Validity of the above accounts on custom domain - example.com
The above email accounts i.e noreply#example.com & help#example.com are provisioned and fully functional. The above accounts are setup at Outlook.com and I have also double-checked the MX records for my domain example.com and the domain settings are Active for my domain. As a proof, I can send/receive emails on both accounts from the accounts.
Development and Production environment Logs:
When I use the Contact Us form in both environments, ActionMailer reports no errors and redirects successfully to Home page.
Started POST "/contact" for 127.0.0.1 at 2013-08-18 12:35:37 +0530
Processing by MessagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UNgMrA04yk4sIbqtXjlLvLvDINgrBT5eP0wMSRYNgPs=", "message"=>{"name"=>"Dummy name", "email"=>"abc#pqr.com", "content"=>"Random body"}, "commit"=>"Send Message"}
Rendered contactus_mailer/new_message.text.erb (0.5ms)
Sent mail to help#example.com (2679ms)
Date: Sun, 18 Aug 2013 12:35:38 +0530
From: noreply#example.com
To: help#example.com
Message-ID: <52107242dbf6c_12a7f3fd8b1835ad03979#Jatins-MacBook-Pro.local.mail>
Subject: [WebsiteName] Dummy name - abc#pqr.com
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Name: Dummy name
Email: abc#pqr.com
Body: Random body
Redirected to http://localhost:3000/
Completed 302 Found in 3841ms (ActiveRecord: 0.0ms)
Mandrill App API log for Production environment:
Full Request:
{
"from_email": null,
"from_name": null,
"async": false,
"key": "API key for production",
"raw_message": "Received: from example.com (unknown [23.20.245.109])\n\t(Authenticated sender: key_production#gmail.com)\n\tby ip-10-31-147-25 (Postfix) with ESMTPSA id 6811A151A064\n\tfor <help#example.com>; Sun, 18 Aug 2013 08:19:11 +0000 (UTC)\nDate: Sun, 18 Aug 2013 08:19:11 +0000\nFrom: noreply#example.com\nTo: help#example.com\nMessage-ID: <5210837f5ce24_26e56b87992f#5c11fd99-5533-4855-af78-40e02c939412.mail>\nSubject: [WebsiteName] Dummy name - abc#pqr.com\nMime-Version: 1.0\nContent-Type: text/plain;\n charset=UTF-8\nContent-Transfer-Encoding: 7bit\n\nName: Dummy name\n\nEmail: abc#pqr.com\n\nBody: Random body",
"to": [
"help#example.com"
]
}
Full Response:
[
{
"email": "help#example.com",
"status": "rejected",
"_id": "9c9f88c588ee4f369437b8dd5d531c8c",
"reject_reason": "soft-bounce"
}
]
Mandrill App API log for development environment:
The Full Request for development env. is similar to the production environment. However, in development the response is different.
Full Response:
[
{
"email": "help#example.com",
"status": "sent",
"_id": "e67f31f893a84ecdb0ed2438e5741ce1",
"reject_reason": null
}
]
NOTE: I am not getting email on my account help#example.com in both development and production environments.
Queries:
Why am I getting rejected status and soft-bounce reject reason for production env., whereas for development it says sent status and no reject reason.
Why am I not receiving any mails in both the cases?
P.S.
Initially, I wasn't using Mandrill app and was using smtp.live.com as my SMTP server along with my no reply#example.com credentials, but that didn't work out.
Then I switched to Mandrill after some digging on Google.
It'd be equally good if someone can help with the Outlook mail setup. That way, Mandrill won't be required at all.
I got it working with and without Mandrill. I didn't get any emails until 7 days later, my inbox was flooded with all my test emails.
Seems there was a glitch with my DNS, TXT records for my email account, which had caused the delay.
Also tried without Mandrill, and the mails are getting sent properly. So, posting the settings for Outlook here. Might come handy for someone else.
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.live.com",
:port => 587,
:enable_starttls_auto => true,
:user_name => 'noreply#example.com',
:password => 'password',
:domain => 'example.com',
:authentication => 'plain'
}
Note: For use in production, set raise_delivery_errors to false.
Do you have
config.action_mailer.default_url_options = {
:host => 'YOUR_HOST_NAME_HERE'
}
defined in application.rb or production.rb? It should be set to your domain name. I've found that some servers will reject mail without an explicitly defined hostname.

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

Unable to send email when view has links

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

No errors in development.log, but mail wouldn't be sent

I use ActionMailer to send my email through Rails, but I can't receive anything.
My development.log said:
Sent mail to mymail#gmail.com (2107ms)
Date: Wed, 07 Dec 2011 17:14:30 +0800
From: no-reply
To: mymail#gmail.com
Message-ID: <4edf2e765a139_147105390c874e3#raincole-laptop.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>
Hello mymail#gmail.com!
</p>
<p>Someone has requested a link to change your password, and you can do this through the link below.</p>
<p>Change my password</p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
But there is nothing new in my inbox or spam box.
I have set:
ruby-1.9.2-p290 :008 > Rails.application.config.action_mailer[:raise_delivery_errors]
=> true
So, how can I see the real error logs to debug?
Edit:
I write a email.yml:
development:
reply: "no-reply"
host: "starfish.dev"
smtp:
address: "smtp.gmail.com"
port: 587
domain: "starfish.dev"
authentication: "plain"
user_name: "mymail#gmail.com"
password: "mypassword"
enable_starttls_auto: true
Then in application.rb:
$EMAIL_CONFIG = ActiveSupport::HashWithIndifferentAccess.new YAML.load(File.open("#{Rails.root}/config/email.yml"))[Rails.env]
config.action_mailer.smtp_settings = $EMAIL_CONFIG[:smtp].symbolize_keys if !$EMAIL_CONFIG[:smtp].nil?
in development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
But, it still just sends to log file, no to real address.
The development and test environments don't use real mailers (unless you specifically configure them). By default it just does exactly what you have noticed: it goes through all the motions of sending an email... but then doesn't actually send it, just logs the email that would have been sent.
EDIT: note that this is a reply to the original question, which did not contain any smtp settings.
have you tried telnet smtp.yoursite.com 587?
if there's no responding, please check your firewall.

Resources