Richard Watkins
Richard Watkins

Reputation: 41

Action mailer not sending/receiving email rails 5

I'm trying to create a simple blog (using rails 5) and want to send email updates when to users when I post something new. I'm trying to use actionmailer and I'm using devise to register users. I'm trying to set it up with just the first user initially.

When I look at my local server the email appears to be sending but it's not received. Any advice would be very welcome I've been stuck a while.

    UserNotifierMailer#sample_email: processed outbound mail in 410.4ms
Sent mail to [email protected] (853.2ms)
Date: Wed, 27 Jul 2016 12:05:33 +0100
From: [email protected]
To:[email protected]
Message-ID: <5798957d9de31_ae813ff962abfe1466438@Unknown-7c-d1-c3-78-04-d2.home.mail>
Subject: Sample Email
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5798957d951e2_ae813ff962abfe1466312";
 charset=UTF-8
Content-Transfer-Encoding: 7bit

My posts controller looks like this:

def create
    @user = User.first
    @post = current_user.posts.build(post_params)
    if @post.save
      flash[:success] = "Your post has been created!"
      UserNotifierMailer.sample_email(@user).deliver
      redirect_to posts_path
    else
      flash[:alert] = "Your new post couldn't be created!  Please check the form."
      render :new
    end
  end

My mailer looks like this:

class UserNotifierMailer < ApplicationMailer
  default from: "[email protected]"
  def sample_email(user)
    @user = user
    @url = 'http://www.google.com'
    mail(to: @user.email, subject: 'Sample Email')
  end
end

and in my development.rb I have these settings:

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "gmail.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: "my_gmail_username",
    password: "my_gmail_password"
  }

Upvotes: 4

Views: 5056

Answers (2)

Anh Bui
Anh Bui

Reputation: 11

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
...
config.action_mailer.delivery_method = :smtp

I don't config any of the above and it still works.

config.action_mailer.default_url_options = { host: 'localhost:3000' }

and have to .deliver_later or .deliver_now, .deliver not work

Upvotes: 0

Mukesh
Mukesh

Reputation: 931

In your configured Gmail account:

  1. Go to Account Settings Than

  2. Go to Sign-in & security, followed by Connected apps & sites

  3. At last in Allow less secure apps ... Check it.

Check Once And Try

Upvotes: 4

Related Questions