Good afternoon,
I'm facing a problem about the way to send email through my application hosted on hostingrails.
In my config/initializers I added a file "setup_mailer" containing this
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
# Production
if Rails.env.production?
ActionMailer::Base.sendmail_settings = {
:location => '/usr/sbin/sendmail',
:arguments => '-i -t -f support#xxxxx.xxx'
}
end
and my mailer is as below
class Notification < ActionMailer::Base
def subscription_confirmation(user)
setup_email(user)
mail(:to => #recipients, :subject => "blabla")
end
protected
def setup_email(user)
#recipients = user.email
#from = "support#xxxxx.xx"
headers "Reply-to" => "support#xxxxx.xx"
#sent_on = Time.now
#content_type = "text/html"
end
end
It seems to work very fine on my local machine. But in production, emails are not sent properly and I receive this message in my support inbox.
A message that you sent using the -t command line option contained no
addresses that were not also on the command line, and were therefore
suppressed. This left no recipient addresses, and so no delivery could
be attempted.
If you have any idea, the support seems cannot help me, hope some of you'll have ideas or config files from hostingrails to share.
Thank you,
albandiguer
I had exactly the same problem - resolved it by removing the -t from the sendmail_settings.
I haven't looked much further to investigate other implications, but at least it works. But from the man page:
-t Extract recipients from message headers. These are added to any
recipients specified on the command line.
With Postfix versions prior to 2.1, this option requires that no
recipient addresses are specified on the command line.
So maybe just a difference in Postfix versions?
Try calling to_s on user.email or specifying user email as "#{user.email}"
Related
I would like to use Sendgrid to manage outgoing emails from a 3.2.2 version rails app I am developing with the help of a friend. She has email working from within the app using gmail, on her local/dev build. I need sendgrid up and running.
I cannot even get it to work locally.
From my development.rb file
config.action_mailer.default_url_options = { :host => 'localhost:3030' }
config.action_mailer.smtp_settings = {
:user_name => ENV['EMAIL_USERNAME'],
:password => ENV['EMAIL_PASSWORD'],
:domain => 'myapplicationdomain.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => 'plain',
:enable_starttls_auto => true
}
config.action_mailer.delivery_method = :smtp
Then I have a variable file in the root of my application that includes the following:
export EMAIL_USERNAME=sendgridusername
export EMAIL_PASSWORD=sendgridpassword
export MAIL_TO=report#myapplicationdomain.com
Here is the code from my mailer
class StatusMailer < ActionMailer::Base
default from: "reports#myapplicationdomain.com"
def status_report(report)
#greeting = "Hello"
#report = report
if ENV['MAIL_TO']
email = ENV['MAIL_TO'] if ENV['MAIL_TO']
else
email = #report.user.email
end
#statuses = #report.statuses
#reviewers = #report.user.reviewers
bcc = []
#reviewers.each do |reviewer|
bcc.append(reviewer.email)
end
#bcc = bcc
mail(to: email, bcc: bcc, subject: 'Status Report')
end
end
Am I missing some other setting? What about the MAIL_TO field in the variable, I am not certain what that should be set to, or if it even needs to be declared.
Is there another file that I should be editing? I had this working several days ago, but functionality somehow slipped away :0
Rails server says that emails were sent, but sendgrid has no record; nor are the emails being received by addresses on the distribution list.
Thank you in advance for any assistance.
Do you have the following settings in your config/environments/development.rb?
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
If not, add them to your config file and restart your server.
Update:
This error suggests that you're not authenticated. Are you sure the values of your ENV['EMAIL_USERNAME'] and ENV['EMAIL_PASSWORD'] variables are present/correct?
This post:
Sendgrid / email sending issues in Ruby on Rails (hosted on Heroku)
Got me up and running. The key being putting the SMTP and sendgrid information in the environment.rb file.
I can't explain exactly why that made the difference, but it did.
I am using postmark to send email from the application. Its working fine for normal emails, but the email attachments are not working.
It works fine on local, as on local i have the smtp+postmark settings (as to work it on local we need to have postmark along with smtp)
But on staging and production am using only SMTP settings
config/environments/staging.rb and config/environments/production.rb
POSTMARK_API_KEY = "<my-api-key>"
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => POSTMARK_API_KEY }
config/environments/development.rb
POSTMARK_API_KEY = "<my-api-key>"
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.postmarkapp.com",
:port => 25,
:domain => 'example.com',
:user_name => POSTMARK_API_KEY,
:password => POSTMARK_API_KEY,
:authentication => 'plain',
:enable_starttls_auto => true }
user_mailer.rb
class UserMailer < ActionMailer::Base
default from: DEFAULT_EMAIL
def send_request(params, attachment)
if attachment.present?
mime_type = MIME::Types.type_for(attachment).first
attachments["#{attachment.split('/').last}"] = { mime_type: mime_type,
content: attachment, encoding: 'base64' }
end
mail(
to: <some_email>,
subject: "Refer Request",
tag: "refer_request")
end
Here attachment is the url of file saved on S3.
In development mode i receive email with attachment.
but not in staging and development mode.
Any help or suggestions would be appreciated. Thanks.
To send an email with attachment using Postmark Api
This approach is using curl command.
Require to get the remote file content
require "open-uri"
Require to get the encoding-decoding methods
require "base64"
Pass your remote file url to get the content of that
file_data = open('https://example.com/dummy.pdf').read
Encryp the bin object to send it via email
encrypt_data = Base64.encode64(file_data)
You can now send the email with attachment using postmark api and make sure to pass your API-KEY in the X-Postmark-Server-Token
system "curl -X POST \"http://api.postmarkapp.com/email\" \
-H \"Accept: application/json\" \
-H \"Content-Type: application/json\" \
-H \"X-Postmark-Server-Token: POSTMARK_API_KEY\” \
-v \
-d \"{From: 'from#example.com', To: 'to#example.com', Subject: 'Postmark test for Attachment Email', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user you have received email with attachment.</body></html>', Attachments:[{'ContentType': 'application/pdf', 'Name': 'dummy.pdf', 'Content': '#{encrypt_data}'}]}\""
Finally am able to find the actual cause of above issue.
I am using the gem postmark-rails, previously postmark was not supporting the email-attachments, so recently they had enhanced gem to support attachments and unfortunately i was using the old version of it, so i need to update gem version to latest as they have mentioned in one of their issues: attachment-issue
also i was trying to send url of file saved on S3, so instead of that i need to read that file from url and then send it as attachment
require "open-uri"
def send_refer_pt_request(params, attachment)
if attachment.present?
mime_type = MIME::Types.type_for(attachment).first
url_data = open(attachment).read()
attachments["#{attachment.split('/').last}"] = { mime_type: mime_type,
content: url_data }
end
mail(
to: <some_email>,
subject: "Refer Request",
tag: "refer_request")
end
I am running a Rails app and using Mailcather gem as an SMTP service. It was said that it can catch all outgoing emails, however I've done making correct settings in config/environments/development.rb, testing it to send email, but no email catched in either 127.0.0.1:1080 and localhost:1080. But the email was sent and received tho. I've tried all of the possible configurations. Here is my config/development.rb
config/development.rb
Ror::Application.configure do
# Mailer
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => '127.0.0.1:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "127.0.0.1", :port => 1025 }
end
Here is my user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "aaaaaaaaa#gmail.com"
def registration_confirmation(user)
#user = user
mail(:to => user.email, :subject => "Registered")
end
end
I used the registration_confirmation() method to check whether user is registered or not via email. But none of of the email popped up in mailcatcher. I did install it under rvm. I did test install it both with wrapper and without wrapper but the result still the same. Bottom line is, it is able to send emails outside, but can't receive email inside. Or any notifications. Did I miss something? Any advice or corrections would be appreciated. Thanks
You call the mail method like this:
UserMailer.registration_confirmation(user).deliver
I'd like to add the exception_notification gem to our app, however, this happens when I try to manually trigger a mail:
exception
# => #<ZeroDivisionError: divided by 0>
ExceptionNotifier::Notifier.exception_notification(request.env, exception)
# => #<ActionMailer::Base::NullMail:0x007fa81bc7c610>
ExceptionNotifier::Notifier.background_exception_notification(exception)
# => #<ActionMailer::Base::NullMail:0x007fa81bf58190>
In the above example, the console is at a breakpoint inside rescue_from Exception in the ApplicationController after a deliberate 1/0 in some controller.
I'm using delayed_job as well, but - no surprise - ExceptionNotifier::Notifier.background_exception_notification(exception).deliver does not spool anything.
I've already set config.consider_all_requests_local = false in development, but still exception_notification instantiates NullMail. In other parts of the app, mailers work just fine and use sendmail.
Any ideas what I'm doing wrong here? Thanks for your help!
Likely you are using an old version of the ExceptionNotifier and a newer version of ActiveMailer::Base. Not calling the mail command within the email functionality will result in the ActionMailer::Base::NullMail instance returned rather than a Mail instance.
From documentation:
class Notifier < ActionMailer::Base
default :from => 'no-reply#example.com',
:return_path => 'system#example.com'
def welcome(recipient)
#account = recipient
mail(:to => recipient.email_address_with_name,
:bcc => ["bcc#example.com", "Order Watcher <watcher#example.com>"])
end
end
I had my tests / rspec returning NullMail objects. the solution was simple, my code was:
def my_mail(foo)
mail(
to: to,
from: from,
subject: #sample_item.campaign_market.campaign.translation_for(language_id, 'sample_item_mailer.request_review.subject'),
content_type: "text/html"
)
#sample_item.update_attributes!({feedback_requested: true, review_requested_at: Time.now})
TrackingService::Event.new(#user, #user.market, 'sample_items', "request_review_email #{#sample_item.id}").call()
end
what's not immediately clear from the ruby docs is that you need to return the mail function,not just execute it. If you need to do something after building the mail object make sure you return the mail at the end. like so:
def my_mail(foo)
m = mail(
to: to,
from: from,
subject: #sample_item.campaign_market.campaign.translation_for(language_id, 'sample_item_mailer.request_review.subject'),
content_type: "text/html"
)
#sample_item.update_attributes!({feedback_requested: true, review_requested_at: Time.now})
TrackingService::Event.new(#user, #user.market, 'sample_items', "request_review_email #{#sample_item.id}").call()
return m
end
Rails 2.3.11
I'm trying to send an activation-style email whenever a user registers. The email gets sent successfully, but has the wrong "from" email address. The subject, content, and recipient's email are all fine. Instead of being sent from activation#[domain].net, they come from [login-name]#box570.bluehost.com.
/app/models/franklin.rb:
class Franklin < ActionMailer::Base
def activation(user)
recipients user.email
from "activation#[sub].[domain].net"
subject "[Product] Registration"
body :user => user
end
end
Applicable part of the controller that calls it:
#user = User.create(
:first_name => params[:first_name],
:last_name => params[:last_name],
:email => params[:email],
:password => params[:password],
:password_confirmation => params[:password_confirmation],
:user_class => "User"
)
Franklin.deliver_activation(#user)
/config/environments/development.rb:
# Settings specified here will take precedence over those in config/environment.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :sendmail
Thank you!
This looks like a Bluehost-specific problem. You may need to make sure the activation#[sub].[domain].net e-mail address is actually set up as a full email account with Bluehost (this seems to be a common solution).