Rails: change default sender in action mailer - ruby-on-rails

I am sending email using action mailer in my rails app. But it allows only one default sender. This is my UserMailer class:
class UserMailer < ActionMailer::Base
default :from => "example#example.com"
def welcome_email(user, order)
#user = user
#order = order
mail(:to => user.email, :subject => "Your Order")
end
def signup_email(user)
#user = user
mail(:to => user.email, :subject => "Thank you.")
end
def invite_confirm(curuser,usemail,post)
#greeting = "Hi"
#user = curuser
#post = post
mail(:to => user.email, :subject => "Hello")
end
end
I tried this:
class UserMailer < ActionMailer::Base
def welcome_email(user, order)
#user = user
#order = order
mail(:to => user.email, :subject => "Your Order", :from => "abc#xyz.com")
end
def signup_email(user)
#user = user
mail(:to => user.email, :subject => "Thank you.", :from => "qwe#asd.com")
end
def invite_confirm(curuser,usemail,post)
#greeting = "Hi"
#user = curuser
#post = post
mail(:to => user.email, :subject => "Hello", :from => "zyx#asd.com")
end
end
But still it is sending email from "example#example.com"
Is there any way to change sender for each method written in UserMailer class? Am i supposed to change anywhere else?
In config/environments/development.rb and config/environments/production.rb i have this:
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => "plain",
:user_name => "example#example.com",
:password => "example",
:enable_starttls_auto => true
}
I guess, i should not change anything here.

You can pass it as a parameter to the mail method:
def new_mail
mail from: "example#example.com", to: "user#example.com"
end

I think you want to send mail with three different emails of the for-each action. Because you use gmail, you need Sending mail from a different address.
No single vendor is optimal for all three types of email; you likely
will use several vendors.
For “company email,” that is, sending individual email to customers or
business associates, you’ll probably use Gmail or Google Apps for
Business. For a single address, you can set up a single Gmail account
to receive and send email from a different address. More likely,
you’ll want several email addresses for your company mail. For that,
use Google Apps for Business.
Send Email with Rails

I found that, this can't be done using smtp. Need to use amazon SES which allows multi sender support.

Here's what i use, it allows to make a "title" different.
class UserMailer < ActionMailer::Base
default :from => '"example" <example#domain.com>'
def send_signup_email(user)
#user = user
mail(to: #user.email, subject: 'example')
end
end

Related

Rails & Mailgun: emails not sending

I have an app that sends out email notifications. Some users have mentioned they are not receiving notifications.
NotificationMailer.rb:
def send_daily_digest(user_id)
#user = User.find(user_id)
mail(to: #user.email, subject: "#{#jobs.count} new jobs yesterday")
mail(to: "myadminemail#gmail.com", subject: "TEST #{#jobs.count} new jobs yesterday for #{#user.email}")
end
My email is myadminemail#gmail.com and I am receiving the email. I checked Mailgun logs and the other (non admin) email is not being sent.
What could be causing this?
EDIT: I just made the below change:
def send_daily_digest(user_id)
#user = User.find(user_id)
#recipients = []
#recipients << #user.email
#recipients << myadminemail#gmail.com
mail(to: #recipients, subject: "#{#jobs.count} new jobs yesterday")
end
Making that change properly sent out the email to both recipients. So maybe it has something to do with not being allowed to have two separate mail_to actions?
My issue now is that both recipients are CC'd on the email. I want the emails to be separate and direct to each user. How can I do this?
/config/environments/development.rb
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:authentication => :plain,
:address => "smtp.mailgun.org",
:port => 587,
:domain => "you domain in https://mailgun.com/app/",
:user_name => "you user_name in https://mailgun.com/app/",
:password => "you password in https://mailgun.com/app/"
}
According to the Rails Docs about ActionMailer the mail method is the main method that creates the message and renders the email templates.
That means is equivalent to ActionController#render so maybe it just sends the mail to the last recipient invoked by the mail method.
So maybe what you should do is to move the delivery logic to a object controlling the mailer.
For example I call these objects dispatchers.
class ClientDispatcher
def self.dispatch_digests
Clients.all.each do |client|
DigestMailer.send_daily_digest(client).deliver_later
DigestMailer.send_daily_digest("admin#gmail.com").deliver_later
end
end
end

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.

Ruby Contact Mailer not working properly

I have a simple script for my contact_mailer.rb which has a simple form as the front end with one dropdown to select purpose and has a switch-case block to choose the email ID accordingly. Now, somehow mail is always being sent to the first one i.e. when 'localization'.
Only this gets fired whatever be selected. Please explain why this may be happening. Am pasting code snippet below for reference:
class ContactUsMailer < ActionMailer::Base
default :from => "bot#mydomain.com"
def contact_us_email(name, message, purpose, email)
#name = name
#message = message
#purpose = purpose
#email = email
content_type "text/html"
case #purpose
when 'localization'
mail(:to => 'me#mydomain.com', :subject => purpose)
when 'marketing'
mail(:to => 'me#mydomain.com', :cc => 'me#mydomain.com, me#mydomain.com', :subject => purpose)
when 'network'
mail(:to => 'me.agm#gmail.com', :subject => purpose)
when 'recruitment'
mail(:to => 'me#mydomain.com', :cc => 'me#mydomain.com', :subject => purpose)
when 'general'
mail(:to => 'me#mydomain.com', :subject => purpose)
else
mail(:to => 'me#mydomain.com', :subject => purpose)
end
end
end
Thanks and Regards

actionmailer question on rails 3

I'm trying to convert this code
def password_reset_instructions(user)
subject "Registered"
recipients user.email
body :edit_password_reset_url => edit_password_reset_url(user.perishable_token)
end
to this code
def password_reset_instructions(user)
#user = user
mail(:to => user.email, :subject => "Registered")
end
My problem is i don't know where to put the code below.
:edit_password_reset_url => edit_password_reset_url(user.perishable_token)"
I am using authlogic on rails 3.
In Rails 3, Mailers work just like controllers. You can use the instance variable of the user in the accompanying view.
Not tested, but try this:
def password_reset_instructions(user)
#edit_password_reset_url = edit_password_reset_path(user.perishable_token)
mail(
:subject => "Password Reset Instructions",
:recipients => user.email
)
end

Resources