欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 七种邮件内容发送方法实例

程序员文章站 2023-11-11 20:14:22
一、文件形式的邮件复制代码 代码如下:#!/usr/bin/env python3#coding: utf-8import smtplibfrom email.mime.t...

一、文件形式的邮件

复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import mimetext
from email.header import header

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = mimetext('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['subject'] = header(subject, 'utf-8')

smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

二、html形式的邮件

复制代码 代码如下:

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import mimetext

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = mimetext('</pre>
<h1>你好</h1>
<pre>','html','utf-8')

msg['subject'] = subject

smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

三、带图片的html邮件

复制代码 代码如下:

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext
from email.mime.image import mimeimage

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msgroot = mimemultipart('related')
msgroot['subject'] = 'test message'

msgtext = mimetext('<b>some <i>html</i> text</b> and an image.
<img alt="" src="cid:image1" />
good!','html','utf-8')
msgroot.attach(msgtext)

fp = open('h:\\python\\1.jpg', 'rb')
msgimage = mimeimage(fp.read())
fp.close()

msgimage.add_header('content-id', '')
msgroot.attach(msgimage)

smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgroot.as_string())
smtp.quit()

四、带附件的邮件

复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext
from email.mime.image import mimeimage

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msgroot = mimemultipart('related')
msgroot['subject'] = 'test message'

#构造附件
att = mimetext(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["content-type"] = 'application/octet-stream'
att["content-disposition"] = 'attachment; filename="1.jpg"'
msgroot.attach(att)

smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgroot.as_string())
smtp.quit()

五、群邮件

复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import mimetext

sender = '***'
receiver = ['***','****',……]
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = mimetext('你好','text','utf-8')

msg['subject'] = subject

smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

六、各种元素都包含的邮件
复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext
from email.mime.image import mimeimage

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

# create message container - the correct mime type is multipart/alternative.
msg = mimemultipart('alternative')
msg['subject'] = "link"

# create the body of the message (a plain-text and an html version).
text = "hi!\nhow are you?\nhere is the link you wanted:\nhttp://www.python.org"
html = """\

 
hi!

       how are you?

       here is the <a href="http://www.python.org">link</a> you wanted.

 

"""

# record the mime types of both parts - text/plain and text/html.
part1 = mimetext(text, 'plain')
part2 = mimetext(html, 'html')

# attach parts into message container.
# according to rfc 2046, the last part of a multipart message, in this case
# the html message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
att = mimetext(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["content-type"] = 'application/octet-stream'
att["content-disposition"] = 'attachment; filename="1.jpg"'
msg.attach(att)

smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

七、基于ssl的邮件

复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import mimetext
from email.header import header
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = mimetext('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['subject'] = header(subject, 'utf-8')

smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()