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
Related
I am trying to implement sendgrid into my backend api rails system so that when a user signs up I can send them a welcome email. After making a post request and handling user creation, I get this verification:
UserMailer#send_sign_up_email: processed outbound mail in 43.5ms
Sent mail to *******#gmail.com (185.8ms)
Date: Wed, 28 Feb 2018 16:54:05 -0800
From: *******#gmail.com
To: *******#gmail.com
Message-ID: <5a974f2d39c92_c5b2abcd76769fc423e0#albert-VirtualBox.mail>
Subject: Welcome to BottlesTonight albert jin!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
My code looks exactly like in this link https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html.
This looks all fine and well, but the email is just not sending (I put stars here for the email but I actually put in my email, and I used another one of emails as the default for sending). There is no email in my outbox or inbox.
However, now that I think about it, I never "logged in" with my email or entered the password, so the application shouldn't have access to send emails with my email. I also never did anything with the api key that I made on my sendgrid account. Furthermore, for the environment.rb file, I wasn't sure what to put in domain, so I put gmail.com. These all seem kinda sketchy to me, I think the tutorial doesn't contain everything. Does anyone know how to configure this? I've been stuck on it for a while.
Edit:
I tried doing it on production and it is not working. Here is more info:
My production.rb looks like this:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => ENV['DEFAULT_HOST'] }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
I have a heroku sendgrid add on. I have set the heroku config vars. In my registrations controller I merely added the line:
UserMailer.send_sign_up_email(#current_user).deliver
My mailer class looks like:
def send_sign_up_email(user)
#user = user
mail(to: #user.email, subject: "Welcome! #{#user.first_name}")
end
However, when I sign up on my website, the user gets added to the database but the email is not sending. Does anyone know why, or how I can debug?
I would suggest to remove all config for ActionMailer from your environment files (i.e. files under /config/environments), except following ones
# Don't care if the mailer can't send.
config.action_mailer.perform_caching = false
# Reference: http://stackoverflow.com/a/20770131/936494
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Then create an initializer /config/initializers/mail_config.rb and add following code to it:
TEST_ENVS = %w(test)
FILESYSTEM_ENVS = TEST_ENVS + %w(development)
# =========== DELIVERY METHOD SETTING
delivery_method = case Rails.env.to_sym
when :production, :staging, :experimental
:sendmail
when :test
:test
else
:smtp
end
ActionMailer::Base.delivery_method = delivery_method
# =========== SMTP SETTINGS
ENVS_TO_USE_GMAIL_CONFIG = %w(development test)
gmail_config = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: ENV['MAIL_USER_NAME'],
password: ENV['MAIL_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true
}
if :smtp == delivery_method
use_gmail_config = ENVS_TO_USE_GMAIL_CONFIG.include?(Rails.env)
smtp_settings = ( use_gmail_config ? gmail_config : {} )
ActionMailer::Base.smtp_settings = smtp_settings
end
# =========== DEFAULT URL OPTIONS
default_url_options_settings = Settings.default_url_options
host = default_url_options_settings.host
port = default_url_options_settings.port
protocol = default_url_options_settings.protocol
default_url_options = {}
default_url_options[:host] = host if host.present?
default_url_options[:port] = port if port.present?
default_url_options[:protocol] = protocol if protocol.present?
ActionMailer::Base.default_url_options = default_url_options
The Settings object is available as part of using config gem. If you do not want to use that gem for configuring env-specific values then you can just hard-code them in the initializer and try it.
My /config/settings.yml looks like
default_url_options:
host: ''
port: ''
protocol: ''
and my /config/settings/development.yml looks like
default_url_options:
host: 'localhost'
port: '3000'
Having a centralized mailer config helps in diagnosing mailer settings related issues in quick manner.
First try it for Gmail account and if it works you can be sure that sending email works. Just make sure in your Gmail account Less Secure Apps setting is enabled.
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'm trying to setup the Mandrill API to send transactional emails. To start with I just want to get a welcome email sent to new users that have just successfully signed up. Once I've completed that I should be able to figure out any other additional emails. I've already coded a few bits, but to be honest I'm not entirely sure I'm doing it right! The code doesn't currently work during testing, which is really annoying.
All the code is included below, all input is welcome :)
I have both the following gems installed.
'mandrill-api'
'mandrill_mailer'
I'm unsure where to place the following code, the documentation for the gem says mail.rb in config/initializers https://github.com/renz45/mandrill_mailer (Look under 'Usage')
ActionMailer::Base.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_API_KEY'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
MandrillMailer.configure do |config|
config.api_key = ENV['MANDRILL_API_KEY']
end
I won't be using Action Mailer so I guess I'll just change
ActionMailer::Base.smtp_settings
to
MandrillMailer::TemplateMailer.smtp_settings
???
I have a transactional_mailer.rb file under app/mailers
class TransactionalMailer < MandrillMailer::TemplateMailer
default from: 'hello#randomemail.com'
def welcome(user)
mandrill_mail template: 'welcome_template',
subject: "hello",
to: {email: user.email, name: user.name},
vars: {
'USER_IDENTIFIER' => user.email,
'LIST_COMPANY' => 'Your Company'
},
important: true,
inline_css: true,
async: true
end
end
The app currently calls the method in the registration controller
MandrillMailer.welcome(resource).deliver
Should it be
TransactionalMailer.welcome(#user).deliver
???
It's not working during testing on the local server (rails s) I'm new to coding and trying to work out why.
All help is appreciated!
Thanks :)
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'm using ruby 1.8.7, rails 2.3.4, rubygems 1.3.6, windows 7 home basic.
Objective: to convert a html page into pdf and send it via email. (like an online receipt)
Here's what i used: prawn (for pdf) emailer (for email)
question: how to do i send an email to a real email address? All i got from the tutorial is sending an "email" that can be seen in command prompt. that's all. another question is how to generate pdf's on the fly, meaning there should be no file generated and attach it to an email? It's really hard and I have been working on this for weeks now. thanks.
The precise answer to your question depends on precisely how you're generating your pdfs, but here's an example that should work:
1) In your controller file (as part of an action)
pdf_doc = Prawn::Document.new()
pdf.text "Hello, world" # etc, etc
data_string = pdf_doc.render
user = 'me#example.com'
Emailer.deliver_email_with_attachment(user,data_string)
2) In your mailer file (e.g. app/models/emailer.rb)
class Emailer < ActionMailer::Base
def email_with_attachment(user, data)
# setup your email as normal using #from, #subject, etc
#from = user
# now attach the pdf
attachment :content_type => "application/pdf", :body => data
end
end
for more information on attachments in ActionMailer see The rails docs
EDIT: You should also make sure you've edited your config file(s) to make sure your rails app can send emails. As you're using windows you'll need to configure sending by SMTP:
config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "my.smtp-server.com",
:port => 25,
:domain => 'iwantgreatcare.org',
:user_name => 'username',
:password => 'password',
:authentication => 'plain',
}
For more information on configuring smtp setting see the Rails ActionMailer guide