Skip to content

tada1765/python-email

Repository files navigation

python-email

reference: from Corey Schafer.

reference: How to Send Emails Using Python - Plain Text, Adding Attachments, HTML Emails, and More from Corey Schafer video here

1.) Google account setting.

Google Account Settings: https://myaccount.google.com/lesssecureapps https://myaccount.google.com/apppasswords?rapt=AEjHL4MPx6tW8MY5-Ak8UXTgmej5h7RIhxYRcBK00cm_nrQbPbmBMyGyVt4avH7oZUjk7wKXzOyrObyJCuYn2sQReXWSjbSaZw

2.) Hide password using OS environment.

win + search environment:...

3.) Create a new project

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.

4.) Python-email code

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)

**result: ** mailreceived

5.) local debug server

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: localhost

6.) SMTP SSL

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

7.) use email.message package for proper subject and email format:

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

8.) email attachment

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)

result: attachment

9.) attach multiple image

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)

mutiAttach

10.) PDF as attachment email send

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)

PDF

11.) send to multiple people.

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]

  1. 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

result: HTMLMSG

About

reference: from Corey Schafer.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages