Python, Django Tips: Sending e-mail


Django documentaion: Sending e-mailにあるように

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', 'from@example.com',
['to@example.com'], fail_silently=False)

という感じでgmailからメールを送ろうと四苦八苦したが、↓のエラーでちっとも送れない。

SMTPSenderRefused (530, '5.5.1 Authentication Required. Learn more at\n5.5.1 http://mail.google.com/support/bin/answer.py?answer=14257


settings.pyも色々と確認したが、問題はここじゃないみたい。ちなみにEmail関連の設定は↓な感じ。

# Email Settings
EAIL_HOST = 'smtp.gmail.com'
MAIL_PORT = 587
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_HOST_PASSWARD = 'password'
EMAIL_USE_TLS =True
DEFAULT_FROM_EMAIL = "test@gmail.com"
SERVER_EMAIL = "test@gmail.com"


詳細な原因は良く分からずだが、どうも悪いのはgmailとの相性のようで、velocity reviewsのforum: smtplib "authentication required" errorによると、なんだかgmailから送るときは↓のようにしないと送れないっぽい。

s = smtplib.SMTP('smtp.gmail.com')
s.set_debuglevel(1)
s.ehlo()
s.starttls()
s.ehlo()
s.login('test@gmail.com', 'password')
s.sendmail('test@gmail.com', 'totest@gmail.com', 'メッセージ')
s.close()

GmailではなくプロバイダのSMTPを使ったら問題なく送れた。Gmailからもsend_mailで送れるとけっこう幸せだけど、とりあえずはしょーがないからこれで我慢しよう。