python+Html 发送带表格的邮件

    def create_pcn_html(self, pcn, pcn_classfy):
        """
        @author: 
        @date: 2023-07-28 15:57:01
        @description: 设置邮件发送HTML模板,拼接html,设计拼接邮件内容
        """
        html = """
                    <!DOCTYPE html>
                    <html lang="en">
                    <body>
                        <div style="margin-bottom:16px;">
                            <div><b><font size="3"> Hi, all,该邮件为XXXXX!</font></b></div>
                            <div>
                                <p class="p1" style="text-indent:2em;"> 1.XXXX:<font color="red" size="3">{}</font> </p> 
                                <p class="p1" style="text-indent:2em;"> 2. XXXX: 
                                    <font color="blue" size="3">{}</font> 
                                    XXXXXXXXXXX。 </p> 
                                <p class="p1" style="text-indent:2em;"> 3. XXXXX:{} </p> 
                                <p class="p1" style="text-indent:2em;"> 4. XXXXX:{} </p> 
                                <p class="p1" style="text-indent:2em;"> 5. XXXXX:{} </p> 
                             </div>
                            <table width="100%" height="10px" align="left" border="1" cellpadding="10px" cellspacing="0">
                                <tr>
                                    <th>ID</th><th>表头1</th>
                                    <th>表头2</th><th>表头3</th>
                                    <th>表头4</th><th>表头5</th>
                                    <th>表头6</th><th>表头7</th>
                                </tr>
                                {}  

                            </table>
                        </div>
                    </body>
                     </html>
                    """
        table_info = self.get_table_info(pcn_classfy)
        pcn_html = html.format(pcn.ig_ec_type, pcn.ig_change, pcn.ig_mpn, pcn.ig_sqe_remark, pcn.ig_ce_remark,
                               '\n'.join(table_info))
        return pcn_html

    def get_table_info(self, pcn_classfy):
        text = """<tr><td align='center'>{}</td><td align='center'>{}</td><td align='center'>{}</td>
                                <td align='center'>{}</td><td align='center'>{}</td><td align='center'>{}</td>
                                <td align='center'>{}</td><td align='center'>{}</td>
                           </tr>"""

        table_info = []
        for id, record in enumerate(pcn_classfy):
            kv_replace_map = {
                "H": "<font color='red'>H(重点关注)</font>",
                "L": "<font color='blue'>L(一般关注)</font>",
                "N": "<font color='green'>N(无影响)</font>",
            }
            result = [kv_replace_map[i] if i in kv_replace_map else i for i in record]
            data = text.format(id + 1, result[0], result[1], result[2], result[3], result[4], result[5], result[6])
            table_info.append(data)
        return table_info

### 使用Python通过SMTP库发送嵌入HTML表格邮件 为了实现这一功能,可以利用`email.mime.text.MIMEText`模块来构建包含HTML内容的消息体,并使用`smtplib.SMTP`来进行邮件发送操作[^1]。 下面是一个简单的例子展示怎样创建并发送一封含有HTML表格格式的内容给指定收件人的方法: ```python import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText def send_email_with_html_table(sender, password, receiver, subject, html_content): msg = MIMEMultipart('alternative') msg['Subject'] = subject msg['From'] = sender msg['To'] = receiver part = MIMEText(html_content, 'html') msg.attach(part) try: server = smtplib.SMTP_SSL('smtp.gmail.com', 465) server.login(sender, password) server.sendmail(sender, receiver, msg.as_string()) print("Email sent successfully!") except Exception as e: print(f"Failed to send email: {e}") finally: server.quit() if __name__ == "__main__": # Example usage sender_address = "your.email@gmail.com" app_password = "your_app_specific_password" recipient_address = "recipient@example.com" sample_data = [ ["Header1", "Header2"], ["Row1 Col1", "Row1 Col2"], ["Row2 Col1", "Row2 Col2"] ] table_rows = "".join([f"<tr><td>{item}</td></tr>" for sublist in sample_data for item in sublist]) html_body = f""" <html> <head></head> <body> <p>Here's your data:</p> <table border="1"> {table_rows} </table> </body> </html> """ send_email_with_html_table( sender=sender_address, password=app_password, receiver=recipient_address, subject="Test Email With HTML Table", html_content=html_body ) ``` 此脚本定义了一个名为`send_email_with_html_table()`函数用于设置邮件主题、发件人地址以及接收者邮箱地址等基本信息;同时接受一段由用户提供的HTML字符串作为邮件正文的一部分。这里还展示了如何构造一个基本的HTML表格结构并通过列表推导式将其转换成适合嵌入到HTML文档中的形式。 请注意,在实际应用中应当妥善保管账户凭证信息,并考虑采用更安全的方式处理敏感数据(如环境变量)。此外,对于Gmail用户来说可能还需要启用两步验证并生成应用程序专用密码以便成功登录SMTP服务器。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值