3

I'm using Action Mailer to send a daily email, but without Rails. I've also looked at other questions here: Action Mailer 3 without Rails, ActionMailer and Ramaze

Here's my code:

require 'action_mailer'

class Mailer < ActionMailer::Base
  def daily_names_email(names,subject="test daily mail",to = "test@domain.com")
  @names = "test names"
  mail(
      :to      => to,
      :from    => "me@domain.com",
      :subject => subject
    ) do |format|
    format.text
    format.html
    end
  end
end

Mailer.raise_delivery_errors = true
Mailer.delivery_method = :smtp
Mailer.smtp_settings = {
 :address   => "smtp.gmail.com",
 :port      => 465,
 :domain    => "google",
 :authentication => :plain,
 :user_name      => "me@domain.com",
 :password       => "*****",
 :enable_starttls_auto => true
}
Mailer.view_paths = File.dirname(__FILE__)
Mailer.logger = Logger.new(STDOUT)

email = Mailer.daily_names_email('hello')

puts email
email.deliver

Here's the Error output:

Date: Thu, 19 Jul 2012 08:46:18 +0800
From: me@domain.com
To: me@domain.com

Message-ID: <500758da4dbda_151082d87c10766@ubuntu.mail>
Subject: test daily mail
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_500758da4a289_151082d87c104fb";
 charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_500758da4a289_151082d87c104fb
Date: Thu, 19 Jul 2012 08:46:18 +0800
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <500758da4c2f6_151082d87c10515@ubuntu.mail>

/usr/lib/ruby/1.9.1/net/protocol.rb:146:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error)
from /usr/lib/ruby/1.9.1/net/protocol.rb:140:in `rbuf_fill'
from /usr/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
from /usr/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
from /usr/lib/ruby/1.9.1/net/smtp.rb:929:in `recv_response'
from /usr/lib/ruby/1.9.1/net/smtp.rb:552:in `block in do_start'
from /usr/lib/ruby/1.9.1/net/smtp.rb:939:in `critical'
from /usr/lib/ruby/1.9.1/net/smtp.rb:552:in `do_start'
from /usr/lib/ruby/1.9.1/net/smtp.rb:519:in `start'
from /var/lib/gems/1.9.1/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:144:in `deliver!'
from /var/lib/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:2034:in `do_delivery'
from /var/lib/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:229:in `block in deliver'
from /var/lib/gems/1.9.1/gems/actionmailer-3.2.6/lib/action_mailer/base.rb:415:in `block in deliver_mail'
from /var/lib/gems/1.9.1/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `block in instrument'
from /var/lib/gems/1.9.1/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /var/lib/gems/1.9.1/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `instrument'
from /var/lib/gems/1.9.1/gems/actionmailer-3.2.6/lib/action_mailer/base.rb:413:in `deliver_mail'
from /var/lib/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:229:in `deliver'
from mailer.rb:34:in `<main>''

Any help would be appreciated! Thanks in advance!

Community
  • 1
  • 1
ringular
  • 65
  • 1
  • 10

1 Answers1

1

Unless its a typo, the "domain" parameter is wrong.

:domain => 'google' should be :domain => 'yourdomain.com'

Your code given below, with the correct domain works fine

require 'action_mailer'

class Mailer < ActionMailer::Base
  def daily_names_email(names,subject="test daily mail",to = "recipient@domain.com")
  mail(
      :to      => to,
      :from    => "you@yourdomain.com",
      :subject => subject
    ) do |format|
    format.text
    format.html
    end
  end
end

Mailer.raise_delivery_errors = true
Mailer.delivery_method = :smtp
Mailer.smtp_settings = {
 :address   => "smtp.gmail.com",
 :port      => 587,
 :domain    => "yourdomain.com",
 :authentication => :plain,
 :user_name      => "you@yourdomain.com",
 :password       => "*****",
 :enable_starttls_auto => true
}
Mailer.view_paths = File.dirname(__FILE__)
Mailer.logger = Logger.new(STDOUT)

email = Mailer.daily_names_email('hello').deliver

puts email
Anand Shah
  • 14,575
  • 16
  • 72
  • 110
  • Hey @anand. Thanks for your help. I've tried the above & get the following responses For these settings: `:port => 465, :domain => "domain.com", :authentication => :login,` or `:port => 465, :domain => "domain.com", :authentication => :plain,` gets me this response: `/usr/lib/ruby/1.9.1/net/protocol.rb:146:in 'rescue in rbuf_fill': Timeout::Error(Timeout::Error)from/usr/lib/ruby/1.9.1/net/protocol.rb:140:in 'rbuf_fill' ` – ringular Jul 19 '12 at 05:21
  • With this settings: ` :port => 587, :domain => "domain.com", :authentication => :login, :port => 465, :domain => "domain.com", :authentication => :plain,` I get: `/usr/lib/ruby/1.9.1/net/smtp.rb:960:in 'check_auth_response': 535-5.7.1 Username and Password not accepted. Learn more at (Net::SMTPAuthenticationError) from /usr/lib/ruby/1.9.1/net/smtp.rb:737:in 'auth_plain' from /usr/lib/ruby/1.9.1/net/smtp.rb:729:in 'authenticate' from /usr/lib/ruby/1.9.1/net/smtp.rb:564:in 'do_start' from /usr/lib/ruby/1.9.1/net/smtp.rb:519:in 'start'` – ringular Jul 19 '12 at 05:24
  • To add on, I realize the port no should be 587 (IMAP) like you specified & not 465 (POP). However, when I use port 587 I get an authentication error. I'm not sure why – ringular Jul 19 '12 at 06:37
  • That is quite strange, as I am using your code and just changing the domain parameter works for me, and yes also the port parameter. As per the messages I see you are using 1.9.1, if you are using RVM to manage Ruby version's can you give your code a try with different version of Ruby, say 1.9.2 or higher? – Anand Shah Jul 19 '12 at 06:41
  • Hey anand, thank you for looking into this again. The error i now get is `/home/ritesh/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/smtp.rb:197:in default_ssl_context': uninitialized constant Net::SMTP::OpenSSL (NameError) from /home/ritesh/.rvm/rubies/ruby-1.9.3-194/lib/ruby/1.9.1/net/smtp.rb:345:in enable_starttls_auto' from /home/ritesh/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:130:in 'deliver!' ` – ringular Jul 19 '12 at 08:17