سؤال

I'm using Ruby on Rails, could you teach me how to use Mandrill to send and receive mail.

My problem is when user input text and press submit, I want message to be sent to my mail. I have read documentation, but I don't understand it.

This is my development.rb, same as production.rb

ActionMailer::Base.smtp_settings = {
    :port =>           '587',
    :address =>        'smtp.mandrillapp.com',
    :user_name =>      'myemail@gmail.com',
    :password =>       's82SlRM5dPiKL8vjrJfj4w',
    :domain =>         'heroku.com',
    :authentication => :plain
}
ActionMailer::Base.delivery_method = :smtp

user_mailer.rb:

class UserMailer < ActionMailer::Base
  default from: "from@example.com"
  def welcome
    mail(to: "morumotto26@gmail.com", subject: "Welcome", body: "Have a nice day")
  end 
end

and in controller:

def index
  UserMailer.welcome.deliver
end

My logs file look like this:

Started GET "/users/" for 127.0.0.1 at 2014-01-21 16:14:10 +0700
Processing by UsersController#index as HTML

Sent mail to morumotto26@gmail.com (2008.0ms)
Date: Tue, 21 Jan 2014 16:14:10 +0700

From: myemail@gmail.com

To: morumotto26@gmail.com

Message-ID: <52de3a62c89b2_5e8c9ea1881631@tnkjapan10.mail>

Subject: Welcome

Mime-Version: 1.0

Content-Type: text/plain;

 charset=UTF-8

Content-Transfer-Encoding: 7bit



Have a nice day
  Rendered text template (0.0ms)
Completed 200 OK in 2018ms (Views: 1.0ms | ActiveRecord: 0.0ms)
هل كانت مفيدة؟

المحلول

First of all you will need to create account on the mandrill.com.

After log in, select type of integration: SMTP or API. SMTP will suite you in most of the cases.

Click SMTP, and create you API key.

Then in your development.rb or production.rb file add these lines:

config.action_mailer.smtp_settings = {
  :port =>           '587',
  :address =>        'smtp.mandrillapp.com',
  :user_name =>      ENV['MANDRILL_USERNAME'],
  :password =>       ENV['MANDRILL_APIKEY'],
  :domain =>         'domain.com',
  :authentication => :plain
}

That's basically all. Now you can send email with Mandrill.

EDIT

Also try to add to your to your environment files these line to perform deliveries and raise delivery errors if any. Also add your default_url_options - in development localhost:3000, in production heroku.com

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: "localhost:3000" }

Restart your app before testing

EDIT 2

If you want ActionMailer to send email on submit button click, then you will need to move UserMailer.welcome.deliver to create action of the respective controller.

def create
   if @object.save
     UserMailer.welcome.deliver
   else
     render "new"
   end
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top