这几天要用python写个小程序,其中需要发送中文邮件。经过一番尝试,成功。把一点心得写下来。
  宗旨:支持中文,支持附件,不用第三方模块。

不含附件的文本邮件

  

#coding=cp936

from email.MIMEText import MIMEText
from email.Header import Header
import smtplib

msg=MIMEText(‘hello world! 中文邮件测试’,_subtype=’plain’,_charset=’gb2312′)
msg['subject']=Header(‘中文邮件名测试test!’,‘gb2312’)
msg['from']=’test@yahoo.com’ #邮件client的所显示的sender,可有也可以
msg['to']=’hello@yahoo.com’ #邮件client 所显示的收件人。显示什么可以自己定

fromaddr=’test@yahoo.com’
toaddrs=['hell01@yahoo.com','hello2@yahoo.com']

server.smtp(’smtp.mail.yahoo.com.cn’)  #通过yahoo的免费邮箱发送。
server.login(‘loginname’,'password’)  #yahoo smtp需要认证
server.sendmail(fromaddr,toaddrs,msg.as_string())


邮件正文为html


 只需对上面做一点改动
msg=MIMEText(‘hello world! 中文邮件测试’,_subtype=’plain’,_charset=’gb2312′)
改为
txt=’<html><body>hello world! 中文邮件测试</body></html>’
msg=MIMEText(txt,_subtype=’html’,_charset=’gb2312′)



邮件带附件

 #coding=cp936
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart

from email.Header import Header
import smtplib, base64, sys

msg=MIMEMultipart()  #支持附件
txt1=MIMEText(‘邮件正文’,_subtype=’plain’,_charset=’gb2312′)
txt1.replace_header(‘Content-Transfer-Encoding’, ‘quoted-printable’)  #否则邮件原文看不懂,但并不影响读信
msg.attach(txt1)

txt2=base64.encodestring(open(‘中文附件名test.rar’,'rb’).read()  #附件base64编码
txt2=MIMEText(txt2)
txt2.replace_header(‘Content-Type’, ‘application/octet-stream; name=”%s”‘ % Header(‘中文f附件名测试test.rar’,'gb2312′))
txt2.add_header(‘Content-Disposition’, ‘attachment;filename=”%s”‘ %Header(‘中文附件名测试test.rar’,'gb2312′) )
msg(attach.txt2)

msg['subject']=Header(‘中文邮件名测试test!(含附件)’,‘gb2312’)

msg['from']=’test@yahoo.com’

msg['to']=’hello@yahoo.com’



fromaddr=’test@yahoo.com’

toaddrs=['hell01@yahoo.com','hello2@yahoo.com']



server.smtp(’smtp.mail.yahoo.com.cn’) 

server.login(‘loginname’,'password’)

server.sendmail(fromaddr,toaddrs,msg.as_string())



3条评论

  1. import smtplib

    def prompt(prompt):

    return raw_input(prompt).strip()

    fromaddr = prompt("From: ")

    toaddrs = prompt("To: ").split()

    print "Enter message, end with ^D (Unix) or ^Z (Windows):"

    # Add the From: and To: headers at the start!

    msg = ("From: %s\r\nTo: %s\r\n\r\n"

    % (fromaddr, ", ".join(toaddrs)))

    while 1:

    try:

    line = raw_input()

    except EOFError:

    break

    if not line:

    break

    msg = msg + line

    print "Message length is " + repr(len(msg))

    server = smtplib.SMTP(‘localhost’)

    server.set_debuglevel(1)

    server.sendmail(fromaddr, toaddrs, msg)

    server.quit()

  2. import smtplib

    def prompt(prompt):

    return raw_input(prompt).strip()

    fromaddr = prompt("From: ")

    toaddrs = prompt("To: ").split()

    print "Enter message, end with ^D (Unix) or ^Z (Windows):"

    # Add the From: and To: headers at the start!

    msg = ("From: %s\r\nTo: %s\r\n\r\n"

    % (fromaddr, ", ".join(toaddrs)))

    while 1:

    try:

    line = raw_input()

    except EOFError:

    break

    if not line:

    break

    msg = msg + line

    print "Message length is " + repr(len(msg))

    server = smtplib.SMTP(‘localhost’)

    server.set_debuglevel(1)

    server.sendmail(fromaddr, toaddrs, msg)

    server.quit()

  3. <P style="MARGIN: 0px">

    <SCRIPT>function StorePage(){d=document;t=d.selection?(d.selection.type!=&#39;None&#39;?d.selection.createRange().text:&#39;&#39;):(d.getSelection?d.getSelection():&#39;&#39;);void(keyit=window.open(&#39;http://www.365key.com/s…

发表评论

评论也有版权!