How To Send E-Mails With BCC in Rails 3 - ruby-on-rails-3

How can I send e-mails with the BCC header? I follow the ruby on rails guide and set :bcc => "email#email.com" and it doesn't work.
Thanks
edit by corroded
Here's the code I tried:
def booking_confirmed_email(booking)
#booking = booking
mail(:to => booking.contact_email,
:bcc => "my#email.com",
:subject => "Congratulations, #{booking.contact_name}!")
end
also tried:
def booking_confirmed_email(booking)
#booking = booking
mail(:to => booking.contact_email,
:bcc => ["my#email.com"],
:subject => "Congratulations, #{booking.contact_name}!")
end
to no avail

Full details here:
http://api.rubyonrails.org/classes/ActionMailer/Base.html
Short answer:
mail(:to => "some#example.com" , :subject => "Example Subject",
:bcc => ["bcc#example.com", "Order Watcher <watcher#example.com>"] ,
:cc => "other#example.com" )
note how you can pass an array of email addresses to each of the :to, :cc, :bcc options.
RailsCast:
http://railscasts.com/episodes/206-action-mailer-in-rails-3

I've just exactly the same problem. It turns out in my case I was BCC'ing the same address I was TO'ing. ActionMailer or the mail server was doing something clever and choosing to only send one copy of the email.
I changed to using two different email addresses and BCC worked perfectly.

on your user_mailer, on your mail def, add the following:
mail(:subject => "enter your subject", :bcc => "email#email.com")
you can also make your bcc recieve a list of emails
#bcc = User.all.pluck(:email)
then call
mail(:subject => "enter your subject", :bcc => #bcc)
hope this helps. :)

Check out http://railscasts.com/episodes/206-action-mailer-in-rails-3 and add 'default :bcc => "your_required_bcc_email" in your equivalent of the user_mailer.rb

If you are using any queue adaptor (ex. Sidekiq) - try restart it.

Related

email not sent with a dot in the host name default_url_options[:host]

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

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
}

Net::SMTPSyntaxError

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..

Rails Cast: ActionMailer Email not sent to inbox

The server states that the email was sent to the correct address but I am not getting the message in my inbox.
My Setup_mail.rb file
ActionMailer::Base.smtp_settings ={
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "my_user_name#gmail.com",
:password => "my_password",
:authentication => "Plain",
:enable_starttls_auto => true
}
My development.rb file is:
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true #default value
config.action_mailer.delivery_method = :smtp #default value
My test.rb file is:
config.action_mailer.delivery_method = :smtp
I have tried multiple variations and am lost. I am running on a Windows 7 machine. I am running Ruby 1.8.7 and Rails 3.0.7
Can anyone help?
Here is my create method:
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
redirect_to #user, :flash => { :success => "Welcome to the Sample App!" }
else
#title = "Sign up"
render 'new'
end
end
My user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "my_user_name#gmail.com"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Thanks for registering")
end
end
Take a look at your server. 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 whitelisted server.
If you try your app in production this should normally work, for example deploy your app to heroku to test it.
Try putting in .deliver at the end. That fixed this issue for me:
mail(:to .....).deliver!
Try changing the authentication from a string to a symbol.
ActionMailer::Base.smtp_settings ={
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "my_user_name#gmail.com",
:password => "my_password",
:authentication => :plain,
:enable_starttls_auto => 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.
I had this same issue, I was able to see that in my console mail was sent but in inbox nothing was appearing, one thing is that you can deploy your app on whitelisted server like heroku, or if you want to see just for testing purpose through your local just enable less secure apps in your browser and you should be able to see that email in your inbox
https://myaccount.google.com/lesssecureapps

Rails 3: My smtp message headers won't take the :from => sender.email

so heres my info to start
config/initializers/mailer.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mydomain.com",
:user_name => "myUser",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.delivery_method = :smtp
app/mailers/notifier.rb
class Notifier < ActionMailer::Base
default :from => "My Company <info#mydomain.com>"
def contact_notification(sender)
#sender = sender
mail(
:to => "info#mydomain",
:from => sender.email,
:subject => "Message from #{sender.fullName} on Mydomain.com")
end
end
My issue, whenever the emails get sent to me they show the smtp account as the from header email address instead of the senders email. So I can't just hit reply to reply to the senders email account.
Any Help would be appreciated.
Up-Date
With the the insite given I found a way to set the Reply-to email Header which did the trick.
Instead of using:
:from => sender.email,
I used
:reply_to => sender.email,
Hope this helps anyone else out there.
This seems specific to the gmail SMTP server see the information here:
http://mail.google.com/support/bin/answer.py?hl=en&answer=22370
This is the applicable section:
Note for IMAP/POP users: If you access Gmail through a POP or IMAP email client (e.g. Outlook) and would like to send messages with a custom "from" address, you have two options. We recommend that you configure your email client with two outgoing SMTP servers, one for Gmail and one for your other address. Your second option is to use Gmail's outbound servers with a different "from" address. If you've already configured the custom from address in the web interface, your message will be sent from:otheraddress#domain.com, sender:username#gmail.com, regardless of which custom from configuration you chose. Your messages will be sent from your regular Gmail address if you never configured your custom from settings in the web interface.
Other than setting that in the web interface for gmail as it suggests, have you tried the Reply-To header? That might work