Python — Send Email Using SMTP
Send Mail To Any Internet Machine (SMTP or ESMTP) #PurePythonSeries — Episode #11
Hi in theses last two episodes (01 and 02) of this series we use MS Outlook and yagmail.
But theses two solution brought a problem: If your email is not native, it fatally will lead your email to spam, because the servers do not match.
But using SMTP there is no issue. You access the server itself.

Let’s Get Started:
Step-by-Step Index:
Create an SMTP object to connect to the server.
Log in to your account.
Define your message header and your login credentials.
Create a MIMEMultipart object and associate the relevant header with it - example: From:, To:, and Subject.
Associate a message with the MIMEMultipart message object.
Finally, send the message.
01#Step —Login to SMTP Server
import smtplibserver = smtplib.SMTP('<mensager.of.your.cia>', 25)
server.starttls()
server.login("<Your-email>", "<Your-password>")Out[1]:(235, b'2.7.0 Authentication successful')
02#Step — Login to SMTP ServerSend email
msg = "Testing SMTP EMAIL "
server.sendmail("<Your-email>", "<Other-email>", msg)
server.quit()
Simple like that!
Now Let’s use Pandas to load the file that brings the list to where we wanto to send email.
03#Step — Load email’s list (Excel file)
# Using PANDASimport pandas as pd
list_table = pd.read_excel('email_schedule_table.xlsx')
display(list_table)
[this prints your table list]
04#Step — First Single test
# Import smtplib for the actual sending function
import smtplib
import osbase_path = "<to.readme.file>"
filename = "readme"
path_to_file = os.path.join(base_path, filename)# Import the email modules we'll need
from email.message import EmailMessage# Open the plain text file whose name is in textfile for reading.
with open(path_to_file, 'r') as fp:
# Create a text/plain message
msg = EmailMessage()
msg.set_content(fp.read())# me == the sender's email address
# you == the recipient's email addressmsg['Subject'] = f'The contents of {textfile}'
msg['From'] = '<your_email>'
msg['To'] = '<another_email>'# Send the message via our own SMTP server.
s = smtplib.SMTP('<mensager.of.your.cia>')
s.send_message(msg)
s.quit()
05#Step — Now the Multiple Emails
# Import smtplib for the actual sending functionimport smtplib
from datetime import datetime# Import the email modules we'll needfrom email.message import EmailMessage
server = smtplib.SMTP('<mensager.of.your.cia>')for i, email in enumerate(admin_df['Email']):
Responsavel = admin_df.loc[i, 'Responsible']
IP = admin_df.loc[i, 'IP']
Intimation = admin_df.loc[i, 'Intimation']
Default = admin_df.loc[i, 'Default']
CNPJ = admin_df.loc[i, 'CNPJ']
msg = EmailMessage()
body = f"""\
To{IP} - N°: {CNPJ}
{Responsible} Dear Administrator - DIMP (Declaration of Means of Payments) -
Public Sector: We request the (re)transmission of DIMP files related to the
period(s) recorded below (month/year): {Default.split(';')} Motivations: EXCLUSIVELY FOR UPDATING AND UPDATING PURPOSES IN THE
INSTITUTIONAL DATABASE OF THIS CIA; PLEASE SEND ALL
FILES WITH NORMAL PURPOSE. Thank you. Sincerely, ------------------------
<your_name>
<your_title>
<your_sector>
EMAIL: <your_email>
TEL: <your_contact> ------------------- """msg.set_content(body)# me == the sender's email address
# you == the recipient's email addressmsg['Subject'] = f" Report of Omissions DA {IP} - {CNPJ} - FOR FINANCE CIA - AT - Request No. {Intimation} "msg['From'] = '<your_email>'
msg['To'] = email# Send the message via our own SMTP server.
#server = smtplib.SMTP('<mensager.of.your.cia>')print("Email successfully sent!")
Email successfully sent!!
That’s it!
Bye o/
👉Jupiter notebook link :)
👉git
Credits & References:
https://stackoverflow.com/questions/41845518/jupyter-anaconda-load-text-file-into-python/48785326
Official smtplib — SMTP protocol client: https://docs.python.org/3/library/smtplib.html
examples: https://docs.python.org/3/library/email.examples.html#email-examples
com HTML: https://code.tutsplus.com/pt/tutorials/sending-emails-in-python-with-smtp--cms-29975
Python: “subject” not shown when sending email using smtplib module: https://stackoverflow.com/questions/7232088/python-subject-not-shown-when-sending-email-using-smtplib-module
Jupyter Anaconda: load text file into python: https://stackoverflow.com/questions/41845518/jupyter-anaconda-load-text-file-into-python/48785326
date: 10/2021
00#Episode#PurePythonSeries — Lambda in Python — Python Lambda Desmistification
01#Episode#PurePythonSeries — Send Email in Python — Using Jupyter Notebook — How To Send Gmail In Python
02#Episode#PurePythonSeries — Automate Your Email With Python & Outlook — How To Create An Email Trigger System in Python
03#Episode#PurePythonSeries — Manipulating Files With Python — Manage Your Lovely Photos With Python!
04#Episode#PurePythonSeries — Pandas DataFrame Advanced — A Complete Notebook Review
05#Episode#PurePythonSeries — Is This Leap Year? Python Calendar — How To Calculate If The Year Is Leap Year and How Many Days Are In The Month
06#Episode#PurePythonSeries — List Comprehension In Python — Locked-in Secrets About List Comprehension
07#Episode#PurePythonSeries — Graphs — In Python — Extremely Simple Algorithms in Python
08#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function
10#Episode#PurePythonSeries — CS50 — A Taste of Python — Harvard Mario’s Challenge Solver
11#Episode#PurePythonSeries — Python — Send Email Using SMTP— Send Mail To Any Internet Machine (SMTP or ESMTP) (this one)