Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Sending HTML e-mail

by Anonymous Monk
on Dec 26, 2003 at 14:54 UTC ( [id://317094]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks
I am trying to grab a html page and send it as a html e-mail, but it's not working properly. I am sending it, but the page doesn't show as html, it's showing the source html code instead. Can anyone tell what is wrong here or how I could accomplish this better? Thanks a lot!!!!
Here is my code:

my $content = get( "http://pagetest.html" ) or die $!; my @mailto; $mailto[0]="ok1\@ok.com"; $mailto[1]="ok2\@ok.com"; $mailto[2]="ok3\@ok.com"; foreach my $element (@mailto) { my $sock = IO::Socket::INET->new (PeerAddr => 'my.mail.server.ip', Type =>'Content-type: text/html; charset=us-ascii' +, PeerPort => 'smtp(25)', Proto => 'tcp')|| die "That sucks, I can't open a +port on the mail server. I'm going to take all my toys and go home no +w.."; my $input = <$sock>; print $sock "HELLO localhost\r\n"; $input = <$sock>; print $sock "mail from:master-ok\@ok.com\r\n"; $input = <$sock>; print $sock "rcpt to:$element\r\n"; print "rcpt to:$element\r\n"; $input = <$sock>; print $sock "DATA\r\n"; print $sock "Subject: Hell is breaking loose!"; print $sock "From: Server Hell\r\n"; print $sock "To: $element\r\n"; print $sock "\r\n"; print $sock "$content\r\n"; print $sock ".\r\n"; $input = <$sock>; print $sock "QUIT"; close ($sock); }

Thanks....

Replies are listed 'Best First'.
Re: Sending HTML e-mail
by jeffa (Bishop) on Dec 26, 2003 at 15:24 UTC
    You are indeed specifying a content type of 'text/html' ...

    <UPDATE>
    but that is the wrong place to set it! The Type attribute in the IO::Socket::INET constructor is for the socket type, not the content type!
    </UPDATE>

    There is most definitely a better way to send email. It's called MIME::Lite:
    use strict; use warnings; use LWP::Simple; use MIME::Lite; my $html = get('http://site.com/pagetest.html') or die "no html\n"; my @mailto = map "ok$_\@ok.com", (1..3); for my $to (@mailto) { my $msg = MIME::Lite->new( From => 'master-ok@ok.com', To => $to, Type => 'text/html', Subject => 'Hell is breaking loose!', Data => $html, ); $msg->send or die "couldn't send message to '$to'\n"; }
    UPDATED code: put into for loop to try and mimic the seeker's code.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      I tried the MIME::Lite approach and got back - error closing /usr/lib/sendmail: (exit 17408)

      Any ideas on how to fix this ???

        I am afraid i do not. The error indicates that the problem lies within the sendmail app itself. I would try to upgrade your system sendmail if possible, or try to use another app (google for "sendmail alternatives").

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Sending HTML e-mail
by tcf22 (Priest) on Dec 26, 2003 at 15:19 UTC
    There is no need to roll your own mailer. There are an assortment of modules that already do this for you. I personally use Mail::Sender. This module allows you to set the content-type to text/html.

    - Tom

      On this topic, does anyone have any code samples for sending html with a stylesheet/css attachment?

      I'm having trouble getting the html to show inline with OpenMultipart (maybe I'm not understanding all the multi-part options)

Re: Sending HTML e-mail
by exussum0 (Vicar) on Dec 26, 2003 at 15:51 UTC
    Since everyone was kind enough to tell you how to get it to work..

    Your code won't work because of a particular reason. You see how your headers have a Subject, a From and a To? There's no Content-type nor Mime version in your email. I'm sure you can deal w/o the mime version for most mailers, but your header for content-type is missing there.

    Also, the type you are specifying in your INET connection, that shouldn't be a mime header, that should be the socket type. perldoc IO::Net..

    IO::Socket::INET IO::Socket::INET provides a constructor to create an AF_INET domain socket and some related methods. The constructor can take the followin +g options PeerAddr Remote host address <hostname>[:<port>] PeerPort Remote port or service <service>[(<no>)] | <no> LocalAddr Local host bind address hostname[:port] LocalPort Local host bind port <service>[(<no>)] | <no> Proto Protocol name (or number) "tcp" | "udp" | ... Type Socket type SOCK_STREAM | SOCK_DGRAM | .. Listen Queue size for listen Reuse Set SO_REUSEADDR before binding Timeout Timeout value for various operations

    Play that funky music white boy..
Re: Sending HTML e-mail
by pijush (Scribe) on Dec 27, 2003 at 10:43 UTC
    Hi!
    You can try following code.
    use Net::SMTP; open (IN, "your html file") or die "I can not open file:$!\n"; #Get the file content in an array my @Message = <IN>; close (IN); my @MailID = <mail ID list>; #sending mail my $Mail = Net::SMTP->new("<your smtp mail server IP>"); $Mail -> mail($ENV{USER}); $Mail -> to (@MailID); $Mail -> data(); $Mail -> datasend ("Subject:<subject>\n"); $Mail -> datasend ("To: @MailID\n"); $Mail -> datasend ("From:<Sender name>\n"); $Mail -> datasend ("Mime-Version: 1.0\;\n"); $Mail -> datasend ("Content-Type: text/html\; charset=\"ISO-8859-1\"\; +"); $Mail -> datasend ("Content-Transfer-Encoding: 7bit\;"); $Mail -> datasend ("\n\n"); #This is important $Mail -> datasend ("@Message\n"); $Mail -> dataend(); $Mail -> quit;
    Bye. -Pijush

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-28 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found