# encoding=utf-8
import smtplib
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
pwd = "Axx2345"
sender = "test@xxx.com"
server = 'smtp.xxx.com'
reveivers = ['test01@xxx.com', 'test02@xxx.com']
subject = 'python 邮件发送'
def send_text_email():
"""发送一个普通的文本邮件"""
text_content = """
<h2>title</h2>
<p>eamil content</p>
"""
msg = MIMEText(text_content, 'plain', 'utf-8')
msg['From'] = sender
msg['To'] = ';'.join(reveivers)
msg['Subject'] = subject # 邮件标题
smtp = smtplib.SMTP(server)
smtp.login(sender, pwd)
smtp.sendmail(sender, reveivers, msg.as_string())
def send_html_email():
"""发送一个带html格式的邮件"""
html_content = """
<h2>title</h2>
<p>eamil content</p>
"""
msg = MIMEText(html_content, 'html', 'utf-8')
msg['From'] = sender
msg['To'] = ';'.join(reveivers)
msg['Subject'] = subject # 邮件标题
smtp = smtplib.SMTP(server)
smtp.login(sender, pwd)
smtp.sendmail(sender, reveivers, msg.as_string())
def send_attach_email():
"""发送一个带附件的邮件"""
msg = MIMEText('带附件的邮件', 'plain', 'utf-8')
image_attach = MIMEImage(open('test.JPG', 'rb').read())
image_attach.add_header('Content-Disposition', 'attachment', filename='test_image.jpg')
image_attach2 = MIMEApplication(open('test.JPG', 'rb').read())
image_attach2.add_header('Content-Disposition', 'attachment', filename='test_image2.jpg')
pdf_attach = MIMEApplication(open('test_pdf.pdf', 'rb').read())
pdf_attach.add_header('Content-Disposition', 'attachment', filename='test_pdf.pdf')
multipart = MIMEMultipart()
multipart['From'] = sender
multipart['To'] = ';'.join(reveivers)
multipart['Subject'] = subject
multipart.attach(msg)
multipart.attach(image_attach)
multipart.attach(image_attach2)
multipart.attach(pdf_attach)
smtp = smtplib.SMTP(server)
smtp.login(sender, pwd)
smtp.sendmail(sender, reveivers, multipart.as_string())
def send_html_contain_image_email():
"""发送一个包含图片的html邮件"""
html_content = """
<h2>title</h2>
<p>eamil content</p>
<br/>
<img src="cid:image01">
"""
msg = MIMEText(html_content, 'html', 'utf-8')
image_attach = MIMEImage(open('test.JPG', 'rb').read())
image_attach.add_header('Content-ID', 'image01')
multipart = MIMEMultipart()
multipart['From'] = sender
multipart['To'] = ';'.join(reveivers)
multipart['Subject'] = subject # 邮件标题
multipart.attach(msg)
multipart.attach(image_attach)
smtp = smtplib.SMTP(server)
smtp.login(sender, pwd)
smtp.sendmail(sender, reveivers, multipart.as_string())
if __name__ == '__main__':
send_text_email()
send_html_email()
send_attach_email()
send_html_contain_image_email()
发送一个普通的文本邮件
发送一个带html格式的邮件
发送一个带附件的邮件
发送一个包含图片的html邮件
调试发送邮件问题
- 点击 -> 更多操作 -> 查看邮件源码
添加抄送人员
def send_email(subject, text_content, toUser, CcUser=None):
pwd = xxx
sender = xxx
reveivers = toUser
server = xxxx
msg = MIMEText(text_content, 'html', 'utf-8')
msg['From'] = sender
msg['To'] = ';'.join(reveivers)
if CcUser is not None:
msg['Cc'] = ';'.join(CcUser)
msg['Subject'] = subject
smtp = smtplib.SMTP(server)
smtp.login(sender, pwd)
smtp.sendmail(sender, reveivers+CcUser, msg.as_string())
send_email("test subject", "test content", ["xx@xxx.com"], ["aa@xxx.com"])
msg['Cc'] = ';'.join(CcUser)
:设置邮件抄送部分的人员名单信息,用于显示在抄送栏位
smtp.sendmail(sender, reveivers+CcUser, msg.as_string())
:这里必须把抄送人员的邮件也加入进来,否则抄送人员只会在抄送栏位显示,但是接收不到邮件信息