如何接收 rails 用户的邮件?

How to receive an email from users in rails?

我有一个表单,允许用户输入他们的姓名、电子邮件地址、主题和消息。当用户点击发送时,消息应该发送给我(管理员)。 我的开发配置下有这段代码...

  config.action_mailer.delivery_method = :smtp
  # SMTP settings for gmail
  config.action_mailer.smtp_settings = {
     :address              => @user.email,
     :port                 => 587,
     :user_name            => ENV['sys.questdentalusa@gmail.com'],
     :password             => ENV['passwordhere'],
     :authentication       => 'plain',
     :enable_starttls_auto => true
 }

以及我的 user_mailer

下的这段代码
def welcome_email(user)
  @user = user
  mg_client = Mailgun::Client.new ENV['api_key']
  message_params = {:from    => ENV[@user.email],
                  :to      => 'sys.questdentalusa@gmail.com',
                  :subject => @user.subject,
                  :text    => @user.text}
  mg_client.send_message ENV['domain'], message_params
end

它不会发送消息。就好像没有执行一样。 规则是,不应涉及任何模型。 例如,您有一个现有的 gmail 帐户并写了一封邮件发送给我。我应该从你输入的 gmail 帐户收到你的消息。

有两件事你的 developer configmessage_params 看起来不对,

message_params 中::from => ENV[@user.email] 应该像 @user.email

smtp_settings 中::address => @user.email, 就像 "smtp.mailgun.org"。在 here

查看更多 smtp_settings

我得到答案已经有一段时间了,我只是决定不妨在这里分享一下。这就是我在 development.rb

中所做的
  config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: "gmail.com",
  user_name: "sys.questdentalusa@gmail.com",
  password: "passwordhere",
  authentication: :plain,
  enable_starttls_auto: true
}

这是我在 Mailer 下得到的

class MessageMailer < ActionMailer::Base
  default from: "sys.questdentalusa@gmail.com"
  default to: "questdentalusa@gmail.com"

  def new_message(contact)
    @contact = contact

    mail subject: 'Inquiry from website: ' + @contact[:subject]
  end
end

我在 new_message.text.erb

下得到了这个
Name: <%= @contact[:name] %>
Email: <%= @contact[:email] %>
Message: <%= @contact[:content] %>

这是在我的控制之下

class HomeController < ApplicationController
  skip_before_filter  :verify_authenticity_token
  def send_mail
    if MessageMailer.new_message(contact_params).deliver
      redirect_to contact_path
      flash[:notice] = 'Your messages has been sent.'
    end
  end
  private
  def contact_params
    params.require(:contact).permit(:name, :email, :subject, :content)
  end
end