Setting email headers so gmail formats email correctly

Hey guys,

I’m trying to format my email so that, my html email shows up correctly
when a user sees their email through gmail.com. Here is what I have so
far:

def invite(email=“”, sender = “”)
@subject = sender.full_name + ’ has invited you to Mixbook.com
@recipients = email
@from = ‘[email protected]
@sent_on = Time.now
@content_type=“text/html”
@headers = {“MIME-Version”=>1.0,“Content-Type”=>“text/html”}
@body[‘sender’] = sender
end

This seems to be working when I send I see the email through outlook
express. What am I missing here. I noticed that the gmail emails that
work correctly use “multipart/alternative.” Is there some kind of trick
that I am missing?

  @body['sender'] = sender

I doubt that is going to work like you want it to. “Sender:” is a
fairly magical header that’s used by MTAs to let MUAs know which
envelope sender was used, so depending on the exact details of your
mail setup that line may work as you intend, be deceptive or be
overwritten.

For my own application, I wrote a tiny plugin that monkeypatches
ActionMailer to allow setting the sender as it should be (in the SMTP
dialog for :smtp, with -f for :sendmail). I don’t have a public svn
server to easily publish it from, but I could post it here, I guess.
If there’s interest.

Oh the @body[‘sender’] = sender shouldnt effect anything. That just
insures that I have an @sender variable in my body to make the email, it
doesnt go in the headers…

Calle D. wrote:

  @body['sender'] = sender

I doubt that is going to work like you want it to. “Sender:” is a
fairly magical header that’s used by MTAs to let MUAs know which
envelope sender was used, so depending on the exact details of your
mail setup that line may work as you intend, be deceptive or be
overwritten.

For my own application, I wrote a tiny plugin that monkeypatches
ActionMailer to allow setting the sender as it should be (in the SMTP
dialog for :smtp, with -f for :sendmail). I don’t have a public svn
server to easily publish it from, but I could post it here, I guess.
If there’s interest.

i went to your link to check out the plugin, it says there are no files
there. Can you upload them or point me to where they are?

On 11/27/06, Eric G. [email protected] wrote:

Oh the @body[‘sender’] = sender shouldnt effect anything. That just
insures that I have an @sender variable in my body to make the email, it
doesnt go in the headers…

Duh. Of course. I blame lack of caffeine.

Anyway, if anyone is interested in the plugin to set the real sender,
it’s on http://rubyforge.org/projects/amsenderplugin/ now.

Calle D. -*- [email protected]

Hmm, I think your plugin might be a little too elaborate. I just need to
enable all mail programs to be able to read the text/html emails i make.
I think this involves setting a multipart/alternative content type along
with entering some things into the headers. Anyone here successfully
created text/html with actionmailer that can be read through gmails
website, it cant be that hard. =) Thanks in advance.

Rails Recipes has a recipe for exactly this case. Here’s a snippet
that can help you along the way:

def multipart_alternative(recipient, name, sent_at = Time.now)

content_type “multipart/alternative”
part :content_type => “text/plain”,
:body => render_message(“multipart_alternative_plain”, :name =>
name)

part :content_type => “text/html”,
:body => render_message(“multipart_alternative”, :name => name)
end

On 11/29/06, Eric G. [email protected] wrote:

i went to your link to check out the plugin, it says there are no files
there. Can you upload them or point me to where they are?

It’s all in the anonymous SVN repo for the project:
svn://rubyforge.org/var/svn/amsenderplugin


Calle D. -*- [email protected]

Im having a lot of problems with this. With gmail, it rendering the
email incorrectly.

Here is my code:

class InviteMailer < ActionMailer::Base

def invite(email, sender)
subject sender.full_name + ’ has invited you to Mixbook.com
recipients email
from ‘[email protected]
sent_on Time.now
#content_type “multipart/alternative”
headers =
{“MIME-Version”=>1.0,“Content-Type”=>“multipart/alternative”}
body ‘sender’ => sender

  #part :content_type=> "text/plain",
   #  :body => render_message("invite_plain",:sender=>sender)

  part :content_type=> "text/html",
     :body => render_message("invite", :sender=>sender)

end
end

Here is the email information it produces:

Delivered-To: [email protected]
Received: by 10.82.105.20 with SMTP id d20cs138436buc; Sat, 2 Dec
2006 18:13:30 -0800 (PST)
Received: by 10.65.204.7 with SMTP id g7mr9566545qbq.1165112010257;
Sat, 02 Dec 2006 18:13:30 -0800 (PST)
Return-Path: [email protected]
From: [email protected]
To: [email protected]
Message-ID: [email protected]
Subject: Aryk G. has invited you to Mixbook.com
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=“----=_NextPart_000_000B_01C7163D.96B74DC0”

notice it comes out as multipart/mixed, why is that?

Don’t know if this is what’s causing your problems, but GMail removes a
lot of HTML formatting, which other programs leave in. It’s pretty near
impossible to get an HTML email to a GMail account to come out looking
like you intend. Check out
http://www.campaignmonitor.com/blog/archives/2005/11/html_email_desi.html
for ideas on working around mail readers’ issues.

Asa

Eric G. wrote:

Im having a lot of problems with this. With gmail, it rendering the
email incorrectly.

Here is the email information it produces:

Delivered-To: [email protected]
Received: by 10.82.105.20 with SMTP id d20cs138436buc; Sat, 2 Dec
2006 18:13:30 -0800 (PST)
Received: by 10.65.204.7 with SMTP id g7mr9566545qbq.1165112010257;
Sat, 02 Dec 2006 18:13:30 -0800 (PST)
Return-Path: [email protected]
From: [email protected]
To: [email protected]
Message-ID: [email protected]
Subject: Aryk G. has invited you to Mixbook.com
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=“----=_NextPart_000_000B_01C7163D.96B74DC0”

notice it comes out as multipart/mixed, why is that?

I checked out that site, it doesnt seem to help with this problem. Maybe
there is a setting I am missing when I initialize actionmailer?

Eric G. wrote:

I checked out that site, it doesnt seem to help with this problem. Maybe
there is a setting I am missing when I initialize actionmailer?

Eric,

I don’t know if this will help you but I ran into trouble with my emails
as well and posted a solution to my misery here:

http://www.ruby-forum.com/topic/83988#150502

I ended up base64 encoding the html portion of my emails.

Perhaps it will help.

Best Regards,

Steven

thats correct, but in the above code you’ll notice the mail headers get
changed, not just wants in the email. Also, facebook sends html messages
that come out fine, so i know its possible. Anybody have any ideas?