Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top