[Answer]-Send email django from any host

0👍

This is pure python code for sending emails using SMTP Lib

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()

1👍

You can try yagmail, it should make it a lot easier:

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

It has a lot more features, for example how to make it easier to send attachments etc.

yagmail can be found at github.

You will probably have to install it first using pip:

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

Leave a comment