6.1 批量发送邮件-工资条

def send_email():
    # smtplib email
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    from email.utils import formataddr
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart

    # 登录
    smtp_obj = smtplib.SMTP('smtp.qq.com')
    smtp_obj.login('360xxxxxx@qq.com', 'xxxxxxxxxx')

    # 工资条
    from openpyxl import load_workbook,Workbook

    wb = load_workbook('./办公自动化/files/工资表.xlsx',data_only=True)
    sh = wb['Sheet1']
    for row in sh[2:3]:
        row_list = []
        for cell in row:
            # print(cell.value)
            row_list.append(cell.value)

        table_header = '''
                <style>
                    table {
                        border-right: 1px solid #000000;
                        border-bottom: 1px solid #000000;
                        text-align: center;
                    }

                    table th {
                        border-left: 1px solid #000000;
                        border-top: 1px solid #000000;
                    }

                    table td {
                        border-left: 1px solid #000000;
                        border-top: 1px solid #000000;
                    }
                </style>
                '''

        table_text = f'''
                    <h2>你好 {row_list[1] } ,请查收工资条</h2>
                    </br>
                    <table border="1" align="left" width="1000" cellspacing="0">
                        <thead>
                            <tr>
                            <th>工号</th>
                            <th>姓名</th>
                            <th>部门</th>
                            <th>基本工资</th>
                            <th>提成</th>
                            <th>加班工资</th>
                            <th>社保扣除</th>
                            <th>考勤扣除</th>
                            <th>应发工资</th>
                            <th>邮箱</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                            <td>{ row_list[0] }</td>
                            <td>{ row_list[1] }</td>
                            <td>{ row_list[2] }</td>
                            <td>{ row_list[3] }</td>
                            <td>{ row_list[4] }</td>
                            <td>{ row_list[5] }</td>
                            <td>{ row_list[6] }</td>
                            <td>{ row_list[7] }</td>
                            <td>{ row_list[8] }</td>
                            <td>{ row_list[9] }</td>
                            </tr>
                        </tbody>
                    </table>
                    '''
        # print(table_text)

        # 编辑内容
        mail_text = table_header + table_text
        msg_body = MIMEText(mail_text, 'html', 'utf-8')

        # 设置从哪发送的
        msg_body['From'] = formataddr((str(Header('人事部', 'utf-8')), '360xxxxxx@qq.com'))
        msg_body['Subject'] = Header('某某公司2024年5月工资条','utf-8')

        # 发邮件
        smtp_obj.sendmail('360xxxxxx@qq.com', [row_list[9]], msg_body.as_string())

if __name__ == '__main__':
    send_email()