Ruby on Rails End of File with SMTP - ruby-on-rails

Apologies if the answer is out there, but in the many similar posts I've browsed, I haven't found the answer I'm looking for.
I've inherited a Ruby on Rails application, and it recently began failing to send emails. From what I can gather, this is due to an smtp failure.
I want to send emails from "do_not_reply#mydomain.com" using "myaccount#gmail.com" for the SMTP settings.
In .../config/environments/production.rb I have
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => '<myaccount#gmail.com>'
:password => '<mygmailpassword>'
}
and in .../app/models/ I have a file called user_notifier.rb which contains
class UserNotifier < ActionMailer::Base
def signup_notification(user)
setup_email(user)
#subject += 'Please activate your new account'
#body[:url] = "<mydomain.com>:8080/activate/#{user.activation_code}"
end
def activation(user)
setup_email(user)
#subject += 'Your account has been activated'
#body[:url] = "<mydomain.com>:8080"
end
def reset_notification(user)
setup_email(user)
#subject += 'Link to reset your password'
#body[:url] = "<mydomain.com>:8080/reset_password/#{user.reset_password_code}"
end
def login_reminder(user)
setup_email(user)
#subject += 'Login Reminder'
#body[:url] = "<mydomain.com>:8080"
end
protected
def setup_email(user)
#recipients = "#{user.email}"
#from = "<do_not_reply#mydomain.com>"
#subject = "<subject>"
#sent_on = Time.now
#body[:user] = user
bcc ["<myaccount#gmail.com>"]
end
end
All of this code once worked, so I'm not sure what has changed. As I write this, I'm realizing that the sudden failure might have corresponded to some maintenance on the network, so I don't know how that might affect things.
EDIT: Added the entire UserNotifier class as requested in the comments

Well, I actually managed to solve this one myself.
I needed to add the :domain option in .../config/environments/production.rb
Why it once worked without :domain I still don't know, but I'll take just having the functional product.
The working setup was
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:domain => "gmail.com",
:user_name => '<myaccount#gmail.com>'
:password => '<mygmailpassword>'
}

Related

Rails 4 + ActionMailer: ":from" is filled with incorrect email address

I am fighting with setting up the "from" information that is shown in the email in header.
It still shows incorrect email address.
In the mailer class, I manually set the default email address:
class NotificationMailer < ActionMailer::Base
default content_type: "text/html", :from => 'default#email.com'
I also specified this in the method for sending the email(s):
def welcome_email(user)
#user = user
mail(to: #user.email, subject: "Welcome!", from: 'default#email.com')
end
But when I receive this email, it's from my-personal-email#gmail.com. I searched through the Rails app where is this email address set up and it's here:
config/initializers/setup_email.rb:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "logistadvise.com",
:user_name => "my-personal-email#gmail.com",
:password => "mypassword",
:authentication => "plain",
:enable_starttls_auto => true
}
Emails are sent out well, but worries me that I am unable to properly set the "from" header.
How to do that?
Thank you.

How to receive an email from users in rails?

