正确解剖HTML电子邮件

时间:2017-12-06 20:13:37

标签: html perl email

我正在尝试使用以下代码通过perl MIME :: Lite发送以下电子邮件:

#!/usr/bin/perl5 -w

###############
sub email_it {
###############

use strict;
use MIME::Lite;

my ($recip, $body, $subj, $r_to, $ccs) = @_;

my $msg = MIME::Lite->new(
        From     =>$r_to,
        To       =>$recip,
        Cc       =>$ccs,
        Subject  =>$subj,
        Data => $body,
    Type     =>'multipart/related', 
          );

$msg->attr('content-type.charset' => 'UTF-8');

my $user = $r_to;
my $host = 'mail.somedomain.com';
my $reply;

my ($user_id, $dom) = split /@/, $user;

open CLAVE, "/usr/local/info/$user_id" or die "could not open " . $user_id . " " . $!;

chomp (my ($userid, $pass, $lname) = <CLAVE>);
close CLAVE; 

eval {

    print "Executing in eval.\n";

     ($reply) = $msg->send('smtp','mail.somedomain.com',
               AuthUser=>$user, AuthPass=>$pass);

    print "reply was: " . $reply . "\n"; # only prints here if there was no problems

};


if ($@) { # msg->send failed


    # we should also put this error in a log file
    print "*********An error occurred ($@), continuing\n";
    print "host was: " . $host . "\n";
    print "The user was: " . $user . "\n";
    print "With pss: " . $pass . "\n";
    print "user name from file was " . $r_to . "\n";
    print "We will try a different server\n";

    # try it again different smtp address
    print "Trying: " . $host . "\n";
    sleep 3;
    $host = 'mail.anotherdomain.com';

    eval {  ($reply) = $msg->send('smtp', $host,
                  AuthUser=>$user, AuthPass=>$pass); };

} # end of if ($@) {


#$msg->send('smtp','smtp.bizmail.yahoo.com', AuthUser=>$user, AuthPass=>$pass);

#######################
} # end of sub email_it
#######################

# get contents of email body
my @body;
my $file = "BODY_OUT.FROM_TMPL";
open IN, '<', $file or warn "Could not open $file: $!\n";
while (my $line = <IN>) {
    push(@body, $line);
}
close(IN) || die "Could not open $file: $!\n";

#make @body a scalar
$body = "@body"; # make body a scalar    

#print "Here comes the body: $body\n";

email_it("myemail\@gmail.com", "Hope this works", $body, "sender\@somedomain.com", "richard\@somedomain.com");

当我发送时,它会被收到,但电子邮件会被所有电子邮件客户端显示为HTML。

