Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

html format email

by GordonLim (Acolyte)
on Apr 05, 2013 at 09:41 UTC ( [id://1027109]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am trying to send my html code to my mail but the result is the code also display as test in my mail and cannot convert to format. Hope you can help me on this, below is the Perl code I using.
my $smtp; my $part; my $pingStatus; my $localcount; my $subject = "Welcome to Booking Request"; $pingStatus = &checkTargetPing("mail.hotmail.com"); if ($pingStatus eq 1) { $smtp = Net::SMTP::TLS->new('mail.radisys.com'); $smtp->mail(@email); $smtp->to(@email); $smtp->mail('jason@hotmail.com'); # $smtp->to('jason@hotmail.com'); $smtp->data(); $smtp->datasend("From:Booking and Managment system\n"); $smtp->datasend("To: jason@hotmail.com\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); $smtp->datasend("<h1>Hello</h1>\n"); $smtp->quit; }
The email I received it just display <h1>Hello</h1> , I want to convert it only show out Hello as header. Thanks

Replies are listed 'Best First'.
Re: html format email
by Corion (Patriarch) on Apr 05, 2013 at 09:45 UTC

    You need to learn about MIME. A good way of sending both, text and HTML parts in a mail is MIME::Lite. Despite what its documentation says, it is one of the better APIs.

      And if you're sending the HTML email to many recipients, you need to realize that the annoying way browsers render HTML is a glorious bastion of consistent standard conformance compared to the way email readers render HTML (that MSOutlook uses the HTML renderer from MSWord just boggles). MailChimp has a decent write up.

      -derby

      update: bad link to mailchimp updated.

Re: html format email
by golux (Chaplain) on Apr 05, 2013 at 13:47 UTC
    Hi GordonLim,

    Corion is right, MIME::Lite is definitely the way to go.

    Here's a short example using your data that may help:

    use strict; use warnings; use MIME::Lite; my $subject = "Welcome to Booking Request"; my $sender = 'myself@this_host.this_domain'; my $recipient = 'jason@hotmail.com'; my $data = qq{ <h1>Hello</h1> }; my $mime = MIME::Lite->new( 'From' => $sender, 'To' => $recipient, 'Subject' => $subject, 'Type' => 'text/html', 'Data' => $data, ); $mime->send() or die "Failed to send mail\n";
    say  substr+lc crypt(qw $i3 SI$),4,5
      Hi, Finally I successed to print out html formal table. But now I'm facing another new problem is how to add function in the html? exmaple add in foreach function or I try to add
      open(INFO, "data2.txt"); # Open db for reading @array=<INFO>; close (INFO); @ascend=sort(@array);
      it just display this code out and not working as function. below is my example script:
      my $data = qq{ open(INFO, "data2.txt"); # Open db for reading @array=<INFO>; close (INFO); @ascend=sort(@array); <h1 border=\"1\" align=\"center\">Welcome to ATCA Booking and man +agment system<br>Chassis information and detail</h1> <body> <TABLE cellSpacing=\"0\" cellPadding=\"0\" border=\"1\" align=\"c +enter\"> <TR align=\"center\"><TD>No</TD><TD>version</TD><TD>Chassis</TD><T +D>Assigned</TD><TD>CMMIP1</TD><TD>CMMIP2</TD><TD>CMM1<br>SerialPortCo +nnection</TD><TD>CMM2<br>SerialPortConnection</TD><TD>Ibootbar_Altuse +n</TD></TR> foreach $line (@ascend){ ($version,$okok)=split(/\|/,$line); <TR><TD align=\"center\"> $i++ </TD><TD> $version </TD> } </table> };
        You've mixed data and code, which won't work. Perhaps you're confusing perl with php?

        What you want to do is separate the data and code, having the code process the data. Something like:

        use strict; use warnings; my $data = qq{ <h1 border=\"1\" align=\"center\">Welcome to ATCA Booking and man +agment system<br>Chassis information and detail</h1> <body> <TABLE cellSpacing=\"0\" cellPadding=\"0\" border=\"1\" align=\"c +enter\"> <TR align=\"center\"><TD>No</TD><TD>version</TD><TD>Chassis</TD><T +D>Assigned</TD><TD>CMMIP1</TD><TD>CMMIP2</TD><TD>CMM1<br>SerialPortCo +nnection</TD><TD>CMM2<br>SerialPortConnection</TD><TD>Ibootbar_Altuse +n</TD></TR> </table> }; # Open db for reading -- but die if there's an error open(INFO, "data2.txt") or die "Failed to open 'data2.txt' ($!)\n"; my @array = <INFO>; close (INFO); my @ascend=sort(@array); my $i = 1; foreach my $line (@ascend){ my ($version, $okok) = split(/\|/,$line); $data .= qq{ <TR><TD align=\"center\"> $i </TD><TD> $version </TD> }; ++$i; } print "DATA:\n $data\n";

        Update: put back $i, which I now see was in each first <td>

        say  substr+lc crypt(qw $i3 SI$),4,5
Re: html format email
by hdb (Monsignor) on Apr 05, 2013 at 10:24 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (9)
As of 2024-04-19 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found