Created
October 30, 2019 11:11
-
-
Save giasuddin90/1665641fa521fb31c0cbc529eabd99ed to your computer and use it in GitHub Desktop.
Python html script mail by using third party mail server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'giasuddin' | |
test_html=""" <!DOCTYPE html> | |
<html> | |
<head> | |
<title>Page Title</title> | |
</head> | |
<body> | |
<h1>Python test mail</h1> | |
<p>my message. {}</p> | |
</body> | |
</html> | |
""" | |
def verifying_mail(to, subject, text): | |
SMTPserver = 'mail.test.org.bd' | |
sender = '[email protected]' | |
destination = [to] | |
USERNAME = "[email protected]" | |
PASSWORD = "abc123" | |
# typical values for text_subtype are plain, html, xml | |
text_subtype = 'html' | |
content = text | |
mail_subject = subject | |
import sys | |
# from smtplib import SMTP_SSL as SMTP | |
# this invokes the secure SMTP protocol (port 465, uses SSL) | |
from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption) | |
from email.mime.text import MIMEText | |
import email | |
import time | |
try: | |
msg = MIMEText(content, text_subtype) | |
msg['Subject'] = mail_subject | |
msg['From'] = sender # some SMTP servers will do this automatically, not all | |
msg['Date'] = email.utils.formatdate(localtime=True) | |
# utc_from_epoch = time.time() | |
# msg['Date'] = email.utils.formatdate(utc_from_epoch, localtime=True) | |
conn = SMTP(SMTPserver) | |
# conn.set_debuglevel(True) | |
conn.login(USERNAME, PASSWORD) | |
try: | |
conn.sendmail(sender, destination, msg.as_string()) | |
finally: | |
conn.close() | |
except Exception as exc: | |
sys.exit("mail failed; %s" % str(exc)) # give a error mess | |
if __name__ == '__main__': | |
my_message = test_html.format("my message") | |
verifying_mail("[email protected]","test mail sfjsd ",my_message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment