Sending mail with ActionMailer and Outlook/Mandrillapp SMTP server - ruby-on-rails

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.

Related

Emails send from rails with O365 being rejected as spam

I have a Rails 5 app deployed on Heroku. I'm using devise for email and have setup everything correctly to send password reset emails. This works perfectly in my dev environment. However, when I send from production I get an error in my 0365 inbox that says:
Generating server: CO2PR13MB0140.namprd13.prod.outlook.com
myemail#gmail.com
Remote Server returned '550 5.7.708 Service unavailable. Access denied, traffic
not accepted from this IP. For more information please go to
http://go.microsoft.com/fwlink/?LinkId=526653 AS(8561)
[CO2PR13MB0124.namprd13.prod.outlook.com]'
Original message headers:
Received: from CO2PR13MB0140.namprd13.prod.outlook.com
([fe80::c872:9c6:9d6a:8b3]) by CO2PR13MB0140.namprd13.prod.outlook.com
([fe80::c872:9c6:9d6a:8b3%4]) with mapi id 15.20.1339.019; Wed, 14 Nov 2018
02:42:20 +0000
MIME-Version: 1.0
Content-Type: text/plain
Date: Wed, 14 Nov 2018 02:42:20 +0000
Message-ID:
<5beb8b87e888a_42b06292aba70609bd#01184e4c-2116-478b-a773-fcc26ac206aa.mail>
Subject: Reset password instructions
In looking into this, and talking to support, it appears that the it's being rejected because a third party server is (my app on Heroku I'm assuming) is trying to send out the email with 0365 settings.
Here are my development.rb settings that work perfectly:
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.office365.com',
:port => '587',
:authentication => :login,
:user_name => ENV['365_USERNAME'],
:password => ENV['365_PASSWORD'],
:domain => 'mysite.com',
:enable_starttls_auto => true
}
Here are my production.rb settings that cause O365 to reject the email. I've tried playing with these quite a bit. And I've ensured that the "from" email matches the login email. i.e. I'm not trying to send from no-reply#mysite.com or similar.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = { :host => 'www.mysite.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.office365.com',
:port => '587',
:authentication => :login,
:user_name => ENV['365_USERNAME'],
:password => ENV['365_PASSWORD'],
:domain => 'mysite.com',
:enable_starttls_auto => true
}
From discussions with support. I may need to add an SPF TXT record to allow emails to be sent from a remote server. The problem is, I have no idea what to add to the SPF record. My current spf record is:
v=spf1 include:spf.protection.outlook.com -all
Their response just says they have blacklisted your IP, so it’s not down to your message content.
If you are sending from Heroku, you may need to list them in your SPF as well. Also ensure that your host name resolves backwards as well as forwards - that should be possible in Heroku’s control panel.
Once you’ve done that, check your SPF record gives a pass to your message source using kitterman.com or mxtoolbox testing services.
O365’s spam filter and blocking policy is pretty bad anyway, however I have had success in asking their support to remove blocks, though you need to persevere because they reject all such requests by default. This is poor service, but it’s still far better than you will get from gmail.

Sendgrid not sending email?

