Bronze
Bronze

Reputation: 143

Rails 4.2 Mailer Problems - Can't get mailer to work

I don't really know what is wrong. I have a RoR server that I'm going to have periodically send out notifications to users via email. The way I'm calling the rails code currently to make that happen is:

$ bundle exec rails r "ProjectDueMailer.test_email"

This is the mailer code:

class ProjectDueMailer < ApplicationMailer
  default from: '<email>@gmail.com'

  def test_email
    mail(to: '<email>@gmail.com', subject: "This is a test!")
    puts "email sent"
  end
end

Obviously replace <email> with the correct email. Similarly, the configuration is:

  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    port: 587,
    user_name: '<email>@gmail.com',
    password: '<password>',
    authentication: "plain",
    enable_starttls_auto: true }

Whenever I run the rails runner like this, nothing happens. It sits for a moment, and then I get the next shell line. If I change def test_email to def self.test_email, I get the error of:

undefined method 'mail' for ProjectDueMailer:Class

I really am at a loss here. I have test_email.html|text.erb files, I generated the mailer using rails g mailer ProjectDueMailer so there's the ApplicationMailer class. What am I doing wrong?

Upvotes: 0

Views: 61

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52367

To send the email, you should call deliver_now method:

ProjectDueMailer.test_mail.deliver_now

Upvotes: 1

Related Questions