title: How to send an email with mail gem in ruby on rails
body: <p>I am trying to send an email using <a href="https://github.com/mikel/mail" rel="nofollow">mail gem</a>. But Unfortunately it is not working.
  This is my controller.</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>This code is producing this error</p>
  
  <blockquote>
    <p>Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:</p>
  </blockquote>
  
  <p>However when I am installing the <a href="https://github.com/sj26/mailcatcher" rel="nofollow">mailcatcher</a> and configure my controller to send the mail to mailcatcher, I can see the email in my mailcatcher UI.</p>
  
  <pre><code>Mail.defaults do
   delivery_method :smtp, address: "localhost", port: 1025
  end
  </code></pre>
  
  <p>Also I have add this two lines to my config/environment/development.rb</p>
  
  <pre><code>config.action_mailer.raise_delivery_errors = true 
  config.action_mailer.perform_deliveries = true
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>Update</strong></p>
  
  <p>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.</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>and This is my mailer</p>
  
  <pre><code>class WelcomeMailer &lt; 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
  </code></pre>
  
  <p>However I am still getting the same error.</p>
  
  <blockquote>
    <p>Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:</p>
  </blockquote>
  
  <p><strong>Answer</strong></p>
  
  <p>So I found the solution. Yes you need to use the ActionMailer. After that you need to go to the <em>config/environments/development.rb</em> , and modify and add these lines:</p>
  
  <pre><code>  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              =&gt; "smtp.gmail.com",
      :port                 =&gt; 587,
      :user_name            =&gt; "YOUR EMAIL",
      :password             =&gt; "YOUR Password",
      :authentication       =&gt; "plain",
      :enable_starttls_auto =&gt; true
    }
  </code></pre>
  
  <p>Also If Gmail complained about this:</p>
  
  <blockquote>
    <p>Net::SMTPAuthenticationError - 534-5.7.9 Application-specific password required</p>
  </blockquote>
  
  <p>Go to this <a href="https://www.google.com/settings/security/lesssecureapps" rel="nofollow">link</a> and let less secure application access Gmail.</p>
  
  <p>Other configurations are available for other services like Yahoo. Just Google it.</p>