I am trying to implement sendgrid into my backend api rails system so that when a user signs up I can send them a welcome email. After making a post request and handling user creation, I get this verification:
UserMailer#send_sign_up_email: processed outbound mail in 43.5ms
Sent mail to *******#gmail.com (185.8ms)
Date: Wed, 28 Feb 2018 16:54:05 -0800
From: *******#gmail.com
To: *******#gmail.com
Message-ID: <5a974f2d39c92_c5b2abcd76769fc423e0#albert-VirtualBox.mail>
Subject: Welcome to BottlesTonight albert jin!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
My code looks exactly like in this link https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html.
This looks all fine and well, but the email is just not sending (I put stars here for the email but I actually put in my email, and I used another one of emails as the default for sending). There is no email in my outbox or inbox.
However, now that I think about it, I never "logged in" with my email or entered the password, so the application shouldn't have access to send emails with my email. I also never did anything with the api key that I made on my sendgrid account. Furthermore, for the environment.rb file, I wasn't sure what to put in domain, so I put gmail.com. These all seem kinda sketchy to me, I think the tutorial doesn't contain everything. Does anyone know how to configure this? I've been stuck on it for a while.
Edit:
I tried doing it on production and it is not working. Here is more info:
My production.rb looks like this:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => ENV['DEFAULT_HOST'] }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
I have a heroku sendgrid add on. I have set the heroku config vars. In my registrations controller I merely added the line:
UserMailer.send_sign_up_email(#current_user).deliver
My mailer class looks like:
def send_sign_up_email(user)
#user = user
mail(to: #user.email, subject: "Welcome! #{#user.first_name}")
end
However, when I sign up on my website, the user gets added to the database but the email is not sending. Does anyone know why, or how I can debug?
I would suggest to remove all config for ActionMailer from your environment files (i.e. files under /config/environments), except following ones
# Don't care if the mailer can't send.
config.action_mailer.perform_caching = false
# Reference: http://stackoverflow.com/a/20770131/936494
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Then create an initializer /config/initializers/mail_config.rb and add following code to it:
TEST_ENVS = %w(test)
FILESYSTEM_ENVS = TEST_ENVS + %w(development)
# =========== DELIVERY METHOD SETTING
delivery_method = case Rails.env.to_sym
when :production, :staging, :experimental
:sendmail
when :test
:test
else
:smtp
end
ActionMailer::Base.delivery_method = delivery_method
# =========== SMTP SETTINGS
ENVS_TO_USE_GMAIL_CONFIG = %w(development test)
gmail_config = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: ENV['MAIL_USER_NAME'],
password: ENV['MAIL_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true
}
if :smtp == delivery_method
use_gmail_config = ENVS_TO_USE_GMAIL_CONFIG.include?(Rails.env)
smtp_settings = ( use_gmail_config ? gmail_config : {} )
ActionMailer::Base.smtp_settings = smtp_settings
end
# =========== DEFAULT URL OPTIONS
default_url_options_settings = Settings.default_url_options
host = default_url_options_settings.host
port = default_url_options_settings.port
protocol = default_url_options_settings.protocol
default_url_options = {}
default_url_options[:host] = host if host.present?
default_url_options[:port] = port if port.present?
default_url_options[:protocol] = protocol if protocol.present?
ActionMailer::Base.default_url_options = default_url_options
The Settings object is available as part of using config gem. If you do not want to use that gem for configuring env-specific values then you can just hard-code them in the initializer and try it.
My /config/settings.yml looks like
default_url_options:
host: ''
port: ''
protocol: ''
and my /config/settings/development.yml looks like
default_url_options:
host: 'localhost'
port: '3000'
Having a centralized mailer config helps in diagnosing mailer settings related issues in quick manner.
First try it for Gmail account and if it works you can be sure that sending email works. Just make sure in your Gmail account Less Secure Apps setting is enabled.

Action Mailer Not delivering Emails - MailCatcher and sendmail

I am using Rails 5. I have the following configurations in my config/environments/development.rb and config/environments/staging.rb.
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: "localhost", port: 1025 }
When I call the method that sends email, I get the following output.
Rendering mailer/consolidated_s3_storage_report.html.erb
Rendered mailer/consolidated_s3_storage_report.html.erb (0.4ms)
Mailer#consolidated_s3_storage_report: processed outbound mail in 17668.1ms
=> #<Mail::Message:86763960, Multipart: true, Headers: <From: mcds#sheridan.com>, <To: mcds.support#sheridan.com>, <Subject: 2017 July - S3 Storage Report>, <Mime-Version: 1.0>, <Content-Type: multipart/mixed; boundary="--==_mimepart_595c962cc36fb_1be1b2198436941"; charset=UTF-8>>
But email is not delivered to my gmail. The 'from' address is 'default from' from which all other emails are sent. Please clarify why my emails are not delivered.
You seem to be using Mailcatcher. Mailcatcher doesn't deliver messages, its to prevent messages from delivering to the actual To email addresses but lets you check of the message is framed correctly. Developers use Mailcatcher in development enviroments to see if the emails are rendered as they expect them to look like without spamming the To email holder.
All the emails that are sent to Mailcatcher can be viewed in a web interface. Visit http://localhost:1080 on your computer, you should be able to see all the emails you send so far from the development environment.
Following is the configuration for send mail to gmail:
config/environments/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "user#gmail.com", #your gmail id
:password => "password", #your gmail password
:authentication => "plain",
:enable_starttls_auto => true
}

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.

The recipient will be replaced with default from

This is an strange problem,
At first I could send my mail via mailgun,
But now the expected recipient bill.email will be replaced with me##{ENV['mailgun_domain']}
I totally have no idea. because the code not changed
class BillMailer < ActionMailer::Base
default from: "me##{ENV['mailgun_domain']}"
def confirm_bill(bill, cart)
mail(to: bill.email, cc: "poc7667#gmail.com")
end
end
I printed the mail object, everything looks fine
#<Mail::Message:70158682584200, Multipart: false, Headers: <From: balance#4am-lesson.com>, <To: test_123#gmail.com>, <Cc: test_123#gmail.com>, <Subject: hihi>, <Mime-Version: 1.0>, <Content-Type: text/html>>
But when I checked the mailgun log, my recipient was replaced with balance_coffe#sandboxc88b11b1bddf4594aad351f79e76d6ba.mailgun.org, it supposed be my default sent from, I thought it a mailgun bug
"message": {
"headers": {
"to": "balance_coffe#sandboxc88b11b1bddf4594aad351f79e76d6ba.mailgun.org",
"message-id": "20150503112244.C04DC5995993#Poc-MacBook-Pro-Retina-15-2013-late-2.local",
"from": "MAILER-DAEMON#Poc-MacBook-Pro-Retina-15-2013-late-2.local (Mail Delivery System)",
"subject": "Undelivered Mail Returned to Sender"
},
"attachments": [],
"recipients": [
"balance_coffe#sandboxc88b11b1bddf4594aad351f79e76d6ba.mailgun.org"
],
"size": 20919
},
"recipient": "balance_coffe#sandboxc88b11b1bddf4594aad351f79e76d6ba.mailgun.org",
"event": "accepted"
This is perfectly valid behavior. If you observe your from and subject attributes in your log, you'll see, your smtp server (OSX default) is trying to tell you about a failure mail delivery. This is what an SMTP server will do if it cannot deliver an email to the recipient.
To fix this, you'll need to configure SMTP setting for your rails app. Add the following in your config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:authentication => :plain,
:address => "smtp.mailgun.org",
:port => 587,
:domain => "MYDOMAIN.mailgun.org",
:user_name => "postmaster#MYDOMAIN.mailgun.org",
:password => "MYPASSWORD"
}
Replace :domain, :username and :password with your mailgun credentials.

Resources