共计 1285 个字符,预计需要花费 4 分钟才能阅读完成。
直接上代码了,很简单的代码看看就好
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr, formataddr
from bs4 import BeautifulSoup
import requests
neihan_url ='http://neihanshequ.com'
user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
headers = { 'User-Agent' : user_agent }
neihan_page=requests.get(neihan_url,headers=headers)
bs_neihan=BeautifulSoup(neihan_page.text,'lxml')
h1_content=bs_neihan.find_all('h1')
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
mail_host="smtp.126.com"
mail_user="xxxxx@126.com"
mail_pass="xxxxx"
sender = 'boostcj@126.com'
receivers =['xxx@126.com','xxxx@126.com']
message = MIMEText(u'大家快点点饭啊!\n'+u'-----------华丽的分割线---------\n'+h1_content[0].next_sibling.string, 'plain', 'utf-8')
message['From'] = _format_addr('老司机 <%s>' % mail_user)
#message['To'] = _format_addr('小伙伴<%s>' % receivers)
message['To']=','.join([ _format_addr('小伙伴 <%s>' % x) for x in receivers ])
subject = '点饭专用'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
#smtpObj.set_debuglevel(1)
smtpObj.connect(mail_host, 25)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
print "success"
except smtplib.SMTPException:
print "Error: can not send mail"
正文完
请博主喝杯咖啡吧!