I have a form that allows users to enter their names, email add, subject and message. When the user hits SEND, the message should be sent to me(admin).
I have this code under my development config...
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => #user.email,
:port => 587,
:user_name => ENV['sys.questdentalusa#gmail.com'],
:password => ENV['passwordhere'],
:authentication => 'plain',
:enable_starttls_auto => true
}
and this code under my user_mailer
def welcome_email(user)
#user = user
mg_client = Mailgun::Client.new ENV['api_key']
message_params = {:from => ENV[#user.email],
:to => 'sys.questdentalusa#gmail.com',
:subject => #user.subject,
:text => #user.text}
mg_client.send_message ENV['domain'], message_params
end
It won't send the message. It's as if it did not execute.
The rule is, no model should be involved.
Example, you have an existing gmail account and wrote a message sent to me. I should receive your message from your entered gmail account.
Two things your developer config and message_params looks wrong,
in message_params : :from => ENV[#user.email] is should be like #user.email
in smtp_settings : :address => #user.email, is like "smtp.mailgun.org". checkout more smtp_settings at here
I got the answer for quite a while now and I just decided to might as well share it here. This is what I did in my development.rb
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
user_name: "sys.questdentalusa#gmail.com",
password: "passwordhere",
authentication: :plain,
enable_starttls_auto: true
}
This is what I got under my Mailer
class MessageMailer < ActionMailer::Base
default from: "sys.questdentalusa#gmail.com"
default to: "questdentalusa#gmail.com"
def new_message(contact)
#contact = contact
mail subject: 'Inquiry from website: ' + #contact[:subject]
end
end
I got this under new_message.text.erb
Name: <%= #contact[:name] %>
Email: <%= #contact[:email] %>
Message: <%= #contact[:content] %>
And this is under my controller
class HomeController < ApplicationController
skip_before_filter :verify_authenticity_token
def send_mail
if MessageMailer.new_message(contact_params).deliver
redirect_to contact_path
flash[:notice] = 'Your messages has been sent.'
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :subject, :content)
end
end

Rails Action Mailer not sending email

I am a complete beginner in Rails and I'm trying to send an email after someone signs up using Action Mailer.
My logs say that the email is sending, but Gmail never gets it.
config/initializers/setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "asciicasts.com",
:user_name => "asciicasts",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "eifion#asciicasts.com"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Registered")
end
end
controllers/users_controller.rb
...
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
...
Thanks!
Make sure you have this option set in your config/environments/development.rb :
config.action_mailer.delivery_method = :smtp
Also, in ActionMailer::Base.smtp_settings you need to specify a valid gmail account. Copy-pasting (asciicasts) is not gonna cut it here.
See this question for reference: Sending mail with Rails 3 in development environment
Instead of 'smtp' you can use 'sendmail'
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = { :address => "smtp.gmail.com",
:port => "587", :domain => "gmail.com", :user_name => "xxx#gmail.com",
:password => "yyy", :authentication => "plain", :enable_starttls_auto => true }
I ran into this same problem for a new mailer I had setup. I couldn't figure out for the life of me why this new mailer couldn't send emails, or even get to the method in the mailer when I stepped through it.
Solution
It ended up being that if you put the deliver_now or deliver* code within the mailer, it does not send the email.
Example Broken
def email_message()
message = mail(to: User.first, subject: 'test', body: "body text for mail")
message.deliver_now
end
Corrected
#Different class; in my case a service
def caller
message = MyMailer.email_message
message.deliver_now
end
def email_message()
mail(to: User.first, subject: 'test', body: "body text for mail")
end
This solved the problem for me, I hope it solves it for someone else.

ActionMailer doesn't use default from, or any "from" for that matter

I'm trying to send out "Welcome Emails" to my users, however, the default :from is not working. It is using the user_name I specify in the config/application.rb file.
This is the code I currently have.
config/application.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "domain.com",
:user_name => "myemail#gmail.com",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => 'email_i_want_to_send_from#gmail.com'
def welcome_email
mail(:to => "myemail#gmail.com", :subject => "welcome")
end
end
Instead of receiving an email from email_i_want_to_send_from#gmail.com, a user would receive an email from myemail#gmail.com. (Yes, I am using actual email addresses and passwords when testing).
Anyone have any ideas as to why this is happening? Thanks.
Google doesn't allow you to "spoof" a from address. I used Send Grid to send out my emails.

Why can't I set the FROM name correctly using Gmail in my Rails app?

I am trying to get the emails I send through Gmail from my Ruby on Rails app to be from Ben.
Currently when the email arrives in a (seperate, non-related) Gmail account, in the Inbox it has ben in the from section.
Here are my settings:
setup_mail.rb
# my domain is my_example.com, and the email address I am sending from is ben#my_example.com
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "my_example.com",
:user_name => "ben#my_example.com",
:password => "my_password",
:authentication => "plain",
:enable_starttls_auto => true
}
if Rails.env.development?
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
else
ActionMailer::Base.default_url_options[:host] = "my_example.com"
end
report_mailer.rb
def send_notification_email(notification_details)
subject = "testing_email"
mail(:to => notification_details[:email], :subject => subject, :from => "Ben")
end
And here are the email settings in Gmail:
In the :from part you can actually specify the email address this way :
:from => 'Ben <ben#my_example.com>'
The :from key there is actually the email you're sending it from. GMail does not allow this to be overriden, as it can lead to abuse.

Resources