0

So I'm nearly complete with setting up a mailer in my Rails app, but I cannot seem to understand one of the pieces of information on https://guides.rubyonrails.org/action_mailer_basics.html.

According to this tutorial, you can specify your own smtp_settings option, so that you can send mail like:

def welcome_email
    smtp_settings = {
        address:              'smtp.gmail.com',
        port:                 587,
        domain:               'example.com',
        user_name:            'myfromemail@example.com',
        password:             "myPassword",
        authentication:       :plain,
        enable_starttls_auto: true
    }

    mail(to: "destination@example.com",
        subject: "Test Notification",
        smtp_settings: smtp_settings
        )
end

but this doesn't actually work. The same example at the bottom uses delivery_method and passes over a hash just like I used in this example.

If I try to use the .deliver method on the mailer, this is the error I get:

Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25)

It's almost like it's completely ignoring the smtp settings that I passed to the mail function. What am I doing wrong here?

I've looked at examples from How to send emails with multiple, dynamic smtp using Actionmailer/Ruby on Rails, but this method does not work:

delivery_method.settings.merge!(smtp_settings)

because the delivery_method is actually :smtp and it's not possible.

The delivery_method option seems to work, but I need to pass additional stuff over like authentication, SSL/TLS, and even the port. Those don't seem to be offered by the delivery_method option.

Trying to implement one of the solutions on that post gives me this error:

MyMailer.delivery_method.settings.merge!(smtp_settings)
Traceback (most recent call last):
        1: from (irb):3
NoMethodError (undefined method `settings' for :smtp:Symbol)
LewlSauce
  • 5,326
  • 8
  • 44
  • 91

1 Answers1

1

For Dynamic mail server settings, you need to configure in the following way

instance_email = Mailer.welcome_email
config_settings = {address: 'xyz.com', port: 587}
instance_email.delivery_method.settings.merge! 
config_settings instance_email.deliver