How to show to whom email was originally destined in rails? - ruby-on-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.

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 send multiple emails with SendGrid?

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.

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.

Adding a uuid to an reply-to address in actionmailer

I have a token that I am creating for a "ticket" in my app, I'd like to send this token in the from address or the reply to address.
I'm trying this
from = "oursupport+"+ticket.token+"#gmail.com"
mail(:from => from, :to => ticket.email, :subject => "Welcome to Support")
When I check the sent email, I cannot see the token added to the from address but it does send correctly.

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