1

使用 Gmail 发送电子邮件有效,但 Mailapp 无效。这是发生了什么:

GMAIL 设置:它的工作!

appname/config/initializers/setup_mail.rb RailsCast

ActionMailer::Base.smtp_settings ={
    :address                    => "smtp.gmail.com",
    :port                       => 587,
    :user_name                  => "gmailusername",
    :password                   => "gmailpassword",
    :authentication             => "plain", 
    :domain                     => "http://localhost:3000",
    :enable_starttls_auto       => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"

appname/app/mailers/newsletter_mailer.rb

class NewsletterMailer < ActionMailer::Base
  default from: "myemail@gmail.com" #for gmai
  def registration_confirmation(newsletter)
    @newsletter = newsletter
    mail(:to => (newsletter.email), :Subject => "Thank you for signing up with us")

  end
end

appname/app/controllers/newsletters_controller.rb

def create
    @newsletter = Newsletter.new(params[:newsletter])

    respond_to do |format|
      if @newsletter.save
        NewsletterMailer.registration_confirmation(@newsletter).deliver
        format.html { redirect_to @newsletter, notice: 'Newsletter was successfully created.' }
        format.json { render json: @newsletter, status: :created, location: @newsletter }
      else
        format.html { render action: "new" }
        format.json { render json: @newsletter.errors, status: :unprocessable_entity }
      end
    end
  end

appname/app/views/newsletter_mailer/registration_confirmation.html.erb

Hello <%= @newsletter.name %>,<br>

Thank you for signing up!<br><br>

Thank you.

MAILGUN 设置:不工作??

appname/config/initializers/setup_mail.rb

ActionMailer::Base.smtp_settings ={
:address                    => "smtp.mailgun.org",
:port                       => 587,
:user_name                  => "postmaster@sandbox481.mailgun.org",
:password                   => "mailgunpassword",
:authentication             => :plain, 
:domain                     => "sandbox481.mailgun.org",
:enable_starttls_auto       => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000" #tried sandbox481.mailgun.org too without any luck.

appname/app/mailers/newsletter_mailer.rb

class NewsletterMailer < ActionMailer::Base
  default from: "postmaster@sandbox481.mailgun.org"
  def registration_confirmation(newsletter)
    @newsletter = newsletter
    mail(:to => (newsletter.email), :Subject => "Thank you for signing up with us")

  end
end

appname/app/controllers/newsletters_controller.rb

def create
    @newsletter = Newsletter.new(params[:newsletter])

    respond_to do |format|
      if @newsletter.save
        NewsletterMailer.registration_confirmation(@newsletter).deliver
        format.html { redirect_to @newsletter, notice: 'Newsletter was successfully created.' }
        format.json { render json: @newsletter, status: :created, location: @newsletter }
      else
        format.html { render action: "new" }
        format.json { render json: @newsletter.errors, status: :unprocessable_entity }
      end
    end
  end

appname/app/views/newsletter_mailer/registration_confirmation.html.erb

Hello <%= @newsletter.name %>,<br>

Thank you for signing up!<br><br>

Thank you.

这是我从日志(终端)得到的

Rendered newsletter_mailer/registration_confirmation.html.erb (0.1ms)

Sent mail to myemail@gmail.com (30005ms)
Date: Fri, 15 Nov 2013 15:47:19 -0600
From: postmaster@sandbox481.mailgun.org
To: myemail@gmail.com
Message-ID: <528696672b8de_be625a2854253423@ubuntu-VirtualBox.mail>
Subject: Registration confirmation
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Hello P Dahal,<br>

Thank you for signing up!<br><br>

Thank you.
Redirected to http://localhost:3000/newsletters/10
Completed 302 Found in 30028ms (ActiveRecord: 14.2ms)

但是我的收件箱(myemail@gmail.com)没有收到那封电子邮件。

4

0 回答 0