Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

HTML/Plain Text mail

by Anonymous Monk
on Jan 04, 2002 at 22:23 UTC ( [id://136332]=perlquestion: print w/replies, xml ) Need Help??

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

Howdy:

I am writing an application that will send out several hundred emails to subscribers of a service. I have no idea how the recipient is viewing the mail so I need to send this in a format that is viewable in a non-html reader. I know that MIME supports a multi-piece email, but I am searching for a Perl package and a little guidance as to how to do this. Any input is greatly appreciated.

Thanks,
TAC

Replies are listed 'Best First'.
Re: HTML/Plain Text mail
by arhuman (Vicar) on Jan 04, 2002 at 22:34 UTC
    A good way is probably Mail::Sender as AidanLee already pointed out below.

    If you (really have to) do it "by hand" using low level modules,
    You'll have to give this kind of struct to the body :
    --$boundary Content-Type: multipart/alternative; boundary=\"$boundary2\" --$boundary2 Content-Type: text/plain; charset="..." Content-Transfer-Encoding: quoted-printable txt goes here --$boundary2 Content-Type: text/html; charset="..." Content-Transfer-Encoding: quoted-printable html goes here --$boundary2-- --$boundary--

    Don't forget to add the :
    content-type: multipart/mixed; boundary=\"$boundary\"
    in the header.

    Hopes this helps...

    UPDATE :
    Added the Mail::Sender mention.
    UPDATE2 :
    Noticed that it was already given by AidanLee.I gave credits.

    "Only Bad Coders Code Badly In Perl" (OBC2BIP)
Re: HTML/Plain Text mail
by gav^ (Curate) on Jan 04, 2002 at 23:13 UTC
    I do this on a weekly basis, but with several hundred thousand emails :)

    One thing I've found is that MIME support in email clients is very poor and you have to be careful what you send. We use tricks like putting invisible plain text at the top of the HTML so that people with broken clients can still read it. MIME::Lite is a handy module for simple MIME messages.

    use MIME::Lite; my $msg = MIME::Lite->new(Type => 'multipart/alternative'); $msg->attach(Type => 'text/plain', Data => $msg_text); $msg->attach(Type => 'text/html', Data => $msg_html, Encoding = 'quote +d-printable'); $msg->scrub; $msg_mime = $msg->as_string;
    The call to scrub seems to help it work a bit better. I prefer to use the Mail::Bulkmail module which makes it easy to send messages to multiple recipients.

      Say gav^, you're not in any way responsible for some of the 40+ spams (over my several emails) I have to throw away each week, are you? :)

      dmm

      Thanks... Actually the process I am working on may potentially send out a few millions emails a week so performance is a huge concern. I have been using MIME::Lite and it is easy and works well. I found the 'multipart/alternative' flag in the Perldoc for Mime::Lite. My only question is that by using this approach you are potentially doubling the size of the email. Will this cause evil things to happen to the smtp server? Also, is there a way to compress the email?

      Thanks again,
      TAC
        Basically I split everything up into 4 messages:
        • Text people
        • AOL people, they get the text message but with the links/mailto's converted into HTML. This seems to give them clickable links which is nice.
        • HTML
        • Unknown people get MIME - use MIME::Lite to give them a multipart/alternative message.
        The text message tends to be pretty small, it's just a short bit of text to try to encourage somebody to go to a link. We work out if somebody is Text/HTML by embedding an invisible pixel in the email and giving them a couple of mailings to load it.

        If you are sending out hundreds of thousands of messages there are serious network/server issues. When we first started we killed out network as our firewall didn't have enough performance to handle a large amount of small connections.

        There is also the problem that 500,000 emails tends to be about 8 gig. This tends to be a very I/O bound operation and takes quite a while.

        How we handle things is we run postfix as our mail server. We first spool everything (without sending a single message) then let it go. We then throttle it so we're sending 40,000 or so an hour without killing our T1.

        There's also issues with web server load. It killed our client's webserver when it got that much extra traffic going there. There's also the problem about handling unsubscribes etc.

        And no, I don't spend all day sending spam :)

        Well that sounds like spam! :) If I may...to what purpose?

        This definitely does double the size of the emails...you've reminded me of something that Apache does...there is a negotiation compression with clients that have compression enable to cut down the costs of bandwidth. I wonder if there is a related thing with SMTP and POP/IMAP?

        Something to look into, anyway.

        Kickstart

      This is about how I do it, too. I have one suggestion-- make sure that the html message string has no newlines in it.

      I don't remember which ones (maybe hotmail??), but some mailreaders display the html of multipart messages with a really screwy layout if there are newlines in there. So, s/\n\t//g is your friend. (If you're getting the text of the mail from folks who write it up in M$ products, like Word, then you want to expand that regex to strip out more characters-- Word puts in funky ^M's and stuff that you'll want to remove as well.)

      -- cat

Re: HTML/Plain Text mail
by Stegalex (Chaplain) on Jan 05, 2002 at 00:49 UTC
    I too send out several thousand opt-in emails per week so I have to put my two cents in as well. If your mailing list is opt-in (may you fry in hell if it's not) then you should ask the subscriber whether they want to receive text or html format. Bottom-line, this is better than trying to get cute by using content-type mime/alternative primarily because the AOL/Hotmail/and Yahoo do a bad job of reading your headers (and if you look at your mailing list, you will find that AOL/Hotmail/and Yahoo domains account for a large percentage of your audience. Also, there is a virus going around right now in a message with a mime/alternative header, so I dump everything with that header right into the trash unopened.

    Better yet, just send everything as text.

    Lest you all berate me as a Luddite or amateur, I did build a fancy/schmancy mime/alternative mailer that applies some smarts to the message format based on the domain, but I sometimes get replies from people saying that they can't read my message and it is often due to the mimetype.

    Also, I would be interested to hear about any difficulties other people have encountered in sending spam to hotmail accounts.
    I like chicken.
Re: HTML/Plain Text mail
by AidanLee (Chaplain) on Jan 04, 2002 at 22:44 UTC
    I believe the documentation for Mail::Sender has some information/examples on this.
Re: HTML/Plain Text mail
by wazoo (Novice) on Jan 04, 2002 at 23:25 UTC
    The Mail::Sendmail module will also do plain text/html messages. The author has a FAQ page that explains building alternative mime messages, as well as attachments, etc. I have used the module sucessfully several times.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found