How to send multiple emails with SendGrid? - ruby-on-rails

The following code seems to work if there is one user, but truncate the email for multiple users:
users.each do |user|
mail(
:to => user.email,
:subject => 'Hi',
:template_name => 'notification'
).deliver
Is this the proper way to send a few emails?
Ruby on Rails 3.2.2
Heroku
SendGrid

I think this is what you're looking for:
def my_mailer_method
users = User.find({ ... })
headers['X-SMTPAPI'] = { :to => users.to_a }.to_json
mail(
:to => "this.will#be.ignored.com",
:subject => "Hi",
:template_name => "notification"
).deliver
end
This sends a message to any number of recipients use the SendGrid's SMTP API. You can find further information on the docs page.
You might also want to take a look at the sendgrid rails gem

To send email to multiple users: pass an array
Replace
:to => user.email
with
:to => users.map(&:email)
more > rails guide

If it is not important for you to hide email addresses from each other, you can specify recipients in a comma delimited string.

It seems that the problem is that each instance of the Mailer can only send one email. Perhaps the mail object is falling out of scope and getting cleaned up by the garbage collector...
The solution that worked was to iterate over the users outside of the Mailer, and call it once for each user. It may be slow but it should happen in the background anyway so it's fine.

Related

Pony.mail() Email is being sent by and to the same email address

I am reading this tutorial where it teaches to build a contact-us form. Everything worked fine, But I think the following line is not working properly.
in inquiry.rb
def deliver
return false unless valid?
Pony.mail({
**:from => %("#{name}" <#{email}>),**
:subject => "feedback",
:body => message,
:html_body => simple_format(message)
})
end
the email is being sent by and to the same email address. Please Help.
Maybe you are using the gmail SMTP server. It has many disadvantages such as limited number of messages for a period. You can use smtp.mandrillapp.com instead of smtp.gmail.com.

How to show to whom email was originally destined in rails?

I have a system which receives and send mails to the various members in a group.
Lets say I have a group support#xyz.com which have members
member_1#xyz.com
member_2#xyz.com
and so on..
Same way there is another group help#xyz.com which have members
member_1#xyz.com
member_3#xyz.com
and so on..
When I get any mail for support#xyz.com it is forwarded to it's member and same way for help#xyz.com.
mail :from => the_person_who_sent_mail,
:to => members_email_id,
:subject => mail_subject,
:reply_to => the_group_name#xyz.com
And it appears like mail was sent to me.
But I want to do a change in this as shown below :
When it lands in my inbox it should be something like
From : the_person_who_sent_mail#something.com
Reply To : the_group_name#xyz.com
To : the_group_name#xyz.com
But according to my setting it comes like
From : the_person_who_sent_mail#something.com
Reply To : the_group_name#xyz.com
To : members_email_id#xyz.com
In my case it shows that the mail is destined to me. But in the first case it shows to whom the mail was originally destined for.
Mailing list servers accomplish this with BCC, like so:
mail :from => the_person_who_sent_mail,
:to => the_group_name#xyz.com,
:subject => mail_subject,
:reply_to => the_group_name#xyz.com,
:bcc => [members_email_id]
The downside to this is that your mailserver will actually attempt to send the e-mail to the_group_name#xyz.com, so it will have to know to ignore this message. However, when the message arrives in the user's inbox, the To: will be the_group_name#xyz.com.

"Postmark::InvalidMessageError: Provide either email TextBody or HtmlBody or both." for a rails 3.2 app

I was reading through the Postmark documentation, saw the rails gem there (github link).
I set it up according to the instructions and I ran into this message when I tried sending an email:
Provide either email TextBody or HtmlBody or both.
I have my email settings in my mailer as such:
mail(
:to => user.email,
:subject => "Thanks for signing up",
:from => "me#domain.com",
"HtmlBody" => "<b>Hello</b>",
"TextBody" => "Hello"
)
Please let me know if you need more information. I'm not sure if this is detailed enough for someone who has seen this error before.
Misnaming Email Views
I ran into this same thing and it was due to a misnaming on my part of the views associated with the email.
Example
_user_first_logs_in.html.erb # Was incorrectly using this.
user_first_logs_in.html.erb # Should be using this.
A good way to test for this locally is by using the mail_view gem by our trusted 37signals boys that allows you to preview email in development. Check it out.
That should expose a lot of basic issues.
JP

emailing a page that already exists

I'm looking at this tutorial http://guides.rubyonrails.org/action_mailer_basics.html and I get why it would send the html page that is in the view. However how do I send a page that already exists before I created the mailer? I don't want to copy the html and the ruby code over because I don't want another copy of that code to have to maintain.
The page I want to email is an announcements page and there is already another view for the announcements page since it is accessible from other parts of the site.
You can pass :template_path and :template_name hash keys to the specific method in your mailer as in:
mail(:to => user.email,
:subject => "Subject",
:template_path => 'announcements',
:template_name => 'index')
In this case it will look for templates at app/views/announcements with name index.

Rails - Attachments for an email appear at the top of the email and not the bottom

Anyone know why rails, places attachments at the top of the email and not the bottom?
Mac Mail, and the Iphone for example show the attachments at the top of the email and not at the bottom, which is very strange.
My user_mailer.rb looks like this:
def error_email
attachments['message.html'] = {:mime_type => 'text/html', :content => message_text }
mail(:to => #message_from, :subject => 'reason whyxxxx')
end
How can I get the attachment at the bottom of the email msg?
Thanks
I've run into this before. A quick fix is to do this in your mailer method to reverse the order of the email parts.
message = mail(:to => #message_from, :subject => 'reason whyxxxx')
message.parts.reverse!
You can sort the parts whichever way you want like this:
body.set_sort_order(order)
body.sort_parts!
-- where order defaults to ['text/plain', 'text/enriched', 'text/html', 'multipart/alternative'].
However this sorts on content type, as far as I can tell, so if your attachment has the same content type as the text part it won't make any difference.

Resources