Overridding default sender ActionMailer Rails 3 - ruby-on-rails-3

As being completely new to Rails, I have one question related to action mailer. In my setup_email.rb in /config/initializer/setup_email.rb, I have email configuration like this:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "yoolk.com",
:user_name => "yoolkers",
:password => "secrete",
:authentication => "plain",
:enable_starttls_auto => true
}
Also in my /app/mailers/contact_us_mailer.rb, I got some code:
class ContactUsMailer < ActionMailer::Base
default :from => 'abc#yoolk.com'
def send_email
mail(:to => 'victory#yoolk.com', :subject => 'Test Action Mailer')
end
end
When I called send_email method in my controller, it worked fine, email sent to victory#yoolk.com. However, the sender was yoolkers#gmail.com. What I really want is sender as abc#yoolk.com. I have been looking through for this for days, but I couldn't find any solution at all. Is there any way to change the default sender from the configuration email? I would appreciate for your contribute.
Thanks

Related

Rails Zoho configuration sending mails via console but not from mailer class

I have following smtp configuration on my development.rb
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.zoho.com",
:port => 465,
:user_name => "xxx#xxx.com",
:password => "xxxx",
:authentication => :login,
:ssl => true,
:tls => true,
:enable_starttls_auto => true
}
I works when I send via rails console , but gives error of
Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at
):
when I send via mailer class.
It seems like gmail is blocking you temporarily while you are trying to reach through controller.
Kindly have a check with this link
https://support.google.com/mail/answer/7126229?visit_id=1-636342470835359266-2568809045&rd=1#cantsignin
Hope this helps.

Rails mailer sending Empty Body on EMail

