python notes: bulk send the specified attachments to the specified email address

Keywords: Programming Python

Objectives:

Send the supplier extension list separated from the last note to the email address of different suppliers in the form of attachment. The code test sends an email every second

 

The code is as follows

 

 

#!/usr/bin/env python
# coding: utf-8

# In[49]:


import smtplib
from email import encoders
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import parseaddr,formataddr
from email.mime.application import MIMEApplication


# In[50]:


import pandas as pd
import numpy as np


# In[51]:


df=pd.read_excel(r"D:\Arrival condition\Supplier mailing list.xlsx",sheet_name=0,header=0)
df.head(2)


# In[52]:


wenjianname=df["file name"]
youjianadd=df["mailing address"]


# In[53]:


n=-1


# In[54]:


for i in wenjianname:
    asender="186@163.com"#sender address 
    n=n+1
    areceiver=df["mailing address"][n]#Address of addressee
    acc="376@qq.com"#CC address
    asubject="Here is your company's extension list"#Mail theme
    from_addr="186@163.com"
    password="RP"#Email authorization code
    msg=MIMEMultipart()#Mail Settings
    msg["Subject"]=asubject
    msg["to"]=areceiver
    msg["cc"]=acc
    msg["from"]="Xiaoling"
    body="Hello, this is your delay list. Please arrange delivery as soon as possible."#Mail text
    msg.attach(MIMEText(body,"plain","utf-8"))#Add message body #Add attachments
    #Note that the file path here is a separator line
    xlsxpart=MIMEApplication(open('D:/Arrival condition/Generate file/'+i,'rb').read())
    xlsxpart.add_header("Content-Disposition","attachment",filename=i)
    msg.attach(xlsxpart)
    #Set mailbox server address and port
    smtp_server="smtp.163.com"
    server=smtplib.SMTP(smtp_server,25)
    server.set_debuglevel(1)
    #Login mailbox
    server.login(from_addr,password)
    #Send mail
    server.sendmail(from_addr,areceiver.split(",")+acc.split(","),msg.as_string())
    #Disconnect server
    server.quit()
 





In addition, if you want to know how to quickly get the list of file names to be attached, please wait a moment to see my next blog. There will be a little trick. You can easily get the list of file names to be attached. Just add the email address of these attachments to be sent later.

Posted by nmcglennon on Sat, 04 Apr 2020 06:12:51 -0700