[Fixed]-How to fix django send mail?

1👍

Here is an implementation for gmail using standard email and smtplib packages (note different port and host in settings):

//settings.py
EMAIL_HOST = 'smtp.googlemail.com' #XXX
EMAIL_PORT = 465                   #XXX
EMAIL_HOST_PASSWORD = '**********' 
EMAIL_HOST_USER = '***@gmail.com'

Code:

import smtplib

from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate

from django.conf import settings

#send_to is a list of recipient emails
def send_mail(send_to, subject, body):

    msg = MIMEMultipart()
    msg['From'] = settings.EMAIL_HOST_USER
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime = True)
    msg['Subject'] = subject

    msg.attach(MIMEText(body))

    server_ssl = smtplib.SMTP_SSL(settings.EMAIL_HOST, settings.EMAIL_PORT)
    server_ssl.ehlo()
    server_ssl.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
    server_ssl.sendmail(settings.EMAIL_HOST_USER, send_to, msg.as_string())
    server_ssl.close()
👤serg

Leave a comment