How can I change this code in my email mailer so that when current_user sends out an email from the application it is received by the recipient :from => current_user.email.
Currently it is from "notification#example.com" but I would like this to change dynamically and Is this possible without resulting in emails going into junk?
class EmailMailer < ActionMailer::Base
default :from => "notification#example.com"
def email_listing(user, listing, email)
#user = user
#listing = listing
#email = email
#url = "www.example.com"
mail(:to => #email.email, :subject => #user.name)
end
end
You can just pass the from option to add a custom from address, and pass the reply_to option for a reply address into the mail method, like
def email_listing(user, listing, email)
#user = user
#listing = listing
#email = email
#url = "www.example.com"
mail(:to => #email.email, :subject => #user.name, from: 'notification#example.com', reply_to: #user.email)
end
Related
This is a particularly weird problem...
I use Devise and I would like to receive emails when I ask to reset my password.
So I added this line to my development.rb file :
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
This is working great...
But if I change the address and write something with a dot in it, like my production address "xxx.herokuapp.com" no mail is sent. I get no errors, in the log I see a message telling the mail has been sent, but I receive nothing.
I only receive an email if there is no dot ('.') in the host name I provide.
Edit
If I use the default Devise:Mailer, everything works fine.
It must be a problem with my custom Mailer.
Here it is :
Mailer.rb
class Mailer < Devise::Mailer
include Devise::Mailers::Helpers
def headers_for(action, opts={})
headers = {
:subject => t("devise.mailer.#{action}.subject"),
:from => mailer_sender(devise_mapping),
:to => resource.email,
:bcc => "myadresse",
:template_path => template_paths
}
end
def confirmation_instructions(record, opts={})
devise_mail(record, :confirmation_instructions, opts)
end
def reset_password_instructions(record, opts={})
devise_mail(record, :reset_password_instructions, opts)
end
def unlock_instructions(record, opts={})
devise_mail(record, :unlock_instructions, opts)
end
end
As explained here, you probably have to call super like this
def headers_for(action, opts={})
headers = super.merge({
:subject => t("devise.mailer.#{action}.subject"),
:from => mailer_sender(devise_mapping),
:to => resource.email,
:bcc => "myadresse",
:template_path => template_paths
})
end
I am trying to send email from action mailer with delayed_job gem in rails.
I am calling method of action mailer from after_create callbacks
in demo model
after_create :send_mail
def send_mail
DemoMailer.to_client( self).deliver
end
in demo_mailer
def to_client( demo)
#demo = demo
mail(:to => demo.email, :subject => "Confirmation")
end
handle_asynchronously :to_client
it shows an error
ArgumentError in DemosController#create
A sender (Return-Path, Sender or From) required to send a message
then I change
def send_mail
DemoMailer.to_client( self).deliver
end
to
def send_mail
DemoMailer.delay.to_client( self)
end
it did not show any error but I did not get any mail also.
As the error suggests: you are missing either a return-path, sender or from field. Setting a from field is not difficult:
mail(:to => demo.email, :from => 'webmaster#your-domain.com', :subject => "Confirmation")
See the ActionMailer#mail documentation for more information.
If you are setting from at top:
class FoobarMailer < ActionMailer::Base
default from: SomeClass.email_variable
#...
You may give a try this:
class FoobarMailer < ActionMailer::Base
default from: -> { SomeClass.email_variable }
#...
Or send it just like #Veger mention:
mail(:to => demo.email, :from => 'webmaster#your-domain.com', :subject => "Confirmation")
i am using rails3.0.6 and ruby 1.8.7, i can easily send email from my applications development mode.But last night i saw a new error when i tried to send mail to my customers email which is polymorphic associated with my invoice model. The error is:
Net::SMTPSyntaxError in InvoicesController#email_invoice
501 < #invoice.account.accountable.email >: missing or malformed local part
My code for email.rb file is :
def send_invoice(invoice)
email = '#invoice.account.accountable.email'
mail(:to => email, :from => "support#thenextwave.in", :subject=>"Invoice, check it.")
end
and in my invoice controller i did:
def email_invoice
#company = Company.find(User.find(session[:current_user_id]))
#invoice = Invoice.find(params[:id])
#invoice_line_items = #invoice.invoice_line_items
#receipt_vouchers = #invoice.receipt_vouchers
#email = #invoice.account.accountable.email
Email.send_invoice(#invoice).deliver
redirect_to invoice_path(#invoice)
flash[:success] = 'Email has been sent successfully.'
end
i goggled for this error and find that it causes due to wrong from and to email addresses or wrong SMTP settings, but my SMTP setting is ok since i can send email from my application but has problem with below line:
#invoice.account.accountable.email
any help would be thaknful..
I come up with a solution, I made mistake in email.rb file in my mailer :
def send_invoice(invoice)
email = '#invoice.account.accountable.email'
mail(:to => email, :from => "support#thenextwave.in", :subject=>"Invoice, check it.")
end
it should be:
def send_invoice(invoice)
email = invoice.account.accountable.email
mail(:to => email, :from => "support#thenextwave.in", :subject=>"Invoice, check it.")
end
Now I can send email easily..
Mailer:
class CustomerHistoryMailer < ActionMailer::Base
default :from => "notifications#example.com"
def log_email(to)
mail(:to => to,
:subject => 'Report')
end
end
Controller:
class Admin::ReportsController < ApplicationController
def index
CustomerHistoryMailer.log_email("me#example.com").deliver
end
end
View (app/views/customer_history_mailer/log_email.text.erb):
Hello, world!
In my mailbox I receive empty message with right subject. Why?
Is it possible that you have generated a .html.rb view as well, and rails is detecting both, thus sending a multipart message (being the HTML empty) thus you are not seeing anything because your email client defaults to the HTML view?
Can you verify that?
--- What about specifying the order for html/text in the multipart msg?
class UserMailer < ActionMailer::Base
def welcome_email(user)
#user = user
#url = user_url(#user)
mail(:to => user.email,
:subject => "Welcome to My Awesome Site") do |format|
format.html
format.text
end
end
end
I removed config.action_mailer.deprecation = :log line from config/environments/development.rb and it works now.
I'm trying to get all of my system's email notifications under one umbrella using PostMarkApp and utilizing the Rails gems (postmark-rails, postmark-gem, and mail). I have successfully created a mailer that handles sending receipts for purchases but I haven't been able to receive emails for forgotten passwords. My development logs show that Devise sent the message but no email is received in my inbox and the PostMark credits are not decremented.
What's the best or easiest way to have Devise's mailers send through my PostMark account?
Snippet from config/environments/development.rb
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => "VALID_API_KEY_WAS_HERE" }
config.postmark_signature = VALID_POSTMARK_SIGNATURE_WAS_HERE
My Mailer that uses Postmark
class Notifier < ActionMailer::Base
# set some sensible defaults
default :from => MyApp::Application.config.postmark_signature
def receipt_message(order)
#order = order
#billing_address = order.convert_billing_address_to_hash(order.billing_address)
mail(:to => #order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
format.html
end
end
end
EDIT: SOLUTION to my question is below
Solved it by having my Notifier mailer extend Devise::Mailer and specifying Devise to use my Notifier as the mailer within config/initializers/devise.rb
snippet from config/initializers/devise.rb
# Configure the class responsible to send e-mails.
config.mailer = "Notifier"
My Notifier Mailer now
class Notifier < Devise::Mailer
# set some sensible defaults
default :from => MyApp::Application.config.postmark_signature
# send a receipt of the Member's purchase
def receipt_message(order)
#order = order
#billing_address = order.convert_billing_address_to_hash(order.billing_address)
mail(:to => #order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
format.html
end
end
# send password reset instructions
def reset_password_instructions(user)
#resource = user
mail(:to => #resource.email, :subject => "Reset password instructions", :tag => 'password-reset', :content_type => "text/html") do |format|
format.html { render "devise/mailer/reset_password_instructions" }
end
end
end
Using the latest version of Devise, the methods above didn't help me. This is my solution.
In config/application.rb:
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => "your-API-key-here" }
In config/initializers/devise.rb:
config.mailer = "UserMailer" # UserMailer is my mailer class
In app/mailers/user_mailer.rb:
class UserMailer < ActionMailer::Base
include Devise::Mailers::Helpers
default from: "default#mydomain.com"
def confirmation_instructions(record)
devise_mail(record, :confirmation_instructions)
end
def reset_password_instructions(record)
devise_mail(record, :reset_password_instructions)
end
def unlock_instructions(record)
devise_mail(record, :unlock_instructions)
end
# you can then put any of your own methods here
end
Finally, make sure you have generated custom devise views
rails generate devise:views
and move the email templates from app/views/devise/mailer/ to app/views/user_mailer/
mv app/views/devise/mailer/* app/views/user_mailer/
If you also want to specify 'tags' in postmark headers you have to do this in your mailer:
# this override method is from Devise::Mailers::Helpers
def headers_for(action)
headers = {
:subject => translate(devise_mapping, action),
:from => mailer_sender(devise_mapping),
:to => resource.email,
:template_path => template_paths,
:tag => action.dasherize # specify the tag here
}
if resource.respond_to?(:headers_for)
headers.merge!(resource.headers_for(action))
end
unless headers.key?(:reply_to)
headers[:reply_to] = headers[:from]
end
headers
end
I also had to generate the views for devise and copy the mail templates into the right place for my mailer. Something like this -
rails generate devise:views
cp app/views/devise/mailer/* app/views/notification_mailer/