Search code examples
ruby-on-railsruby-on-rails-4smtpgmail

Trying to send email with Gmail STP (Rails)


I'm trying to send some emails from my web application. Here is my configuration:

//config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address:              'smtp.gmail.com',
port:                 587,
domain:               'domain.com',
user_name:            'myusername',
password:             'mypassword',
authentication:       'plain',
enable_starttls_auto: true  } 

// mailers/contact_mailer
class ContactMailer < ApplicationMailer
default from: '[email protected]'

def send_email()
    mail(to: '[email protected]', subject: 'Welcome to My Awesome Site')
end
end 

// views/contact_mailer/send_email.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, name</h1>
<p>
<p>Thanks for joining and have a great day!</p>
</body>
</html> 

So to test it, I ran this command from the Rails command:

ContactMailer.send_email

And I'm getting this result:

irb(main):003:0> ContactMailer.send_email
Rendered contact_mailer/send_email.html.erb within layouts/mailer (0.1ms)

ContactMailer#send_email: processed outbound mail in 10.4ms
=> #<Mail::Message:69980432504260, Multipart: false, Headers: <From:    [email protected]>, <To: [email protected]>, <Subject: Welcome   to My Awesome Site>, <Mime-Version: 1.0>, <Content-Type: text/html>>
irb(main):004:0> 

Unfortunately, after checking my email, I don't receive the email


Solution

  • mail only generates a mailer object. You need to append something like deliver_now for it to actually send. For example:

    ContactMailer.send_email.deliver_now