Eメールを送る

Although Python makes sending e-mails relatively easy via the smtplib library, Scrapy provides its own facility for sending e-mails which is very easy to use and it’s implemented using Twisted non-blocking IO, to avoid interfering with the non-blocking IO of the crawler. It also provides a simple API for sending attachments and it’s very easy to configure, with a few settings.

簡単な例

MailSenderをインスタンス化するには2つの方法があります. 標準コンストラクタを使用してインスタンス化できます:

from scrapy.mail import MailSender
mailer = MailSender()

または, settings を尊重する, Scrapy設定オブジェクトを渡してインスタンス化することもできます:

mailer = MailSender.from_settings(settings)

そして、MailSenderを使って電子メールを送信する方法(添付ファイルなし):

mailer.send(to=["someone@example.com"], subject="Some subject", body="Some body", cc=["another@example.com"])

MailSender クラスリファレンス

MailSenderは, フレームワークの他の部分と同様に, Twisted non-blocking IO, を使用するため, Scrapyから電子メールを送信するために使用するのに望ましいクラスです.

class scrapy.mail.MailSender(smtphost=None, mailfrom=None, smtpuser=None, smtppass=None, smtpport=None)
パラメータ:
  • smtphost (str) – the SMTP host to use for sending the emails. If omitted, the MAIL_HOST setting will be used.
  • mailfrom (str) – the address used to send emails (in the From: header). If omitted, the MAIL_FROM setting will be used.
  • smtpuser – the SMTP user. If omitted, the MAIL_USER setting will be used. If not given, no SMTP authentication will be performed.
  • smtppass (str) – the SMTP pass for authentication.
  • smtpport (int) – the SMTP port to connect to
  • smtptls (boolean) – enforce using SMTP STARTTLS
  • smtpssl (boolean) – enforce using a secure SSL connection
classmethod from_settings(settings)

Instantiate using a Scrapy settings object, which will respect these Scrapy settings.

パラメータ:settings (scrapy.settings.Settings object) – the e-mail recipients
send(to, subject, body, cc=None, attachs=(), mimetype='text/plain', charset=None)

Send email to the given recipients.

パラメータ:
  • to (str or list of str) – the e-mail recipients
  • subject (str) – the subject of the e-mail
  • cc (str or list of str) – the e-mails to CC
  • body (str) – the e-mail body
  • attachs (iterable) – an iterable of tuples (attach_name, mimetype, file_object) where attach_name is a string with the name that will appear on the e-mail’s attachment, mimetype is the mimetype of the attachment and file_object is a readable file object with the contents of the attachment
  • mimetype (str) – the MIME type of the e-mail
  • charset (str) – the character encoding to use for the e-mail contents

メール設定

これらの設定は, MailSender クラスのデフォルトのコンストラクタ値を定義し, コードを記述することなくプロジェクト内の電子メール通知を構成するために使用できます(これらの拡張子と MailSender を使用するコード用).

MAIL_FROM

初期値: 'scrapy@localhost'

Eメールの送信に使用する送信者Eメール (From: ヘッダー).

MAIL_HOST

初期値: 'localhost'

Eメールの送信に使用するSMTPホスト.

MAIL_PORT

初期値: 25

Eメールの送信に使用するSMTPポート.

MAIL_USER

初期値: None

SMTP認証に使用するユーザー. 無効にすると、SMTP認証は実行されません.

MAIL_PASS

初期値: None

MAIL_USER とともにSMTP認証に使用するパスワード.

MAIL_TLS

初期値: False

STARTTLSを使用して強制する. STARTTLSは、既存の安全でない接続を取得し, SSL / TLSを使用して安全な接続にアップグレードする方法です.

MAIL_SSL

初期値: False

SSL暗号化接続を使用して接続を強制する.