When on Heroku my custom mailer does not send, I get no errors at all. This is the log -
Spree::HmsCommunicator#progress_email: processed outbound mail in 5.5ms
=> nil
It works perfectly fine in development -
Spree::HmsCommunicator#progress_email: processed outbound mail in 1178.4ms
Sent mail to blah#thehandbagspa.com (983.0ms)
Date: Tue, 19 Jan 2016 08:30:54 +0000
From: blah#thehandbagspa.com
To: blah#thehandbagspa.com
Message-ID: <569df43e70279_fe9a3ff1ae06020465535#Andrews-MacBook-Pro.local.mail>
Subject: Handbag Update | The Handbag Spa
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_569df43e6d9de_fe9a3ff1ae0602046541e";
charset=UTF-8
Content-Transfer-Encoding: 7bit
track-opens: true
application.rb -
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :postmark
config.action_mailer.perform_deliveries = true
config.action_mailer.postmark_settings = { :api_token => "***...." }
hms_communicator.rb -
module Spree
class HmsCommunicator < BaseMailer
require 'twilio-ruby'
def progress_email(handbag, stage, movedTo)
#handbag = handbag
#stage = stage
#movedTo = movedTo
mail(:subject => 'Handbag Update | The Handbag Spa',
:to => #handbag.user.email,
:from => 'blah#thehandbagspa.com',
:track_opens => 'true')
#send_text_message
end
def test_email
mail(:subject => 'Handbag Update | The Handbag Spa',
:to => 'blah#thehandbagspa.com',
:from => 'blah#thehandbagspa.com',
:body => 'hey',
:track_opens => 'true')
end
def send_text_message
account_sid = '****...'
auth_token = '****...'
alphanumeric_id = "TheHandbagS"
twilio_phone_number = '+..'
recipient_phone_number = '+..'
client = Twilio::REST::Client.new(account_sid, auth_token)
begin
client.messages.create(
from: alphanumeric_id,
to: recipient_phone_number,
body: "Hello Freya. We have received your item into the spa. It will be enjoying it's first treatment shortly. Love, The Handbag Spa"
)
rescue Twilio::REST::RequestError => error
if error.code == 21612
client.messages.create(
from: twilio_phone_number,
to: recipient_phone_number,
body: "Hello, this is a message from Andrew"
)
else
# handle this some other way
raise error
end
end
end
end
end
Trying these mail commands from the console locally works fine and I get the email returned in the console but in heroku console they return a nil object -
Spree::HmsCommunicator#progress_email: processed outbound mail in 5.0ms
=> #<ActionMailer::Base::NullMail:0x007fc9cc5c19b0>
However this works fine in the Heroku console?? -
ActionMailer::Base.mail(from: "test#example.co", to: "valid.recipient#domain.com", subject: "Test", body: "Test").deliver_now
I'm dying here, I need some help!
Related
We want to switch from Postmark to Postfix, but with Postfix the mail with the attachment(PDF file) is arriving as garbage:
Date: Sun, 10 Apr 2016 13:17:56 +0300
Mime-Version: 1.0
Content-Type: application/pdf; charset=UTF-8; filename=test_order1460278254278.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=test_order1460278254278.pdf
Content-ID: <570a2854317a9_1c6f12f77f841854#app1.mail>
JVBERi0xLjQKJeLjz9MKNSAwIG9iago8PC9CYXNlRm9udC9QV1dNR1QrQXJp
YWxNVC9EZXNjZW5kYW50Rm9udHNbNiAwIFJdL1R5cGUvRm9udC9TdWJ0eXBl
L1R5cGUwL0VuY29kaW5nL0lkZW50aXR5LUgvVG9Vbmljb2RlIDkgMCBSPj4K
ZW5kb2JqCjEwIDAgb2JqCjw8L0Jhc2VGb250L1dYSkRSSytMdWNpZGFTYW5z
LVR5cGV3cml0ZXJCb2xkL0Rlc2NlbmRhbnRGb250c1sxMSAwIFJdL1R5cGUv
Rm9udC9TdWJ0eXBlL1R5cGUwL0VuY29kaW5nL0lkZW50aXR5LUgvVG9Vbmlj
b2RlIDE0IDAgUj4+CmVuZG9iagoxNSAwIG9iago8PC9CYXNlRm9udC9UaW1l
This is my code:
class UserMailer < ActionMailer::Base
default :from => "mailer#xodapp1.com"
default :bcc => "do_not_reply#xodapp1.com"
default :reply_to => "do_not_reply#xodapp1.com"
def send_email(agent, customer, subject, body, mail_to = nil, mail_cc = nil, url_attachment = nil , name_attachment = nil)
#attachments[name_attachment] = {:content => open(url_attachment).read, :mime_type => MIME::Types.type_for(name_attachment).first} unless url_attachment.nil?
mail_to = !mail_to.nil? ? mail_to : "\"#{customer.name}\" <#{customer.email}>"
mailers_to = mail_to.split(',')
mail_cc = !mail_cc.nil? ? mail_cc.split(',') : agent.email
if [nil, []].include?(mail_cc)
mail_cc = UserMailer.default[:bcc]
end
body = "Copy attached" if body.nil? || body.empty?
mail_obj = mail(:to => mailers_to,
:from => "\"#{agent.name}\" <mailer#xodapp1.com>",
:bcc => mail_cc,
:subject => subject,
:content_type => "text/html",
:body => body.html_safe,
:reply_to => "#{!agent.email.nil? ? agent.email : UserMailer.default[:reply_to]}"
)
mail_obj.attachments[name_attachment] = open(url_attachment).read unless url_attachment.nil?
return mail_obj
end
end
I remove the content_type and change the order:
class UserMailer < ActionMailer::Base
default :from => "mailer#cavsystems.com"
default :bcc => "do_not_reply#cavsystems.com"
default :reply_to => "do_not_reply#cavsystems.com"
def send_email(agent, customer, subject, body, mail_to = nil, mail_cc = nil, url_attachment = nil , name_attachment = nil)
#attachments[name_attachment] = {:content => open(url_attachment).read, :mime_type => MIME::Types.type_for(name_attachment).first} unless url_attachment.nil?
mail_to = !mail_to.nil? ? mail_to : "\"#{customer.name}\" <#{customer.email}>"
mailers_to = mail_to.split(',')
mail_cc = !mail_cc.nil? ? mail_cc.split(',') : agent.email
if [nil, []].include?(mail_cc)
mail_cc = UserMailer.default[:bcc]
end
body = "Copy attached" if body.nil? || body.empty?
attachments[name_attachment] = open(url_attachment).read unless url_attachment.nil?
mail(:to => mailers_to,
:from => "\"#{agent.name}\" <mailer#cavsystems.com>",
:bcc => mail_cc,
:subject => subject,
#:content_type => "text/html",
:body => body.html_safe,
:reply_to => "#{!agent.email.nil? ? agent.email : UserMailer.default[:reply_to]}"
)
#mail_obj.attachments[name_attachment] = open(url_attachment).read unless url_attachment.nil?
#return mail_obj
end
end
Now the email is arriving with the pdf attachment but the HTML code is raw :-(
It's o.k. because we need the pdf not the html.
It will be great if the HTML will be visible.
I looked around for similar issues but nothing seems to work for me.
I'm using ActionMailer
to send emails. The email outbound went through successfully but I I'm not receiving it in my email.
I have enabled POP mail service.
config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:authentication => :plain,
:address => "smtp.mailgun.org",
:port => 587,
:domain => "tvtt.co",
:user_name => "postmaster#tvtt.co",
:password => "password"
}
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
mailers/notification_mailer.rb
class NotificationMailer < ActionMailer::Base
default from: 'support#tvtt.com'
def acquisition_email(listing)
#neighborhood = listing.neighborhood
#tenant_fee = listing.tenant_fee
#listing = listing
mail(
to: "yseremail#gmail.com",
subject: "List your place on tvtt!"
)
end
end
And here is the "successful" output for the email outbound:
Sent mail to yseremail#gmail.com (23.1ms)
Date: Sat, 12 Dec 2015 20:05:13 -0500
From: support#tvtt.com
To: yseremail#gmail.com
Message-ID: <566cc44930cf0_64f23fc8933831dc28566#yseremail-MacBook-Pro.local.mail>
Subject: List your place on tvtt!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
I'm just about in the finishing stages of my website, however I am having trouble with the ActionMailer. It prints out the message just fine, I'm just eager to know how to wire it so it can send to gmail account. I'm primary confused how to route it and configure it properly. I have a contact page that has a model that takes parameters like the recipient, subject, message and the time it was sent: Mailer model: Note all this code is on a local machine
class UserEmail < ActionMailer::Base
default from: 'XXX#gmail.com'
def contact(sender, subject, message, sent_at = Time.now)
#sender = sender
#message = message
#sent_at = sent_at.strftime("%B %e, %Y at %H:%M")
mail(:subject => subject)
end
end
Here's the about controller which the contact methods lie in:
class AboutController < ApplicationController
# ...\controllers\home_controller.rb
#----------------------------------------------------
# show contact form
def contact
#title = "Contact"
#sender = ''
#subject = ''
#message = ''
end
def sendmail
#sender = params[:sender]
#subject = params[:subject]
#message = params[:message]
if validate(#sender, #subject, #message)
UserEmail.contact(#sender, #subject, #message).deliver
flash[:success] = "Your message sent sucessfully!"
redirect_to about_index_path
else
flash.now[:error] = "Your message did not send"
redirect_to about_index_path
end
end
private
def validate(sender, subject, message)
#email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
if sender.blank? || subject.blank? || message.blank?
#error = "Message not sent: Required information not filled"
return false
elsif subject.length >= 50
#error = "Message not sent: Subject must be smaller than 50 characters"
return false
elsif sender[#email_regex].nil?
#error = "Message not sent: Email not valid"
return false
else
return true
end
end
end
Now this is where I am lost.
Here's what my route like to the mailer. Is this routed appropriately?:
match '/contact_email', :to => 'about#sendmail'
When I configure the mailer, does the code rest in the application.rb or the development.rb? Here's what I have in my application.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'XXX#gmail.com',
:password => 'XXX',
:authentication => 'plain',
:enable_starttls_auto => true,
}
Thanks in advance!
Change
def contact(sender, subject, message, sent_at = Time.now)
#sender = sender
#message = message
#sent_at = sent_at.strftime("%B %e, %Y at %H:%M")
mail(:subject => subject)
end
to
def contact(sender, subject, message, recipient, sent_at = Time.now)
#sender = sender
#message = message
#sent_at = sent_at.strftime("%B %e, %Y at %H:%M")
#recipient = recipient
mail(:subject => subject, :to => #recipient)
end
And don't forget to set recipient in your calling function.
Have you put the following lines in development.rb
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
I am sending an email in my Rails 2 app and after a bit of hacking and learning about emails in Rails it now works fine.
Now I am just trying to add an attachment which in theory should probably be straightforward but I seem to be having issues with it.
I am using mailcatcher to preview the emails in the development environment and I can see the attachment headers in the email source but nothing is shown in mailcatcher (docs say it supports attachments)
emailer.rb
class Emailer < ActionMailer::Base
def quotation_notification(q)
#recipients = q.recipient_email
#from = q.partner_name + "<#{q.partner_email}>"
#subject = "New Quotation from " + q.partner_name
#sent_on = Time.now
#quote_id = q.quote_id
#customer_id = q.customer_id
#customer_name = q.customer_name
#recipient_email = q.recipient_email
#partner_name = q.partner_name
#partner_email = q.partner_email
#partner_ref = q.partner_ref
#version_no = q.version_no
#line_items = q.line_items
#quotation_date = q.quotation_date
content_type "multipart/alternative"
part "text/html" do |p|
p.body = render_message("quotation_notification.text.html.rhtml", :message => q)
end
attachment :content_type => "application/pdf",
:body => File.read(RAILS_ROOT + '/pdfs/' + q.quote_id + '.pdf')
#body[:q] = q
end
end
The email is being sent in a controller like so
q = QuotationEmail.new(quote_id, customer_id, customer_name, recipient_email, partner_name, partner_email, partner_ref, version_no, line_items, quotation_date)
# send email
Emailer.deliver_quotation_notification(q)
Just for completeness, my view is app/views/emailer/quotation_notification.text.html.rhtml
quotation_email.rb
class QuotationEmail
attr_accessor :quote_id, :customer_id, :customer_name, :recipient_email, :partner_name, :partner_email, :partner_ref, :version_no, :line_items, :quotation_date
def initialize(quote_id, customer_id, customer_name, recipient_email, partner_name, partner_email, partner_ref, version_no, line_items, quotation_date)
#quote_id = quote_id
#customer_id = customer_id
#customer_name = customer_name
#recipient_email = recipient_email
#partner_name = partner_name
#partner_email = partner_email
#partner_ref = partner_ref
#version_no = version_no
#line_items = line_items
#quotation_date = quotation_date
end
end
In the source of the email, after the closing html tag I can see
--mimepart_522da7e7e5959_a0885cd9314396
Content-Type: application/pdf
Content-Transfer-Encoding: Base64
Content-Disposition: attachment
*base 64 encoded stuff*
--mimepart_522da7e7e5959_a0885cd9314396--
If I have done anything bonkers then it is because I haven't sent any emails in Rails yet so still figuring things out.
I can only assume this was a problem with mailcatcher, by setting up development emails to be sent to a gmail account, the emails were sent correctly with attachments.
environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.co.uk",
:user_name => "gmailaddress#domain.co.uk",
:password => "accountpassword",
:authentication => :plain,
:enable_starttls_auto => true #This line is must to ensure the tls for Gmail
}
I used an existing app email address hosted on gmail, so not strictly an username#gmail.com / username#googlemail.com but I would think that those addresses would also work with the above.
According to these docs on Rspec matchers, calling an_instance_of is another way of calling instance_of?(Class). When I test in the console, this works fine, but in my spec it fails.
In console:
m = Mail.new
=> #<Mail::Message:70144097437100, Multipart: false, Headers: >
m.instance_of?(Mail::Message)
=> true
Failure:
1) IncomingEmailsController should deliver the email with lead info
Failure/Error: post :create, to: "me#example.com", text: "Content", html: "HTML Content", from: "email#example.com", subject: "Jimmy Bean"
<UserMailer (class)> received :forward_email with unexpected arguments
expected: (#<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x007feff35fd148 #klass=Mail::Message>)
got: (#<Mail::Message:70334278571020, Multipart: true, Headers: <From: email#example.com>, <To: me#example.com>, <Subject: Jimmy Bean>, <Content-Type: multipart/alternative; boundary=--==_mimepart_50c1ffa9e2ba2_2e1e3ff7f8c35ad49574>>, nil)
incoming_emails_controller_spec.rb
describe IncomingEmailsController do
it "should deliver the email with lead info" do
# expect
UserMailer.should_receive(:forward_email).with(an_instance_of(Mail::Message))
# when
post :create, to: "me#example.com", text: "Content", html: "HTML Content", from: "email#example.com", subject: "Jimmy Bean"
end
end
incoming_emails_controller.rb
def create
# create a Mail object from the params sent by Sendgrid
prepare_email(params) #returns #email (instance of Mail::Message)
UserMailer.forward_email(#email).deliver
end
private
def prepare_email(params)
email = Mail.new
email.to = params["to"]
email.from = params["from"]
email.subject = params["subject"]
text_part = Mail::Part.new
text_part.body = params["text"]
html_part = Mail::Part.new
html_part.content_type = 'text/html; charset=UTF-8'
html_part.body = params["html"]
email.text_part = text_part
email.html_part = html_part
#email = email
end
It looks like this was solved: https://github.com/rspec/rspec-mocks/issues/202.