send email w/ python
2012-03-05 00:00
274 查看
simple email w/o attachement:
email w/ html file:
相关link: http://docs.python.org/library/email-examples.html
# Import smtplib for the actual sending function import smtplib # Import the email modules we'll need from email.mime.text import MIMEText # Open a plain text file for reading. For this example, assume that # the text file contains only ASCII characters. textfile = "d:\\result.html" fp = open(textfile, 'rb') # Create a text/plain message msg = MIMEText(fp.read()) fp.close() # me == the sender's email address # you == the recipient's email address msg['Subject'] = 'The contents of %s' % textfile me = "rebecca.hu@macrotarget.com" you = "rebecca.hu@qq.com" msg['From'] = me msg['To'] = you # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib.SMTP('smtp.live.com','587') s.starttls() #用于加密 s.login("rebecca.hu@macrotarget.com", "mima001") s.sendmail(me, you, msg.as_string()) s.quit()
email w/ html file:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # me == my email address # you == recipient's email address me = "rebecca.hu@mail.com" you = "you@mail.com" # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') #实例一个附件 msg['Subject'] = "Link" msg['From'] = me msg['To'] = you # 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 = """\ #<html> # <head></head> # <body> # <p>Hi!<br> # How are you?<br> # Here is the <a href="http://www.python.org">link</a> you wanted. # </p> # </body> # #""" # Record the MIME types of both parts - text/plain and text/html. #part1 = MIMEText(text, 'plain') textfile = "d:\\result.html" fp = open(textfile, 'rb') part2 = MIMEText(fp.read(), 'plain') fp.close() part2.replace_header("Content-type", "Application/octet-stream;name='result.html'") #重写"Content-type" part2.add_header("Content-Disposition","attachment;filename='result.html'")#加入"Content-Disposition" 为附件"attachment" # 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) # Send the message via local SMTP server. s = smtplib.SMTP('smtp.live.com','587') s.starttls() s.login("rebecca.hu@mail.com", "12345") # sendmail function takes 3 arguments: sender's address, recipient's address # and message to send - here it is sent as one string. s.sendmail(me, you, msg.as_string()) s.quit()
相关link: http://docs.python.org/library/email-examples.html
相关文章推荐
- Python+sendEmail发邮件
- python实例26[sendemail]
- python email ==> send 发送邮件 :) [smtplib, email 模块]
- imap send email by python
- python auto send email
- python xml sendEmail
- python实例26[sendemail]
- Python生成器generator之next和send运行流程
- python3.5 email实现发送邮件功能
- Fedora git send-email 使用 msmtp 发送 Gmail 邮件
- zabbix使用sendEmail实现邮件报警
- zabbix使用sendEmail实现邮件报警
- python模块之email: 电子邮件编码解码 (二、编码邮件)
- How can I send an email by Java application?
- python模块之email: 电子邮件编码解码 (一、解码邮件)
- a simple email test in python
- Python之email
- python开发_email_读取邮件头信息
- Spring v3.0.2 Learning Note 14 - Send Email with Spring
- Python:generator的send()方法流程分析