Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import smtplib
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- from email.mime.application import MIMEApplication
- def sendmail(smtp_server, port, username, password, from_addr, to_addr, subject, content, content_type='plain', cc_addr=None, bcc_addr=None, attachment=None):
- """
- smtp_sever: 填SMTP服务器地址,string
- port: 填服务器端口,int
- username: 邮箱登录名,string
- password: 邮箱登录密码,string
- from_addr:发件人地址,只能填一个地址,string
- to_addr: 收件人地址,可多个,string、list、tuple均可
- subject: 邮件主题,string
- content: 邮件正文,string
- content_type: 邮件正文类型,只有两个选择:plain(默认)或html。string
- cc_addr: 抄送人地址,可多个,string、list、tuple均可,默认为None
- bcc_addr: 密送人地址,可多个,string、list、tuple均可,默认为None
- attachment: 邮件附件路径,可多个,string、list、tuple均可,默认为None
- """
- msg = MIMEMultipart()
- # 处理发件人
- from_show_addr = from_addr
- from_addr = str(from_addr)
- msg['From'] = from_show_addr
- # 处理收件人
- if isinstance(to_addr, list):
- to_show_addr = ','.join(to_addr)
- to_addr = to_addr
- elif isinstance(to_addr, tuple):
- to_show_addr = ','.join(to_addr)
- to_addr = list(to_addr)
- elif isinstance(to_addr, str):
- if ',' in to_addr:
- to_addr.replace(' ', '')
- to_show_addr = to_addr
- to_addr = to_addr.split(',')
- else:
- to_show_addr = to_addr
- to_addr = [to_addr]
- msg['To'] = to_show_addr
- # 处理标题
- subject = str(subject)
- msg['Subject'] = subject
- # 处理抄送人
- if cc_addr is not None:
- if isinstance(cc_addr, list):
- cc_show_addr = ','.join(cc_addr)
- to_addr = to_addr + cc_addr
- elif isinstance(cc_addr, tuple):
- cc_show_addr = ','.join(cc_addr)
- to_addr = to_addr + list(cc_addr)
- elif isinstance(cc_addr, str):
- if ',' in cc_addr:
- cc_addr.replace(' ', '')
- cc_show_addr = cc_addr
- cc_addr = cc_addr.split(',')
- to_addr = to_addr + cc_addr
- else:
- cc_show_addr = cc_addr
- to_addr.append(cc_addr)
- msg['Cc'] = cc_show_addr
- # 处理密送人
- if bcc_addr is not None:
- if isinstance(bcc_addr, list):
- to_addr = to_addr + bcc_addr
- elif isinstance(bcc_addr, tuple):
- to_addr = to_addr + list(bcc_addr)
- elif isinstance(bcc_addr, str):
- if ',' in bcc_addr:
- bcc_addr.replace(' ', '')
- bcc_addr = bcc_addr.split(',')
- to_addr = to_addr + bcc_addr
- else:
- to_addr.append(bcc_addr)
- # 处理正文
- if content_type.lower() == 'plain':
- msg.attach(MIMEText(content, 'plain', 'UTF-8'))
- elif content_type.lower() == 'html':
- msg.attach(MIMEText(content, 'html', 'UTF-8'))
- # 处理附件
- if attachment is not None:
- if isinstance(attachment, list) or isinstance(attachment, tuple):
- for each_file in attachment:
- if os.path.isfile(each_file):
- with open(each_file, 'rb') as f:
- part = MIMEApplication(f.read(), Name=os.path.basename(each_file))
- part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(each_file)
- msg.attach(part)
- elif isinstance(attachment, str):
- if os.path.isfile(attachment):
- with open(attachment, 'rb') as f:
- part = MIMEApplication(f.read(), Name=os.path.basename(attachment))
- part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(attachment)
- msg.attach(part)
- server = smtplib.SMTP(smtp_server, int(port))
- server.starttls()
- server.login(username, password)
- server.sendmail(from_addr, to_addr, msg.as_string())
- print('Sent E-mail successfully!')
- server.quit()
Add Comment
Please, Sign In to add comment