有没有办法使Yagmail在邮件正文上正确显示HTML内容?

时间:2019-02-05 17:23:13

标签: python html css yagmail

我能够正确发送带有附件的邮件,HTML作为邮件正文,一切正常。

问题是CSS样式不会显示为HTML内容,而是在邮件正文中显示为纯文本,并在行之间添加一些空格。

即使我禁止所有CSS内容并“简化” html,空格仍会出现。

这是代码:

import yagmail

#------------------------------------------------------------------------------
# HTML.
BODY = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Factura electrónica - XXXXXXXXXXXX</title>
  <style type="text/css">
  body {
   padding-top: 0 !important;
   padding-bottom: 0 !important;
   padding-top: 0 !important;
   padding-bottom: 0 !important;
   margin:0 !important;
   width: 100% !important;
   -webkit-text-size-adjust: 100% !important;
   -ms-text-size-adjust: 100% !important;
   -webkit-font-smoothing: antialiased !important;
 }
 .tableContent img {
   border: 0 !important;
   display: block !important;
   outline: none !important;
 }
 a{
  color:#382F2E;
}

p, h1,h2,ul,ol,li,div{
  margin:0;
  padding:0;
}

h1,h2{
  font-weight: normal;
  background:transparent !important;
  border:none !important;
}

@media only screen and (max-width:480px)

{

table[class="MainContainer"], td[class="cell"] 
    {
        width: 100% !important;
        height:auto !important; 
    }
td[class="specbundle"] 
    {
        width: 100% !important;
        float:left !important;
        font-size:13px !important;
        line-height:17px !important;
        display:block !important;
        padding-bottom:15px !important;
    }   
td[class="specbundle2"] 
    {
        width:80% !important;
        float:left !important;
        font-size:13px !important;
        line-height:17px !important;
        display:block !important;
        padding-bottom:10px !important;
        padding-left:10% !important;
        padding-right:10% !important;
    }

td[class="spechide"] 
    {
        display:none !important;
    }
        img[class="banner"] 
    {
              width: 100% !important;
              height: auto !important;
    }
        td[class="left_pad"] 
    {
            padding-left:15px !important;
            padding-right:15px !important;
    }

}

@media only screen and (max-width:540px) 

{

table[class="MainContainer"], td[class="cell"] 
    {
        width: 100% !important;
        height:auto !important; 
    }
td[class="specbundle"] 
    {
        width: 100% !important;
        float:left !important;
        font-size:13px !important;
        line-height:17px !important;
        display:block !important;
        padding-bottom:15px !important;
    }   
td[class="specbundle2"] 
    {
        width:80% !important;
        float:left !important;
        font-size:13px !important;
        line-height:17px !important;
        display:block !important;
        padding-bottom:10px !important;
        padding-left:10% !important;
        padding-right:10% !important;
    }

td[class="spechide"] 
    {
        display:none !important;
    }
        img[class="banner"] 
    {
              width: 100% !important;
              height: auto !important;
    }
        td[class="left_pad"] 
    {
            padding-left:15px !important;
            padding-right:15px !important;
    }

}

.contentEditable h2.big,.contentEditable h1.big{
  font-size: 26px !important;
}

 .contentEditable h2.bigger,.contentEditable h1.bigger{
  font-size: 37px !important;
}

td,table{
  vertical-align: top;
}
td.middle{
  vertical-align: middle;
}

a.link1{
  font-size:13px;
  color:#27A1E5;
  line-height: 24px;
  text-decoration:none;
}
a{
  text-decoration: none;
}

.link2{
color:#ffffff;
border-top:10px solid #27A1E5;
border-bottom:10px solid #27A1E5;
border-left:18px solid #27A1E5;
border-right:18px solid #27A1E5;
border-radius:3px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
background:#27A1E5;
}

.link3{
color:#555555;
border:1px solid #cccccc;
padding:10px 18px;
border-radius:3px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
background:#ffffff;
}

.link4{
color:#27A1E5;
line-height: 24px;
}

h2,h1{
line-height: 20px;
}
p{
  font-size: 14px;
  line-height: 21px;
  color:#000000;
}

.contentEditable li{

}

.appart p{

}
.bgItem{
background: #ffffff;
}
.bgBody{
background: #ffffff;
}

img { 
  outline:none; 
  text-decoration:none; 
  -ms-interpolation-mode: bicubic;
  width: auto;
  max-width: 100%; 
  clear: both; 
  display: block;
  float: none;
}

</style>


<script type="colorScheme" class="swatch active">
{
    "name":"Default",
    "bgBody":"ffffff",
    "link":"27A1E5",
    "color":"AAAAAA",
    "bgItem":"ffffff",
    "title":"444444"
}
</script>


</head>
<body paddingwidth="0" paddingheight="0"   style="padding-top: 0; padding-bottom: 0; padding-top: 0; padding-bottom: 0; background-repeat: repeat; width: 100% !important; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; -webkit-font-smoothing: antialiased;" offset="0" toppadding="0" leftpadding="0">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td><table width="600" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#ffffff" style="font-family:helvetica, sans-serif; border:1px solid #eaeaea" class="MainContainer">
      <!-- =============== START HEADER =============== -->
  <tbody>
    <tr>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td valign="top" width="20">&nbsp;</td>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td class="movableContentContainer">
      <div class="movableContent" style="border: 0px; padding-top: 0px; position: relative;">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td height="15"></td>
    </tr>
    <tr>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td valign="top" width="100%"><img src="http://www.xxxxxxxxxxx.com/emailings/factura/images/logo.png" alt="Logo" title="Logo" width='100%' height='100%'><br>
    </tr>
  </tbody>
</table>
</td>
    </tr>
  </tbody>
