Net::SMTPSyntaxError - ruby-on-rails-3

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

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

ActionMailer::Base Dynamic :from email address

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

How To Send E-Mails With BCC in 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.

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 send empty message

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.