从任何主机发送电子邮件 django

Send email django from any host

我已经阅读了很多通过 Django 发送电子邮件的链接。我已经尝试了所有这些,但它们不起作用。我尝试通过 python shell 发送电子邮件,但收到“1”。 - 那么我应该使用什么设置才能使电子邮件正常工作,我愿意使用任何邮件服务器? - 我正在使用 gmail,但我读到它会导致问题,例如,如果我要使用 hotmail,我是否需要在 settings.xml 中指定电子邮件密码? - 如何调试这个问题?

这是使用 SMTP Lib

发送电子邮件的纯 python 代码
from threading import Thread
import requests
import time
import smtplib
def email_sender(input_message, email_to, client):
    ''' function to send email '''
    to = email_to
    gmail_user = 'email of sender account'
    gmail_pwd = 'password of sender account'
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(gmail_user, gmail_pwd)
    header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:site down! \n'
    input_message = input_message + client
    msg = header + input_message
    smtpserver.sendmail(gmail_user, to, msg)
    smtpserver.close()

你可以试试 yagmail,它应该会更容易:

import yagmail
yag = yagmail.Connect('user@gmail.com', 'password')
yagmail.send(email_to, subject = 'site down!', contents = 'with some error')

它有更多的功能,例如如何更方便地发送附件等。

yagmail 可以在 github 找到。

您可能必须先使用 pip 安装它:

pip install yagmail  # python 2
pip3 install yagmail # python 3