Cloud9 and ActionMailer / Mailgun?

2019-08-02 12:03发布

问题:

I hope you can lend me a hand with this!

I used to develop locally, but I'm abroad and I'm using Cloud9 to work with some projects. Currently, It's giving me a hard time with Action Mailer.

My ActionMailer Initializer:

ActionMailer::Base.smtp_settings = {
  port:              '2525', 
  address:           'smtp.mailgun.org',
  user_name:         ENV['MAILGUN_SMTP_LOGIN'],
  password:          ENV['MAILGUN_SMTP_PASSWORD'],
  domain:            'app07ad98bdda3b4c469a24228512cffe5c.mailgun.org',
  authentication:    :plain,
  content_type:      'text/html'
}
ActionMailer::Base.delivery_method = :smtp

mailers/gun_mailer.rb

class GunMailer < ActionMailer::Base
  default from: "from@example.com"

  def welcome_email(user)
    @user = user
    @url  = 'http://example.com/login'
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end

end

views/gun_mailer/welcome_email.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to example.com, <%= @user.email %></h1>
    <p>
      You have successfully signed up to example.com,
      your username is: <%= @user.email %>.<br>
    </p>
    <p>
      To login to the site, just follow this link: <%= @url %>.
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

Console

u = User.first
GunMailer.welcome_email(u).deliver

I've set up the environment variables with figaro, and evrything seems to be correct... But the mail is never sent! I've heard that C9 has some ports blocked (587 being one of them), I've tried with 2587, 2525 (as other posters recommended), but It doesn't work!

回答1:

First, I would make sure that the following variables are actually being set within your mail.rb using the Rails console:

ENV['MAILGUN_SMTP_LOGIN']

ENV['MAILGUN_SMTP_PASSWORD']

Second, you should verify that the ports are not blocked by doing the following:

$ telnet smtp.mailgun.org 2525

$ telnet smtp.mailgun.org 587  

Note: The port isn't blocked if you see something similar to the following:

Trying 104.130.177.23...
Connected to smtp.mailgun.org.
Escape character is '^]'.
220 ak47 ESMTP ready

If one of the above ports are not blocked, then I would stop here, update mail initializer to use the unblocked port, and recheck your code. Otherwise, I would continue reading.

Third, if you have determined that both ports are blocked, then you'll need to use the HTTP API and I would read the documentation here:

https://documentation.mailgun.com/api-sending.html#sending

or

using a different hosting company like Heroku.com or DigitalOcean.com.

Well, I wish that the information above assists you in sorting out your issue.