Mailgun 设置但收到两个错误 Rails

Mailgun setup but receiving two errors with Rails

设置 Mailgun 后,我收到两个目前无法解决的错误。以下是与它们相关的代码错误:

Mailgun::CommunicationError(404 找不到资源):

app/mailers/user_mailer.rb:11:in introduction_email' app/controllers/articles_controller.rb:29:in create'

def introduction_email(user)
    @user = user
    mg_client = Mailgun::Client.new ENV['api_key']
    message_params = {:from   => ENV['PIP_username'],
              :to     => @user.email,
              :subjet => "Sample Mail using Mailgun API.",
              :text   => "This mail is sent using Mailgun API via mailgun-ruby."}
    #The line below is the line of concern for the second error.
    mg_client.send_message ENV['domain'], message_params 
end


def create 
    @article = Article.create(article_params)
    @article.user = current_user
    if @article.save
      #The line below is the line of concern for the second error.
      UserMailer.introduction_email(@article.user).deliver_now 
      flash.notice = "Article #{@article.title} has created!"
      redirect_to community_path
    else
      flash.notice = "Try Again!"
end

如果您碰巧看到任何问题、有任何想法或需要更多代码,请告诉我 =) 希望了解这些问题导致问题的原因并解决它们,谢谢!

编辑

由于我在编写教程时所做的更改,我意识到我的邮件程序在 UserMailer 中,但我将控制器信息放在 articles_controller 中。我已经切换,创建了一个 ArticleMailer 并替换了那里的代码,但仍然是同样的问题。

如果您使用 mailgun_rails gem 并在您的环境配置中设置 config.action_mailer.delivery_method = :mailgun,您应该能够以 Rails 默认方式发送电子邮件:

class UserMailer < ActionMailer::Base
  def introduction_email(user)
    @user = user
    mail({
      :from    => ENV['PIP_username'],
      :to      => @user.email,
      :subject => "Sample Mail using Mailgun API.",
      :text    => "This mail is sent using Mailgun API via mailgun-ruby."
    })
  end
end

然后:

UserMailer.introduction_email(@article.user).deliver

编辑 您还需要从 Mailgun 设置以下环境变量才能使 gem 正常工作:

MAILGUN_API_KEY
MAILGUN_SMTP_LOGIN
MAILGUN_SMTP_PASSWORD
MAILGUN_SMTP_PORT
MAILGUN_SMTP_SERVER