reference: from Corey Schafer.
reference: How to Send Emails Using Python - Plain Text, Adding Attachments, HTML Emails, and More from Corey Schafer video here
Google Account Settings: https://myaccount.google.com/lesssecureapps https://myaccount.google.com/apppasswords?rapt=AEjHL4MPx6tW8MY5-Ak8UXTgmej5h7RIhxYRcBK00cm_nrQbPbmBMyGyVt4avH7oZUjk7wKXzOyrObyJCuYn2sQReXWSjbSaZw
win + search environment:...
refer: my open Vision Studio Code project with Virtual environment detail here
do not name your file as email.py, because other package of module has use the name, so to prevent crush use other name like mailDemo.py.
on 01_mailDemo.py:
import os
import smtplib
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
print(EMAIL_PASSWORD)
print(EMAIL_ADDRESS)
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
subject = 'Grab dinner this weekend?'
body = 'How about dinner at 6pm this Staturday?'
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail(EMAIL_ADDRESS, 'xxxxxxxxxxxx@gmail.com', msg)
start debug mail server on localhost: goto terminal tpye: python -m smtpd -c DebuggingServer -n localhost:1025
create new python file 02_localhostDebug.py:
import os
import smtplib
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
with smtplib.SMTP('localhost', 1025) as smtp:
subject = 'Grab dinner this weekend?'
body = 'How about dinner at 6pm this Staturday?'
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail(EMAIL_ADDRESS, 'xxxxxxxxxxx@gmail.com', msg)
result: on the terminal will show:
create a python file 03_smtpSSL.py:
import os
import smtplib
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
subject = 'Grab dinner this weekend?'
body = 'How about dinner at 6pm this Staturday?'
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail(EMAIL_ADDRESS, 'xxxxxxxxxxxx@gmail.com', msg)
same result on 1.)
create 04_UseSendMessage.py file:
import os
import smtplib
from email.message import EmailMessage
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
msg = EmailMessage()
msg['Subject'] = 'Grab dinner this weekend?'
msg['From'] = EMAIL_ADDRESS
msg['To'] = 'xxxxxxxxxxx@gmail.com'
msg.set_content('How about dinner at 6pm this Staturday?')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
result : same as 1.)
create a python file 05_emailAttachment.py:
import os
import smtplib
import imghdr
from email.message import EmailMessage
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
msg = EmailMessage()
msg['Subject'] = 'Check out this image here!'
msg['From'] = EMAIL_ADDRESS
msg['To'] = 'xxxxxxxxxxxx@gmail.com'
msg.set_content('Image attached...')
with open('pic.jpg', 'rb') as f:
file_data = f.read()
file_type = imghdr.what(f.name)
file_name = f.name
# print(file_type)
msg.add_attachment(file_data, maintype='image', subtype=file_type, filename=file_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
create a python file 06_multipleAttachment.py
import os
import smtplib
import imghdr
from email.message import EmailMessage
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
msg = EmailMessage()
msg['Subject'] = 'Check out this image here!'
msg['From'] = EMAIL_ADDRESS
msg['To'] = 'www.tada1765@gmail.com'
msg.set_content('Image attached...')
files = ['pic.jpg', 'default.jpg']
for file in files:
with open(file, 'rb') as f:
file_data = f.read()
file_type = imghdr.what(f.name)
file_name = f.name
msg.add_attachment(file_data, maintype='image', subtype=file_type, filename=file_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
create a python file 07_PDFAttach.py:
import os
import smtplib
import imghdr
from email.message import EmailMessage
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
msg = EmailMessage()
msg['Subject'] = 'Check out this PDF here!'
msg['From'] = EMAIL_ADDRESS
msg['To'] = 'xxxxxxx@gmail.com'
msg.set_content('PDF attached...')
files = ['Black logo_TAR UC copy.pdf']
for file in files:
with open(file, 'rb') as f:
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype='application', subtype='octet_stream', filename=file_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
create a python file 08_sendMutilplePeople.py:
import os
import smtplib
import imghdr
from email.message import EmailMessage
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
contacts= ['xxxxxxxxxxxx@gmail.com', 'yyyyyyyyyyyyyy@gmail.com']
msg = EmailMessage()
msg['Subject'] = 'Check out this PDF here!!!'
msg['From'] = EMAIL_ADDRESS
msg['To'] = contacts
# msg['To'] = ', '.join(contacts)
msg.set_content('PDF attached...')
files = ['Black logo_TAR UC copy.pdf']
for file in files:
with open(file, 'rb') as f:
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype='application', subtype='octet_stream', filename=file_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
result:[bothreceive]
- Create HTML messages email
create a python file 09_HTMLMessage.py:
import os
import smtplib
import imghdr
from email.message import EmailMessage
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
contacts= ['xxx@gmail.com', 'yyyy@gmail.com']
msg = EmailMessage()
msg['Subject'] = 'Check out this HTML text here!!!'
msg['From'] = EMAIL_ADDRESS
msg['To'] = contacts[0]
# msg['To'] = ', '.join(contacts)
msg.set_content('This is a plain text email')
msg.add_alternative("""\
<!DOCTYPE html>
<html>
<body>
<h1 style="color:SlateGray;">This is an HTML Email!</h1>
</body>
</html>
""", subtype='html')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
""" = multi-line string