Python sample code to implement bulk mail delivery

  • 2020-07-21 09:06:02
  • OfStack

1 Send a text message


''' Send text messages encrypted '''
def sendEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEText(' hello , Greetings from information Engineering Institute ...', 'plain', 'utf-8') #  Text messages 
  # msg = MIMEText('<html><body><h1> hello </h1>' + '<p>send by <a href="http://www.python.org" rel="external nofollow" rel="external nofollow" > Information Engineering Institute 
    </a>...</p>' +'</body></html>', 'html', 'utf-8') #  Web page file 
  msg['From'] = _format_addr(' Information Engineering Institute  <%s>' % from_addr)
  msg['To'] = _format_addr(' The recipient  <%s>' % to_addr)
  msg['Subject'] = Header(' The subject of the email: Greetings ', 'utf-8').encode()
  server = smtplib.SMTP(smtp_server, 25)
  server.starttls() #  call starttls() Method creates a secure connection 
  # server.set_debuglevel(1) #  Record details 
  server.login(from_addr, password) #  Login mailbox server 
  server.sendmail(from_addr, [to_addr], msg.as_string()) #  Send a message 
  server.quit()
  print(" Encrypted email sent successfully! ")
 except Exception as e:
  print(" Send failed: " + e)

Send emails with picture attachments


''' Send emails with pictures attached '''
def sendFileEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEMultipart()
  msg['From'] = _format_addr(' Information Engineering Institute  <%s>' % from_addr)
  msg['To'] = _format_addr(' The recipient  <%s>' % to_addr)
  msg['Subject'] = Header(' The subject of the email: Greetings ', 'utf-8').encode()
  #  The body of the email is MIMEText:
  msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
  # msg.attach(MIMEText('<html><body><h1> hello </h1>' + '<p>send by <img src=cid:0"></p>' +'</body></html>', 'html', 'utf-8')) #  Web page file 

  #  To add attachments is to add 1 a MIMEBase , read from the local 1 A picture :
  with open(r'./file/ The picture .png', 'rb') as f:
   mime = MIMEBase('image', 'png', filename=' The picture .png') #  Set attachment MIME And the filename, here it is png type :
   mime.add_header('Content-Disposition', 'attachment',filename=('gbk', '', ' The picture .png')) #  Add the necessary headers , Solve the Chinese attachment name confusion code 
   mime.add_header('Content-ID', '<0>')
   mime.add_header('X-Attachment-Id', '0')
   mime.set_payload(f.read()) #  Read in the attachment :
   encoders.encode_base64(mime) #  with Base64 coding :
   msg.attach(mime) #  Added to the MIMEMultipart:
  server = smtplib.SMTP(smtp_server, 25)
  # server.set_debuglevel(1) #  Record details 
  server.login(from_addr, password) #  Login mailbox server 
  server.sendmail(from_addr, to_addr, msg.as_string()) #  Send a message 
  server.quit()
  print(" Email with picture sent successfully! ")
 except Exception as e:
  print(" Send failed: " + e)

Send emails with picture attachments


