Skip to content

Commit fd4e8ce

Browse files
authored
Create dynamicemailsender.py
1 parent 37f7e60 commit fd4e8ce

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

dynamicemailsender.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import smtplib # for sending mails via server
2+
from email.message import EmailMessage # email components
3+
from string import Template # to substitute
4+
from pathlib import Path # to read html files
5+
6+
7+
html = Template(Path('index.html').read_text())
8+
9+
email = EmailMessage() # email is an object of EmailMessage() class
10+
email['from'] = 'fromname'
11+
email['to'] = 'toemailid'
12+
email['subject'] = 'This is a test email using python'
13+
14+
email.set_content(html.substitute(name='Sam'), 'html') # 1st arg is substituting the name, 2nd arg is specifying its an html file
15+
16+
17+
with smtplib.SMTP(host='smtp.gmail.com', port=587) as smtp:
18+
smtp.ehlo() # general protocol
19+
smtp.starttls() # start the secure encrypted connection
20+
smtp.login('senderemailid', 'senderemailpassword')
21+
smtp.send_message(email)
22+
print('Email has been sent successfully!')

0 commit comments

Comments
 (0)