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
Related
I didn't figure out how I send mail from gmail in development environment.It didn't send email. I didn't understand the rails guide, and also I wonder if the production env is the same ?
config/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'something.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'mail.google.com',
user_name: 'myusername#gmail.com',
password: 'mypassword',
authentication: 'plain',
enable_starttls_auto: true }
mailer/user_mailer.rb
default :from => 'something.com'
def welcome_email(user)
#user = user
#url = 'http://something.com'
mail(to: #user.email, subject: 'Welcome')
end
edit
where I call, in users create method,
UserMailer.welcome_email(#user).deliver_now
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Try this in development.rb It will either send mail or raise delivery error in console.
You can call your mailer methods in two way to send email,
In UsersController's create action
def create
#your codes and logics to save user...
UserMailer.welcome_email(#user).deliver_now
#your codes goes here ...
end
In User model after_create callback
class User < ActiveRecord::Base
after_create :send_email
def send_email
UserMailer.welcome_email(self).deliver_now
end
end
I would prefer second one for sending email, use model callback rather in controller.
trying to send an accepted order email notification with the Shoppe gem.
production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => ENV['chbrown1293'],
:password => ENV['*******'],
:authentication => "plain",
:enable_starttls_auto => true
}
orders controller
def payment
gon.client_token = generate_client_token
#order = Shoppe::Order.find(current_order.id)
#result = Braintree::Transaction.sale(
amount: current_order.total,
payment_method_nonce: params[:payment_method_nonce])
if #result.success?
Shoppe::OrderMailer.accepted(#order)
current_order.destroy
redirect_to root_url, notice: "Payment successful, congratulations!"
end
end
Not sure what I'm missing but its probably quite obvious! (I've never set up a mailer before - I am indeed a noob :))
Thanks!
Shoppe has an automatic mailer built into the app's gemfile under it's orders controller if I am not mistaken. (Check the github repo to confirm)
In order to access it you should setup the config file for production with the mailer information.
Also ensure that you setup the mailer on "/shoppe/settings"
As soon as you make a purchase you should be sent an email.
I have not used Shoppe ever, but in ActionMailer, we have to call method deliver in order to send emails. Try changing
Shoppe::OrderMailer.accepted(#order)
to
Shoppe::OrderMailer.accepted(#order).deliver
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
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.
in my ActionMailer config file I have this:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "mail.foo.com",
:port => 25,
:domain => "foo.com",
:authentication => :email,
:user_name => "no-reply#foo.com",
:password => "foo1234567"
}
With this configuration can I only send out email from the no-reply#foo.com email address? If so is there a way to send out emails from other addresses? I have this in my ActionMailer class:
class Notifications < ActionMailer::Base
def answered_question(faq)
subject 'Your question has been answered'
recipients faq.email
from 'Foo <no-reply#foo.com>'
sent_on Time.now
content_type "text/html"
body :faq => faq
end
def completed_order(order)
subject 'Your order has been completed'
recipients order.email
from 'Foo <registrations#foo.com>'
sent_on Time.now
content_type "text/html"
body :order => order
end
end
In development everything works out fine but in production the completed_order emails are not being sent out.
Thanks.
I would guess that this is more of a SMTP issue that it is ActionMailer. Some SMTP's do not require Username/Passwords to send outgoing mail and so you can set the From address as you like.
That said, since you're experiencing issues sending out messages that have a From address different from what you're using to authenticate to your SMTP server, I'd guess there's a restriction on the SMTP box that only allows messages to be sent if the From address matches the authenticating UID.