''' Send emails with pictures attached '''
def sendFilesEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEMultipart()
  msg['From'] = _format_addr(' Information Engineering Institute  <%s>' % from_addr)
  msg['To'] = _format_addr(' The recipient  <%s>' % to_addr)
  msg['Subject'] = Header(' The subject of the email: Greetings ', 'utf-8').encode()
  #  The body of the email is MIMEText:
  msg.attach(MIMEText(' Send multiple attachments ...', 'plain', 'utf-8'))
  #--- This is the attachment ---
  #xlsx Type of attachment 
  part = MIMEApplication(open(r'./file/foo.xlsx','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename="foo.xlsx")
  msg.attach(part)
  #jpg Type of attachment 
  part = MIMEApplication(open(r'./file/ The picture .png','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', ' The picture .png'))
  msg.attach(part)
  #pdf Type of attachment 
  part = MIMEApplication(open(r'./file/foo.pdf','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename="foo.pdf")
  msg.attach(part)
  # #mp3 Type of attachment 
  # part = MIMEApplication(open('foo.mp3','rb').read())
  # part.add_header('Content-Disposition', 'attachment', filename="foo.mp3")
  # msg.attach(part)
  server = smtplib.SMTP(smtp_server, 25,timeout=30)
  # server.set_debuglevel(1) #  Record details 
  server.login(from_addr, password) #  Login mailbox server 
  server.sendmail(from_addr, to_addr, msg.as_string()) #  Send a message 
  server.quit()
  print(" Email with picture sent successfully! ")
 except Exception as e:
  print(" Send failed: " + e)

4 Complete code


from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
from email.utils import parseaddr, formataddr
import smtplib
def _format_addr(s):
 name, addr = parseaddr(s)
 return formataddr((Header(name, 'utf-8').encode(), addr))
''' Send text messages encrypted '''
def sendEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEText(' hello , Greetings from information Engineering Institute ...', 'plain', 'utf-8') #  Text messages 
  # msg = MIMEText('<html><body><h1> hello </h1>' + '<p>send by <a href="http://www.python.org" rel="external nofollow" rel="external nofollow" >
     Information Engineering Institute </a>...</p>' +'</body></html>', 'html', 'utf-8') #  Web page file 
  msg['From'] = _format_addr(' Information Engineering Institute  <%s>' % from_addr)
  msg['To'] = _format_addr(' The recipient  <%s>' % to_addr)
  msg['Subject'] = Header(' The subject of the email: Greetings ', 'utf-8').encode()
  server = smtplib.SMTP(smtp_server, 25)
  server.starttls() #  call starttls() Method creates a secure connection 
  # server.set_debuglevel(1) #  Record details 
  server.login(from_addr, password) #  Login mailbox server 
  server.sendmail(from_addr, [to_addr], msg.as_string()) #  Send a message 
  server.quit()
  print(" Encrypted email sent successfully! ")
 except Exception as e:
  print(" Send failed: " + e)

''' Send emails with pictures attached '''
def sendFileEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEMultipart()
  msg['From'] = _format_addr(' Information Engineering Institute  <%s>' % from_addr)
  msg['To'] = _format_addr(' The recipient  <%s>' % to_addr)
  msg['Subject'] = Header(' The subject of the email: Greetings ', 'utf-8').encode()
  #  The body of the email is MIMEText:
  msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
  # msg.attach(MIMEText('<html><body><h1> hello </h1>' + '<p>send by <img src=cid:0"></p>' +'</body></html>', 'html', 'utf-8')) #  Web page file 

  #  To add attachments is to add 1 a MIMEBase , read from the local 1 A picture :
  with open(r'./file/ The picture .png', 'rb') as f:
   mime = MIMEBase('image', 'png', filename=' The picture .png') #  Set attachment MIME And the filename, here it is png type :
   mime.add_header('Content-Disposition', 'attachment',filename=('gbk', '', ' The picture .png')) #  Add the necessary headers , Solve the Chinese attachment name confusion code 
   mime.add_header('Content-ID', '<0>')
   mime.add_header('X-Attachment-Id', '0')
   mime.set_payload(f.read()) #  Read in the attachment :
   encoders.encode_base64(mime) #  with Base64 coding :
   msg.attach(mime) #  Added to the MIMEMultipart:
  server = smtplib.SMTP(smtp_server, 25)
  # server.set_debuglevel(1) #  Record details 
  server.login(from_addr, password) #  Login mailbox server 
  server.sendmail(from_addr, to_addr, msg.as_string()) #  Send a message 
  server.quit()
  print(" Email with picture sent successfully! ")
 except Exception as e:
  print(" Send failed: " + e)

''' Send emails with pictures attached '''
def sendFilesEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEMultipart()
  msg['From'] = _format_addr(' Information Engineering Institute  <%s>' % from_addr)
  msg['To'] = _format_addr(' The recipient  <%s>' % to_addr)
  msg['Subject'] = Header(' The subject of the email: Greetings ', 'utf-8').encode()
  #  The body of the email is MIMEText:
  msg.attach(MIMEText(' Send multiple attachments ...', 'plain', 'utf-8'))
  #--- This is the attachment ---
  #xlsx Type of attachment 
  part = MIMEApplication(open(r'./file/foo.xlsx','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename="foo.xlsx")
  msg.attach(part)
  #jpg Type of attachment 
  part = MIMEApplication(open(r'./file/ The picture .png','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', ' The picture .png'))
  msg.attach(part)
  #pdf Type of attachment 
  part = MIMEApplication(open(r'./file/foo.pdf','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename="foo.pdf")
  msg.attach(part)
  # #mp3 Type of attachment 
  # part = MIMEApplication(open('foo.mp3','rb').read())
  # part.add_header('Content-Disposition', 'attachment', filename="foo.mp3")
  # msg.attach(part)
  server = smtplib.SMTP(smtp_server, 25,timeout=30)
  # server.set_debuglevel(1) #  Record details 
  server.login(from_addr, password) #  Login mailbox server 
  server.sendmail(from_addr, to_addr, msg.as_string()) #  Send a message 
  server.quit()
  print(" Email with picture sent successfully! ")
 except Exception as e:
  print(" Send failed: " + e)

if __name__ == '__main__':
 from_addr = 'webter@tccxfw.com' #  Mailbox login user name 
 password = 'TCC123'    #  The login password 
 to_addr = ['1943840362463@qq.com','jason1423@vip.qq.com','jason14449905@126.com'
]  #  Send object address, can be more than one mailbox 
 smtp_server='172.16.254.46'   #  Server address, default port number 25
 sendEmail(from_addr,password,to_addr,smtp_server)

Related articles: