由官网提供的方法能够发现有两种形式能够发送邮件,html
其一:配置settings,使用python
[python] view plain copy 服务器
mailer = MailSender.from_settings(settings) dom
的方法读取settings的配置,不过本人这样写好之后一直出现这种错误scrapy
[python] view plain copy ide
TypeError: 'module' object has no attribute '__getitem__' url
没有找到解决方法,因此尝试了另外一种spa
其二:官网连接.net
直接在MailSender中配置须要的字段。code
本人使用的是163邮箱发送邮件,请注意:受权码的获取以及邮箱的相应配置以下:
受权码获取成功之后必定要妥善保存,缘由你懂得!!
下面代码为爬虫关闭的时候,执行发送邮件的功能!(代码亲测已过)
[python] view plain copy
# -*- coding: utf-8 -*-
import scrapy
import logging
class MaiziSpiderSpider(scrapy.Spider):
name = "maizi_spider"
allowed_domains = ["maiziedu.com"]
start_urls = (
'http://www.maiziedu.com/wiki/crawler/logging/',
)
logger = logging.getLogger(__name__)
def parse(self, response):
self.logger.info('Parse function called on %s', response.url)
print response.url
def closed(self, reason):# 爬取结束的时候发送邮件
from scrapy.mail import MailSender
# mailer = MailSender.from_settings(settings)# 出错了,没找到缘由
mailer = MailSender(
smtphost = "smtp.163.com", # 发送邮件的服务器
mailfrom = "***********@163.com", # 邮件发送者
smtpuser = "***********@163.com", # 用户名
smtppass = "***********", # 发送邮箱的密码不是你注册时的密码,而是受权码!!!切记!
smtpport = 25 # 端口号
)
body = u"""
发送的邮件内容
"""
subject = u'发送的邮件标题'
# 若是说发送的内容太过简单的话,极可能会被当作垃圾邮件给禁止发送。
mailer.send(to=["****@qq.com", "****@qq.com"], subject = subject.encode("utf-8"), body = body.encode("utf-8"))
发送的过程当中可能会被识别为垃圾邮件,为了防止这种状况的发生,修改发送邮件的主题和发送邮件的内容便可。