</table></td>
    </tr>

    <tr>
       <td ><hr style='height:1px;background:#DDDDDD;border:none;'></td>
     </tr>
  </tbody>
</table>
      </div>
      <!-- =============== END HEADER =============== -->
<!-- =============== START BODY =============== -->
 <div class="movableContent" style="border: 0px; padding-top: 0px; position: relative;">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td class="specbundle"><table width="100%" cellpadding="0" cellspacing="0" align="center">
                      <tr>
                        <td>
                          <div class='contentEditableContainer contentTextEditable'>
                            <div class='contentEditable' style='text-align: left;'>
                              <h2 style='font-size:14px;'><strong>Estimado Cliente:</strong></h2>
                              <br>
                              <p style="font-size: 14px;">Le informamos que ya se encuentra disponible su factura con vencimiento el <strong>XXXXXX</strong>, puede encontrarla adjunta en este e-mail.</p>
                              <br>
                             </div>
                          </div>
                        </td>
                      </tr>
                    </table></td>
    </tr>
  </tbody>
</table>
</td>
    </tr>
  </tbody>
</table>
      </div>       
       <div class="movableContent" style="border: 0px; padding-top: 0px; position: relative;">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
   <tr>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
     <td><table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
                      <tr>
                        <td style='text-align: center;'>
                          <div class='contentEditableContainer contentTextEditable'>
                            <div class='contentEditable' style='text-align: left;'>                                                     
                              <p style="font-size: 14px;">Puede realizar el pago en forma rápida y sencilla en los siguientes lugares habilitados:</p>
                            </div>
                          </div>
                        </td>
                      </tr>
                   </table></td>
    </tr>
  </tbody>
</table>
</td>
    </tr>
  </tbody>
</table>
      </div>
      <div class="movableContent" style="border: 0px; padding-top: 0px; position: relative;">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td height="10"></td>
    </tr>
    <tr>
      <td valign="top" width="580"><div class='contentEditableContainer contentImageEditable'>
                      <div class='contentEditable' style="text-align: center;"><img class="banner" src="http://www.xxxxxxxxx.com/emailings/factura/images/canales.jpg" alt="Canales" title="Canales" width="580" height="221" border="0"></div></div></td>
    </tr>
  </tbody>
</table>
     </div>
      <div class="movableContent" style="border: 0px; padding-top: 0px; position: relative;">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
  </tbody>
</table>
     </div>
      <div class="movableContent" style="border: 0px; padding-top: 0px; position: relative;">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td style="background:#F6F6F6; border-radius:6px;-moz-border-radius:6px;-webkit-border-radius:6px"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
  </tbody>
</table>
</td>
    </tr>
  </tbody>
</table>
     </div>
      <!-- =============== END BODY =============== -->
<!-- =============== START FOOTER =============== -->
    <div class="movableContent" style="border: 0px; padding-top: 0px; position: relative;">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td height="48"></td>
    </tr>
    <tr>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td valign="top" width="90" class="spechide">&nbsp;</td>
      <td><table width="100%" cellpadding="0" cellspacing="0" align="center">
                      <tr>
                         <td>
                         <table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <tbody>
                                <tr>
                                  <td class="specbundle2"><table width="100%" cellpadding="0" cellspacing="0" align="center">
                                                              <tr>
                                                                <td>
                                                                  <div class='contentEditableContainer contentFacebookEditable' align="center">
                                                                    <div class='contentEditable' align="center" style='text-align: center;color:#AAAAAA;'>
                                                                      <a href="https://www.facebook.com/xxxxxxx/" target="_blank"><img src="http://www.xxxxxxxxxx.com/emailings/factura/images/facebook.png" alt="facebook" width='40' height='40' data-max-width="40" data-customIcon="true" border="0" style="display:inline-block    " ></a>
                                                                    </div>
                                                                  </div>
                                                                </td>

                                                              </tr>
                                                            </table>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                         </td>
                      </tr>
                      <tr>
                        <td>
                          <div class='contentEditableContainer contentTextEditable'>
                            <div class='contentEditable' style='text-align: center;color:#AAAAAA;'>
                              <p>
                                <strong>Atención telefónica: <br>(xxxxx) 43-1902/4 de lunes a viernes entre las 8 y 17 hs. <br>
Emergencias: (xxxxx) xxx60916 las 24 hs.</strong> <br/>

                              <br/><br/>
                              <span><strong>xxxxxxxxxx</strong></span><br/>
                              Av. xxxxxxxxxxxx Nº xxxx entre X y X - <br>xxxxxxx, de lunes a viernes de 7:30 a 14:00 hs. <br>
                              Email: consultas@xxxxxxxxx.com  <br/>
                              </p>
                            </div>
                          </div>
                    </td>
                      </tr>
                    </table></td>
      <td valign="top" width="90" class="spechide">&nbsp;</td>
    </tr>
  </tbody>
</table>
</td>
    </tr>
  </tbody>
</table>
     </div>
      <div class="movableContent" style="border: 0px; padding-top: 0px; position: relative;">

      <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td>
</td>
    </tr>
    <tr>
        <td height='40'></td>
    </tr>
  </tbody>
</table>
     <!-- =============== END FOOTER =============== --> 
      </div>
      </td>
    </tr>
  </tbody>
</table>
</td>
      <td valign="top" width="20">&nbsp;</td>
    </tr>
  </tbody>
</table>
</td>
    </tr>
  </tbody>
</table>
</td>
    </tr>
  </tbody>
</table>
  </body>
  </html>
"""

yag = yagmail.SMTP({'xxxxx@yyyyy.com' : 'Name'},
                   'password', 'mail.xxxx.com', 25, None, False)
contents = [BODY, 'Test message']
yag.send('xxx@yyyy.com', str('Test message'), contents)

谢谢!

0 个答案:

没有答案