Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

gmail -- sending out html messages

by iaw4 (Monk)
on Oct 29, 2011 at 23:17 UTC ( [id://934664]=perlquestion: print w/replies, xml ) Need Help??

iaw4 has asked for the wisdom of the Perl Monks concerning the following question:

dear perl monks---I have been using the very nice Net::SMTP::TLS module to send ascii email from my gmail account.

now I want to send out some simple html email. (this is because I want to place a form into the email itself. the form itself has only 2 short questions, so I prefer including it in the email itself to sending email readers to my webpage to access the form. to be clear, I can collect the answers back on my webserver, so it is not a matter of having an action="mailto:...@..." in my form's submit tag.)

can someone please point me to an example of sending html email via gmail TLS?

sincerely, /iaw

Replies are listed 'Best First'.
Re: gmail -- sending out html messages
by anneli (Pilgrim) on Oct 30, 2011 at 00:13 UTC

    You'll need to compose a multipart MIME email, most likely. I don't know the specifics of that, but I will warn that many email clients will refuse to render (or will mangle) <form>s in HTML mail.

Re: gmail -- sending out html messages
by zentara (Archbishop) on Oct 30, 2011 at 11:37 UTC
    If you can send ascii text mail, you should be able to change the Content-type to text/html and generate your html as a here-doc, then just put the here-doc in your datasend method. Usually it's recommended to generate your html with MIME::Lite, then use your TLS datasend method to send the MIME::Lite object.

    Here is a completely untested example, that should show you the basic idea, based on the example code from the TLS module. You first create your html with MIME::Lite, then use TLS's datasend to send the MIME::Lite with the as_string method.

    use strict; use MIME::Lite; use Net::SMTP::TLS; # Send HTML document with inline images # Create a new MIME Lite object my $msg = MIME::Lite->new( From =>'foo@gmail.com', To =>'whoever@wherever.com', Subject =>'Hi', Type =>'multipart/related'); # Add the body to your HTML message $msg->attach(Type => 'text/html', Data => qq{ <BODY BGCOLOR=#FFFFFF> <H2>Hi</H2> <P ALIGN="left"> This is an HTML message. </P> <P ALIGN="left"> <A HREF="http://foo123.com/">Here's a link</A>. </P> <P ALIGN="middle"> <IMG SRC="cid:2uni2.jpg"> </P> </BODY> }); # Attach the image $msg->attach(Type => 'image/jpg', Id => '2uni2.jpg', Path => '2uni2.jpg'); # Send it with TLS my $mailer = new Net::SMTP::TLS( 'gmail.com', Port => 465, User => 'foo', Password=> 's3cr3t'); $mailer->mail('foo@gmail.com'); $mailer->to('whoever@wherever.com'); $mailer->data; # this is where you send the MIME::Lite object $mailer->datasend( $msg->as_string ); $mailer->dataend; $mailer->quit;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: gmail -- sending out html messages
by scorpio17 (Canon) on Oct 31, 2011 at 15:13 UTC

    I've had some experience trying to generate/send html newsletters, and gmail (and possibly other web-based email clients) will modify your html in ways beyond your control. These modifications are not documented (at least, no where I've been able to find), and may change without warning. Modifications usually involved things like stripping out javascript, and most css. For formatting, put everything into a table (think web layout before css was invented...). And gmail apparently strips all non-critical whitespace, so your original html becomes one long line of text, then it inserts linebreaks every 60 characters or so (sort of like Text::Wrap, I'm guessing). This will sometimes result in a non-printable character inside of an anchor tag's href attribute, which breaks the link. You can fix this by inserting bogus non-breakable spaces in your html upstream (hopefully at the end of a line where it won't show) - this is like "padding" that you can use to "bump" the bad characters outside of the link. But it's like dominoes: fixing one may break all the ones that follow. Previously, my newsletter looked great everywhere except from inside gmail. Now, I can make it look good in gmail, but it's a painful, tedious process. If anyone has a better method, I'd like to hear about it, too.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://934664]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found