<html>
 <head>

     <title>Grid Master Template</title>
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
     <!--[if !mso]><!-->
     <meta http-equiv="X-UA-Compatible" content="IE=edge" />
     <!--<![endif]-->

     <style type="text/css">

         table td {border: 1px solid #DDDDDD;} 

         .ReadMsgBody { width: 100%; background-color: #F1F1F1; }
         .ExternalClass { width: 100%; background-color: #F1F1F1; }
         body { width: 100%; background-color: #f6f6f6; margin: 0; padding: 0; -webkit-font-smoothing: antialiased; font-family: Arial, Times, serif }
         table { border-collapse: collapse !important; mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
         *[class*="mobileOn"] { display: none !important; max-height: none !important; }

         @-ms-viewport{ width: device-width; }

         @media only screen and (max-width: 639px){
         .wrapper{ width:100%;  padding: 0 !important; }
         }    
         @media only screen and (max-width: 480px){ 
         .centerClass{ margin:0 auto !important; } 
         .imgClass{width:100% !important; height:auto;}    
         .wrapper{ width:320px;  padding: 0 !important; }      
         .container{ width:300px;  padding: 0 !important; }
         .mobile{ width:300px; display:block; padding: 0 !important; text-align:center; }
         .mobile50{ width:300px; padding: 0 !important; text-align:center; }
         *[class="mobileOff"] { width: 0px !important; display: none !important; }
         *[class*="mobileOn"] { display: block !important; max-height: none !important; }
         }

     </style>

     <!--[if gte mso 15]>
     <style type="text/css">
         table { font-size:1px; line-height:0; mso-margin-top-alt:1px;mso-line-height-rule: exactly; }
         * { mso-line-height-rule: exactly; }
     </style>
     <![endif]-->    

 </head>
 <body marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" yahoo="fix" style="background-color:#F2F2F2; font-family:Arial,serif;margin:0;padding:0;min-width: 100%; -webkit-text-size-adjust:none;-ms-text-size-adjust:none;">

     <!--[if !mso]><!-- -->
     <img style="min-width:640px;display:block;margin:0;padding:0" class="mobileOff" width="640" height="1" src="https://s14.postimg.org/7139vfhzx/spacer.gif">
     <!--<![endif]-->

     <!-- Start Background -->
     <table width="100%" cellpadding="0" cellspacing="0" border="0">
         <tr>
             <td width="100%" valign="top" align="center">

 <!-- Start Wrapper -----------------------------------------  GRID ONE -->

          <!-- Start Wrapper -----------------------------------------  GRID ONE -->
 <table width="640" cellpadding="0" cellspacing="0" border="0" class="wrapper" bgcolor="#FFFFFF">
     <tr>
         <td height="10" style="font-size:10px; line-height:10px;"> </td><!-- Spacer -->
     </tr>
     <tr>
         <td align="center">
             <!-- Start Container  -->
             <table width="600" cellpadding="0" cellspacing="0" border="0" class="container">
                 <tr>
                     <td width="600" class="mobile" style="font-size:12px; line-height:18px;">
                       <div align="center">
            <h2>RUSH LOGISTICS, INC - 888-448-7874 - rushlogistics.com/movie</h2>
            <h2><<< We Deliver Reliability >>></h2>
              </div>
                     </td>
                 </tr>
             </table>
             <!-- Start Container  -->                   

         </td>
     </tr>
     <tr>
         <td height="10" style="font-size:10px; line-height:10px;"> </td><!-- Spacer -->
     </tr>                        
 </table> 
 <!-- End Wrapper  --> 
 <!-- Start Wrapper -----------------------------------------  GRID ONE -->
 <table width="640" cellpadding="0" cellspacing="0" border="0" class="wrapper" bgcolor="#FFFFFF">
     <tr>
         <td height="10" style="font-size:10px; line-height:10px;"> </td><!-- Spacer -->
     </tr>
     <tr>
         <td align="center">
             <!-- Start Container  -->
             <table width="600" cellpadding="0" cellspacing="0" border="0" class="container">
                 <tr>
                     <td width="600" class="mobile" style="font-size:12px; line-height:18px;">
                       <p><strong>CUSTOMER RATE QUOTATION:</strong></p>
              <p> Attention: Fulgencio Salgado</p>
              <pre>Cosmo Films   Quote No: 0
                                                    Date:  
                </td>
                 </tr>
             </table>
             <!-- Start Container  -->                   
         </td>
     </tr>
     <tr>
       <td height="10" style="font-size:10px; line-height:10px;"> </td><!-- Spacer -->
     </tr>                        
 </table> 
 <!-- End Wrapper  --> 

  <!-- Start Wrapper  -->
  <table width="640" cellpadding="0" cellspacing="0" border="0" class="wrapper" bgcolor="#FFFFFF">
      <tr>
          <td height="10" style="font-size:10px; line-height:10px;"> </td> <!-- Spacer -->
      </tr>
      <tr>
          <td align="center">

              <!-- Start Container  -->
              <table width="600" cellpadding="0" cellspacing="0" border="0" class="container">
                  <tr>
                      <td width="200" class="mobile" style="font-size:12px; line-height:18px;">
                        <h3>Origin</h3>
             </td>
                      <td width="200" class="mobile" style="font-size:12px; line-height:18px;">
                         <h3>Destination</h3>
                      </td>
                      <td width="200" class="mobile" style="font-size:12px; line-height:18px;">
                         <h3>Base Price</h3>
                      </td>                    
                  </tr>
              </table>
              <!-- Start Container  -->                   

          </td>
      </tr>
      <tr>
          <td height="10" style="font-size:10px; line-height:10px;"> </td> <!-- Spacer -->
      </tr>                        
  </table> 
  <!-- End Wrapper  -->

   <!-- Start Wrapper  -->
  <table width="640" cellpadding="0" cellspacing="0" border="0" class="wrapper" bgcolor="#FFFFFF">
      <tr>
          <td height="10" style="font-size:10px; line-height:10px;"> </td> <!-- Spacer -->
      </tr>
      <tr>
          <td align="center">

              <!-- Start Container  -->
              <table width="600" cellpadding="0" cellspacing="0" border="0" class="container">
                <tr>
                  <td width="200" class="mobile" style="font-size:12px; line-height:18px;">
                    Addison,  IL
                  </td>
                  <td width="200" class="mobile" style="font-size:12px; line-height:18px;">
                    Whitestown,  IN
                  </td>
                  <td width="200" class="mobile" style="font-size:12px; line-height:18px;">
                    695.00
                  </td>                    
                  </tr>
              </table>
              <!-- Start Container  -->                   

          </td>
      </tr>
      <tr>
          <td height="10" style="font-size:10px; line-height:10px;"> </td> <!-- Spacer -->
      </tr>                        
  </table> 
  <!-- End Wrapper  -->   

  <!-- Start Wrapper  -->
 <table width="640" cellpadding="0" cellspacing="0" border="0" class="wrapper" bgcolor="#FFFFFF">
     <tr>
         <td height="10" style="font-size:10px; line-height:10px;"> </td><!-- Spacer -->
     </tr>
     <tr>
         <td align="center">

             <!-- Start Container  -->
             <table width="600" cellpadding="0" cellspacing="0" border="0" class="container">
                 <tr>
                     <td width="600" class="mobile" style="font-size:12px; line-height:18px;">
                       <strong>* Plus fuel sur charge (currently 16.5%) TOTAL:</strong>
            <p>Thank you for the opportunity to quote your business.</p>
            <p>
              Sincerely,  
            </p>
            <p>
              Richard Reina
            <p/>
            <p>
              Watch our 3 minute movie and learn about the Rush Logistics difference.
              rushlogistics.com/movie
            </p>
            <p>
              *Rate not valid for a collect shipment to a non current Rush Logistics customer
            </p>
            </td>
                 </tr>
             </table>
             <!-- Start Container  -->                   

         </td>
     </tr>
     <tr>
         <td height="10" style="font-size:10px; line-height:10px;"> </td><!-- Spacer -->
     </tr>                        
 </table> 
 <!-- End Wrapper  --> 



             </td>
         </tr>
     </table>
     <!-- End Background -->

 </body>
 </html>

之前我使用MIME :: Lite发送了更简单的电子邮件,如下面的那些,它们可以直观地显示。我怀疑这是一个非常基本的东西,我错过但无法弄清楚是什么。

<hr/>
<pre>
          Richard Reina 1808 W Buckingham. Chicago, IL 60657

</pre>
<hr/>

<p><strong>ANNOINCEMENT:                                </strong>

</p><p> Attention: Residents
</p>

</p>Building water will be shut off from 9am to 10:30am</p>
 <p> Thank you for the opportunity to quote your business.

</p><p> Sincerely,

</p><p> Richard Reina

</p>

1 个答案:

答案 0 :(得分:0)

考虑将<!DOCTYPE html>添加到第一行。

有关DOCTYPE序言的说明,请参阅http://w3c.github.io/html/syntax.html#the-doctype

此外,根据http://search.cpan.org/~rjbs/MIME-Lite-3.030/lib/MIME/Lite.pm,不再建议使用MIME :: Lite。