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
Related
I followed the recommendation here from Sujoy Gupta to use smtp to send emails on rails. I get no error on the console but the emails never seem to have reached ses: The statistics there show no email sent/received and I never got them in my inbox either.
I added this to my config/environments/development.rb/test.rb/production.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "email-smtp.us-west-2.amazonaws.com",
:port => 587,
:user_name => "...", # My SMTP user here.
:password => "...", # My SMTP password here.
:authentication => :login,
:enable_starttls_auto => true
}
and app/mailers/UserMailer.rb:
class UserMailer < ApplicationMailer
default from: "user#hotmail.com"
def test(email)
mail(to: email, subject: 'ses test')
end
end
Here is what printed in the console:
UserMailer.test("user#hotmail.com").deliver_now
Rendered user_mailer/test.html.erb within layouts/mailer (0.1ms)
UserMailer#test: processed outbound mail in 16.2ms
Sent mail to user#hotmail.com (769.1ms)
Date: Wed, 11 Nov 2015 10:26:33 -0800
From: user#hotmail.com
To: user#hotmail.com
Message-ID: <56438859b7ee2_a6503fe78cc601fc19958#10ddb1e504ea.ant.amazon.com.mail>
Subject: ses test
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<body>
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
A quick brown fox jumped over the lazy dog.
</body>
</html>
</body>
</html>
=> #<Mail::Message:70263775568080, Multipart: false, Headers: <Date: Wed, 11 Nov 2015 10:26:33 -0800>, <From: user#hotmail.com>, <To: user#hotmail.com>, <Message-ID: <56438859b7ee2_a6503fe78cc601fc19958#10ddb1e504ea.ant.amazon.com.mail>>, <Subject: ses test>, <Mime-Version: 1.0>, <Content-Type: text/html>, <Content-Transfer-Encoding: 7bit>>
Following the java documentation here, I was able to send an email successfully using the same host and smtp username/password.
Any idea what I might have wrong? Where can I find more logs to dig more into it?
Thanks!
Turns out I just needed to close and reopen the rails console. For some reason reloading it is not enough.
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!
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
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.
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.