0

I am trying to send an email using mail gem. But Unfortunately it is not working. This is my controller.

    def create
      fn = params["firstname"]
      ln = params["lastname"]
      email = params["email"]
      file = params["file"]
      summery = params["summery"]
      email_body = "Hello\n This is Your favorite website.\nA I want to personaly say hi."
      mail = Mail.new do
        from 'someone@gmail.com'
        to email
        subject "Saying Hi"
        body email_body
      end
      mail.add_file(filename: file.original_filename, content: File.read(file.tempfile.path)) unless file.nil?
      mail.deliver!
      render json: {message: "A bug has been created", success: true}, status: 201
    end

This code is producing this error

Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:

However when I am installing the mailcatcher and configure my controller to send the mail to mailcatcher, I can see the email in my mailcatcher UI.

Mail.defaults do
 delivery_method :smtp, address: "localhost", port: 1025
end

Also I have add this two lines to my config/environment/development.rb

config.action_mailer.raise_delivery_errors = true 
config.action_mailer.perform_deliveries = true

From my searches I saw that some people are mentioning that dont send email on development mode, however on this case I really want to test the full capability.

Update

As @Uzbekjon and @SimoneCarletti suggested I change my code to use the ActionMailer. I created the a file in app/mailer/ and I am calling that from my controller.

  def create
    fn = params["firstname"]
    ln = params["lastname"]
    email = params["email"]
    file = params["file"]
    summery = params["summery"]
     email_body = "Hello\n This is Your favorite website.\nA I want to personaly say hi."
    WelcomeMailer.welcome(fn, ln, email, file, email_body).deliver_now
    render json: {message: "An Email has been send", success: true}, status: 201
  end

and This is my mailer

class WelcomeMailer < ActionMailer::Base
  default from: "someone@yahoo.com"

  def welcome(first_name, last_name, email, file, email_body)
    attachments["#{file.original_filename}"] = File.read("#{file.tempfile.path}")
 mail(
   to: email, 
   subject: 'Welcome to My Awesome Site', 
   body: email_body
 )
  end
end

However I am still getting the same error.

Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:

Answer

So I found the solution. Yes you need to use the ActionMailer. After that you need to go to the config/environments/development.rb , and modify and add these lines:

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp
  # SMTP settings for gmail
  config.action_mailer.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :user_name            => "YOUR EMAIL",
    :password             => "YOUR Password",
    :authentication       => "plain",
    :enable_starttls_auto => true
  }

Also If Gmail complained about this:

Net::SMTPAuthenticationError - 534-5.7.9 Application-specific password required

Go to this link and let less secure application access Gmail.

Other configurations are available for other services like Yahoo. Just Google it.

click
  • 447
  • 1
  • 7
  • 25
  • Why are you using the Mail gem directly, instead of the ActiveRecord mailer library that is part of Rails? – Simone Carletti Aug 11 '16 at 14:50
  • @SimoneCarletti that is my next step if no one could figure this one out, do you suggest the Active Record Mailer over the gem? – click Aug 11 '16 at 14:54
  • Yes, you should definitely use `ActionMailer` (and not `ActiveRecord` which is the ORM). FYI `ActionMailer` wraps the `mail` gem. – Simone Carletti Aug 11 '16 at 14:55
  • @SimoneCarletti I have added the code with the ActionMailer, however I am getting a same error. – click Aug 11 '16 at 20:59
  • Do I need to run a SMTP server or something. Sorry I am not really familiar with the process. – click Aug 11 '16 at 21:01

2 Answers2

1

Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:

Looks like mail gem is trying to connect to your local smtp server on port 25. Most probably you don't have the service running and receiving connections on port 25.

To solve, install and run sendmail or postfix on your machine.

PS. Use ActionMailer.

Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
  • I have updated the code with the ActionMailer but I am getting a same error. – click Aug 11 '16 at 21:00
  • As I wrote in my answer, the problem is that you don't have "mail server" running on your local machine. To solve it, you need to install one. For example `sendmail` or `postfix`. – Uzbekjon Aug 12 '16 at 04:59
0

You don't have a mail server running on port 25. You can install postfix and start the server using

sudo postfix start

And then modify the settings in config/environments/development.rb

config.action_mailer.delivery_method = :sendmail

Hope this helps.

Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44