How to Send Emails Using Python - Plain Text, Adding Attachments, HTML Emails, and More
Why Stack Overflow recommends it
Stack Overflow users link it when answering or asking about sending mail from Python with smtplib, especially Gmail SMTP setup, app passwords and attachments.
Recommended by 3 people across 3 different months, 2020 to 2021.
What it is
A Python tutorial by Corey Schafer on sending email with smtplib and the email package. It starts with plain text messages and moves on to attachments, HTML bodies and Gmail account settings.
What people said
Recommends C. Tim · 2020-04 · Stack Overflow · on “add excel file attachment when sending python email”
I found an easy way to do it using what Corey Shafer explains in this video [this video] on sending emails with python. import smtplib from email.message import EmailMessage SENDER_EMAIL = "sender_email@gmail.com" APP_PASSWORD = "xxxxxxx" def send_mail_with_excel(recipient_email, subject, content, excel_file): msg = EmailMessage() msg['Subject'] = subject msg['From'] = SENDER_EMAIL msg['To'] = recipient_email msg.set_content(content) with open(excel_file, 'rb') as f: file_data = f.read() msg.add_attachment(file_data, maintype="application", subtype="xlsx", filename=excel_file) with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: smtp.login(SENDER_EMAIL, APP_PASSWORD) smtp.send_message(msg)
Recommends Yankz Kuyateh · 2021-02 · Stack Overflow · on “How do I create a python script such that it sends an email when csv files in a directory has not updated in the last 24 hours?”
Gmail SMTP: Go to [LINK] Enable 2-step-verification, after it's enabled just below it click on"App Passwords", click on "Select app", and select "Other", Type in the name you want and click GENERATE. The password it will generate is the EMAIL_PASSWORD. If this not clear, watch this video [this video]
Recommends Matiiss · 2021-07 · Stack Overflow · on “How to send mail with SMTP relay?”
the setup seems to be unrelated to python, so I suggest that You take a look at the `smtplib` docs [LINK] and watch some tutorials like this one (I suggest this one): How to Send Emails Using Python - Plain Text, Adding Attachments, HTML Emails, and More by Corey Schafer [this video] and then come back when You have issues with Your code or provide a [mre] now. also read [ask] (I believe that this is not the case where code is not necessary)
Cites Kurosh Ghanizadeh · 2020-12 · Stack Overflow · on “Getting "Network is unreachable" for python smtplib”
I've tried to set up email confirmation for django app, and after a few failure steps I tried it with simple python module using smtplib (going through Corey's tutorial [this video] ) but after the first part of tutorial when he got the email I got this error after about 30 seconds of waiting: Traceback (most recent call last): File "main.py", line 10, in <module> with smtplib.SMTP('smtp.gmail.com', port) as smtp: File "/usr/lib/python3.8/smtplib.py", line 253, in __init__ (code, msg) = self.connect(host, port) File "/usr/lib/python3.8/smtplib.py", line 339, in connect self.sock = self._get_socket(host, port, self.timeout) File "/usr/lib/python3.8/smtplib.py", line 308, in _get_socket return sock
Cites Shlok · 2021-11 · Stack Overflow · on “socket.gaierror: [Errno 11001] getaddrinfo failed. Cant send mail through smtplib”
I am trying to send a mail using python with the module SMTPLIB, I followed the instructions of this video [this video] Here is the code import os import smtplib with smtplib.SMTP('stmp.gmail.com', 587) as smtp: smtp.ehlo() smtp.starttls() smtp.ehlo() smtp.login('example@gmail.com', 'examplepassword') Email_subject = '2 Factor Authentication Code' Email_body = "Hello" msg = f'Subject: {Email_subject}\n\n{body}' smtp.sendmail('example@gmail.com', 'example@gmail.com', msg) Here is the error that I'm getting Traceback (most recent call last): File "e:/Programming/Intaf/Production_Register/Malingtry.py", line 7, in <module> with smtplib.SMTP('stmp.gmail.com', 587) as smtp: File "C:\Users\myname\AppDa