I have following on config/application.rb
config.action_mailer.default_url_options = { :host => 'xxxxx.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:domain => 'xxxxx.com',
:user_name => 'xxx.xxxxx#gmail.com',
:password => 'xxxxxxxxxxxxxxxx',
:authentication => 'plain',
:enable_starttls_auto => true }
And in app/mailers/welcome_mailer.rb
def welcome_email(user)
#user = user
#lang=I18n.locale
if #user.email.present?
begin
headers = {
:subject => welcome_email_subject(#user).to_s,
:from => ADMINISTRATIVE_EMAIL,
:to => user.email
}
mail(headers)
rescue Exception => e
abort
end
end
end
I have a template on /app/views/welcome_mailer/welcome_email.html.erb
I am using this mailer action for sending welcome emails along with confirmation link by using devise.For that I have done the following on /config/initializers/welcome_mailers.rb
module Devise::Models::Confirmable
def send_on_create_confirmation_instructions
if self.email
WelcomeMailer.welcome_email(self).deliver
end
end
def send_reset_password_instructions
generate_reset_password_token! if should_generate_reset_token?
WelcomeMailer.generate_password(self).deliver
end
end
Even though the development I have used same smtp configurations I am getting empty body for the mail sent on production and the same working fine in development(local).By the way my production environment is Amazon EC2. Initally 15 days before I have got the same issue and I solved by changing the smtp account.Now it is not happening in any order.Suggest with your feedback or comments.
You can overwrite the template route in the mailer config:
class WelcomeMailer < ActionMailer::Base
...
self.template_root = "#{RAILS_ROOT}/app/views/mailers" # Rails.root in 3.x
...
end

Why doesn't Rails pick up my custom mail delivery method?

I defined a custom delivery method, and load it in an initializer:
ActionMailer::Base.add_delivery_method :custom, CustomDelivery
I then added config.action_mailer.delivery_method = :custom to both development.rb and production.rb.
But when I want to send an e-mail
UserMailer.authorize(user).deliver
It fails with something related to SMTP (ArgumentError: A sender (Return-Path, Sender or From) required to send a message
from /Users/me/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:99:in deliver!') – which I don't want to use.
Why is it not picking up the custom delivery method?
UPDATE:
When I try from the console, I notice the following:
irb(main):019:0> UserMailer.delivery_method
=> :custom
irb(main):020:0> UserMailer.authorize(user).delivery_method
=> #<Mail::SMTP:0x00000100bdc738 #settings={:address=>"localhost", :port=>25, :domain=>"localhost.localdomain", :user_name=>nil, :password=>nil, :authentication=>nil, :enable_starttls_auto=>true, :openssl_verify_mode=>nil, :ssl=>nil, :tls=>nil}>
(Btw I searched for "SMTP" in my project and there are 0 occurrences)
Configure the action_mailer delivery_method with your custom delivery class:
config.action_mailer.delivery_method = MyCustomDelivery
That class should implement a deliver! instance method that takes an instance of the Mail gem. Something like this:
class MyCustomDelivery
def deliver!(mail)
puts "MAIL FROM: #{mail.from}"
puts "RCPT TO: #{mail.to}"
puts "DATA: #{mail.to_s}"
end
end
Have you configured SMTP via environment.rb ? Here is how mine looks like.
ActionMailer::Base.smtp_settings = {
:domain => 'gmail.com',
:address => 'smtp.gmail.com',
:port => 587,
:tls => true,
:authentication => :plain,
:charset => 'utf-8',
:user_name => ENV['GMAIL_USERNAME'],
:password => ENV['GMAIL_PASSWORD'],
:enable_starttls_auto => true
}

Keep getting A sender (Return-Path, Sender or From) required to send a message

class SupportMailer < ActionMailer::Base
default :from => "email1#gmail.com"
def welcome_email(ticket)
case ticket.game
when "gameone"
#ticket = ticket
headers["Reply-to"] = "email1+#{ticket.token}#gmail.com"
headers["Return-Path"] = "email1+#{ticket.token}#gmail.com"
mail(:from => "email1#gmail.com", :to => ticket.email, :subject => "Welcome to 1 Support Ticket")
when "gametwo"
#ticket = ticket
headers["Reply-to"] = "email2+#{ticket.token}#gmail.com"
headers["Return-Path"] = "email2+#{ticket.token}#gmail.com"
mail(:from => "email2#gmail.com", :to => ticket.email, :subject => "Welcome to 2 Support Ticket")
when "gamethree"
#ticket = ticket
headers["Reply-to"] = "email3+#{ticket.token}#gmail.com"
header["Return-Path"] = "email3+#{ticket.token}#gmail.com"
mail(:from => "email3#gmail.com", :to => ticket.email, :subject => "Welcome to 3 Support Ticket")
end
end
end
I've set my default :from, so I don't get why I keep getting this message, I'm also trying to set it via headers to no avail.
here are my settings
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "emailx#gmail.com",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
I just call it like so, SupportMailer.support_response(#message).deliver
How do I fix this?
I notice you have no default case for the case statement. If you never end up calling the "mail" method inside your methods in the Mailer class, you'll get that error. Try moving your case statement out to where you call SupportMailer, maybe have methods for each case. That way you never call the SupportMailer unless you've already determined the correct ticket game.

gmail smtp with rails 3

I am trying to get a confirmation email sending using a gmail account. I have looked around and there is nothing that is obvious. There is no errors or anything, it just dosn't send
I have this as the initalizer:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "<address>#gmail.com",
:password => "<password>",
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
You don't need tlsmail gem anymore at least with Rails 3.2
This will be sufficient
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'baci.lindsaar.net',
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
From the all-mighty-guide ActionMailer configuration for gmail
add tlsmail to gemfile
gem 'tlsmail'
run :
bundle install
add these settings to config/envirnoments/development.rb file
YourApplicationName::Application.configure do
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:enable_starttls_auto => true,
:authentication => :login,
:user_name => "<addreee>#gmail.com",
:password => "<password>"
}
config.action_mailer.raise_delivery_errors = true
You should check that my_user_name#gmail.com has actually sent the email. We have had issues with this in the past when sending verification emails out through Gmail's SMTP server, since sending in bulk end up not sending at all.
I suggest you log into my_user_name#gmail.com and verify that there are no problems and that the emails are sent.
If not, you may want try a service like Send Grid to send outgoing emails.
Alternatively, you can look into your server. Or if you are in development, have a look at log/development.log. I'm pretty sure that you can see in your logs that it's actually trying to send the mail.
The problem is that Google doesn't trust your local IP address and your mails won't get delivered (not even to the spam directory). There is no way to work around this but using a white-listed server.
You can try this out by deploying your app into a production server like Heroku and test it there.
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'baci.lindsaar.net',
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
The <username> means to fill in your real username? So does the <password>