Sending emails with attachments using Ruby on Rails via SendGrid - ruby-on-rails

I am trying to send email with attachments using Ruby on Rails.
I followed the instructions from the ActionMailer site.
def welcome(recipient)
#account = recipient
attachments['file.csv'] = File.read('/path/to/users.csv')
mail(:to => recipient,
:bcc => ["email#example.com", "email2#example.com"],
:subject => "Sending attachment")
end
I am able to receive emails but without the attachment, I am trying to attach csv file but I am getting a file called "noname" as attachment

I just had this problem. I was not providing a body for the email, and was sending the attachment only. Unfortunately, it appears that SendGrid gets confused by this, sending an empty email (which is expected) with an empty attachment (which is neither expected nor desired).
Therefore, the solution: provide a body for the email. In your specific case, an /app/views/application_mailer/welcome.text.erb template with a simple text, saying "See attached" or whatever appropriate.

SendGrid is an SMTP service, and thus should function just as any other outbound SMTP service. Are you sure your syntax and filepaths are correct?
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['free_book.pdf'] = File.read('path/to/file.pdf')
mail(:to => recipient, :subject => "New account information")
end
end
Verify correct syntax
Verify correct filepath
Verify permissions on file are set correctly
Check your logs

Related

Altering an email address but not the associated name in a Ruby on Rails Mail interceptor

Background: in a Rails 3.2 app, I have an ActionMailer purchase confirmation email that is manipulated in a "stage" environment so that email destined for addresses associated with payment processor sandbox accounts will actually be sent to the email addresses of the people who manage the sandbox accounts. This is currently done inside the mailer class:
# app/mailers/purchase_mailer.rb
class PurchaseMailer < ActionMailer::Base
default :from => "\"#{SiteConfig.name}\" <#{SiteConfig.support_email}>"
def purchase_notification(purchase)
#purchase = purchase
mail :to => "\"#{purchase.customer_name}\" <#{address_filter(purchase.customer_email)}>",
:subject => "[#{SiteConfig.name}] Purchase Confirmation"
end
private
def address_filter(email_address)
# Check for and remove sandbox identifiers
if Rails.env.stage?
email_address.sub(/_\d+_p(er|re)#/, '#')
else
email_address
end
end
end
But, hey, that looks like a great use case for an interceptor, no? So I pulled out the address_filter method above and added this to the Rails app.
# config/initializers/mail.rb
Mail.register_interceptor(StageMailInterceptor) if Rails.env.stage?
# lib/stage_mail_interceptor.rb
class StageMailInterceptor
def self.delivering_email(message)
receivers = []
message.to.each do |to|
receivers << to.sub(/_\d+_p(er|re)#/, '#')
end
message.to = receivers
end
end
At first glance, this appears to work great. In the stage environment, the email is intercepted and the "to" address becomes the email address I want the email to go to. The person managing the sandbox account used to make the purchase receives the email. Perfect... except the name of the sandbox account is gone. What once was "Joe Example" <joe_1338142567_per#example.com> changed to "Joe Example" <joe#example.com> is now changed to joe#example.com
...the name is now gone.
Looking at the Mail message interface, I see that message.to= can be set with a name, but calling message.to gets me an array of just email addresses without names, whether the name was provided or not.
Question: what is the correct way to alter an email address without altering the name associated with the email address in a mail interceptor?
This doesn't seem like it's the "right" way to do this, but grabbing the "To" header of the message, doing the replacement, and setting with message.to= allows me to preserve the names while altering the email address. So my interceptor became:
# lib/stage_mail_interceptor.rb
class StageMailInterceptor
def self.delivering_email(message)
message.to = message.header["To"].to_s.gsub(/_\d+_p(er|re)#/, '#')
end
end

ruby on rails ActionMailer error

I'm trying to use ActionMailer for the first time.
My mailer looks like this:
class RequestMailer < ActionMailer::Base
default from: "fred#ameipro.com"
def request_email(worequest)
#worequest = worequest
mail(:to => "dave#ameipro.com", :subject => "New Service Request")
end
end
But, I get the following error:
Net::SMTPFatalError in WorequestsController#create
550 Cannot receive from specified address <fred#ameipro.com>: Unauthenticated senders not allowed
How do I correct?
Thanks!
This is an error from the mail server - it is refusing to deliver email from your address. Thats the part you need to diagnose. To help you, we'd need more information about the server you're trying to send through. To troubleshoot, I would try configuring a different client to send through your server, then try to send an email from fred#ameipro.com to dave#ameipro.com and see what happens.

How can I make my mailer deliver a message to an address containing umlauts under Rails 3?

This is my mailer:
class MyUserMailer < ActionMailer::Base
default from: "me#mail.com",
:content_transfer_encoding => "7bit"
def build_email(user)
mail(:to => user.email,:subject => "Welcome")
end
end
I'm testing with an address containing umlauts: äardvark#mail.com, but I keep receiving the following error, when I try to deliver the email:
'to' parameter is not a valid address. please check documentation
Documentation says that the mailer will use UTF-8 to encode all the fields. How can I make this work?
EDIT: I'm using Rails 3.2.5
Problem was caused by my usage of the mailgun Rails gem. After I switched to using smtp directly, problem went away.

Postmark::InvalidMessageError when attempting to send mail

I use Rails 3 + Postmark-Rails.
At first, I created account on postmarkapp.com to get my api key.
I Activated my signature by email link [for example, "vitalyp#dot.com"]
-- After that, I added this to application.rb
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => "fdcb..." }
I use this method to send emails:
class UserMailer < ActionMailer::Base
default :from => "vitalyp#dot.com"
def do_my_mail(to, subj)
mail(:to => to, :subject => subj)
end
I get this error:
Postmark::InvalidMessageError
Sender signature not defined for From address.
Any ideas?
SPF and DKIM are recommended, not required. However, sender signature is required (as stated in the documentation).
Are you sure you are using the same From address as the one used to create the sender signature? This can happen if you are using different email.
Postmark uses sender signatures to make sure you don't use their service for spam purposes:
From http://developer.postmarkapp.com:
"Sender signatures are needed in order to verify that, well, you really own the mailbox, and that you are not a spammer (yes, we hate spam too). You must have a sender signature for each from address used in your application."

Rails 3: Contact form to send messages from User Profiles

I worked through some basic tutorials on Rails 3. The goal is a community-website on abilities and activities. I am using Devise for authentication. The creation of user profiles with avatars worked well (thanks to paperclip).
As a next step, I want to enable registered users to send an e-mail to a user from his (or her) profile page. I found a great tutorial on creating a contact form using Google Apps:
http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/
The mailer class in this tutorial looks like:
class NotificationsMailer < ActionMailer::Base
default :from => "noreply#youdomain.dev"
default :to => "you#youremail.dev"
def new_message(message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}")
end
end
My question: What is the best way to replace you#youremail.dev with the receivers E-Mail-Address? (from the User-Model)
Thanks in advance!
You can modify the new_message to accept the user (or list of users) to whom you want to send the email. Or an array of email addresses if you want to. Then pass the receiver's email address to the mail method as the :to option.
def new_message(receiver, message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}",
:to => receiver.email_address) # or something similar
end
Then you can invoke your mailer like this
NotificationEmail.new_message(a_user, a_message).deliver
To read the API see here or here (I prefer APIdock).
Also a more comprehensive guide on ActionMailer is available here. If you are new to Rails, you can find more guides here.

Resources