Reputation: 199
I want to write a script that will connect to my email and send emails for me from my laptop terminal using ActionMailer. I'm testing my connectivity to my email server and can't seem to get it to work. For some reason it's still trying to connect to localhost port 25 even though I specified my smtp server and port.
My code:
action.rb
require 'action_mailer'
mailer = ActionMailer::Base.new
# check settings:
mailer.delivery_method = :smtp
mailer.smtp_settings = {
address: 'auth.smtp.1and1.co.uk',
domain: '1and1.com',
port: 587,
authentication: 'login',
user_name: 'MY EMAIL ADDRESS',
password: 'MY PASSWORD',
enable_starttls_auto: true
}
# send mail:
mailer.mail(
from: 'MY EMAIL ADDRESS',
to: 'MY OTHER EMAIL ADDRESS TO TEST WITH',
subject: 'test',
body: "Hello, you've got mail!"
).deliver
For some reason I get the following output:
My-MBP:auto_emailer user$ ruby action.rb
Traceback (most recent call last):
18: from action.rb:22:in `<main>'
17: from /Users/user/.rvm/gems/ruby-2.6.3/gems/mail-2.7.1/lib/mail/message.rb:260:in `deliver'
16: from /Users/user/.rvm/gems/ruby-2.6.3/gems/actionmailer-6.0.3.2/lib/action_mailer/base.rb:587:in `deliver_mail'
15: from /Users/user/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.3.2/lib/active_support/notifications.rb:180:in `instrument'
14: from /Users/user/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.3.2/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
13: from /Users/user/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.3.2/lib/active_support/notifications.rb:180:in `block in instrument'
12: from /Users/user/.rvm/gems/ruby-2.6.3/gems/actionmailer-6.0.3.2/lib/action_mailer/base.rb:589:in `block in deliver_mail'
11: from /Users/user/.rvm/gems/ruby-2.6.3/gems/mail-2.7.1/lib/mail/message.rb:260:in `block in deliver'
10: from /Users/user/.rvm/gems/ruby-2.6.3/gems/mail-2.7.1/lib/mail/message.rb:2159:in `do_delivery'
9: from /Users/user/.rvm/gems/ruby-2.6.3/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:100:in `deliver!'
8: from /Users/user/.rvm/gems/ruby-2.6.3/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:109:in `start_smtp_session'
7: from /Users/user/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/net/smtp.rb:518:in `start'
6: from /Users/user/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/net/smtp.rb:548:in `do_start'
5: from /Users/user/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/timeout.rb:103:in `timeout'
4: from /Users/user/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/timeout.rb:93:in `block in timeout'
3: from /Users/user/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/net/smtp.rb:549:in `block in do_start'
2: from /Users/user/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/net/smtp.rb:539:in `tcp_socket'
1: from /Users/user/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/net/smtp.rb:539:in `open'
/Users/user/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/net/smtp.rb:539:in `initialize': Connection refused - connect(2) for "localhost" port 25 (Errno::ECONNREFUSED)
Upvotes: 2
Views: 535
Reputation: 944
You need to specify the delivery_method
and smtp_settings
options on the class level. Read more at https://api.rubyonrails.org/v6.0.3/classes/ActionMailer/Base.html
require 'action_mailer'
# Specify settings
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'auth.smtp.1and1.co.uk',
domain: '1and1.com',
port: 587,
authentication: 'login',
user_name: 'MY_EMAIL_ADDRESS',
password: 'MY_PASSWORD',
enable_starttls_auto: true
}
# Send email
ActionMailer::Base.new.mail(
from: 'MY_EMAIL_ADDRESS',
to: 'ANOTHER_EMAIL_ADDRESS',
subject: 'Testing from Ruby script',
body: "Hello, you've got mail!"
).deliver
Upvotes: 2