Sending an email from python

Hi guys, I am feeling frustrated becaseu I was able to send two emails from my Python script, but after the second attempt it is still showing me that the emails are sent successfully on the VScode terminal, however, when I check on my email account I am not able to see them in none of the folders. I have checked the SMTP connection enabling the telnet service, I have activated the 2FA on my gmail account, etc, but still, not showing me more than the first two emails that I sent. Any suggestion? Thanks in advance

We can only possibly diagnose problems in code, when that code is actually shown to us.

1 Like

Hi Karl, sure, and thak you for your reply. Here is the code. This is my first time in a forum, so I did not know how it worked

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

import smtplib

from pathlib import Path

from string import Template

message = MIMEMultipart()

message[“from”] = “Someone”

message[“to”] = “email_address”

message[“subject”] =“Python Test”

template = Template(Path(“template.html”).read_text())

body = template.substitute({“name”:“Gigi”})

message.attach(MIMEText(body, “html”))

message.attach(Path(“template.html”))

with smtplib.SMTP(host=“smtp.gmail.com”, port= 587) as smtp:

smtp.ehlo()

smtp.starttls()

smtp.login(“email_address”, “Password”)

print(“sent”)

Hi, It’s not clear from your code how this was formatted (which is very important in Python). Can you please edit your post and surround it with triple-quote marks (three backticks “```”) like this:

import this

That being said - I don’t see any emails being sent in this code :slight_smile: You’re only logging in, but never calling something like

smtp.sendmail(sender_address, recipient_address, message.as_string())
1 Like

Hello Hans, yes, you were right, I added the sent line and it worked :slight_smile: Thanks for your time and reply !!

Hi, how can I show a popup message for Success and Failed email?

Hi,

if there is an email confirmation handshake notifying the sender of success or fail, you can use that message/variable with the following pop-up window function:


import tkinter as tk  # You will have to download the tkinter module/library from pip

send = True     # Comment out one of these for test purposes
send = False

def email_send_confirmation(email_result):
    root = tk.Tk()                              # Create window 
    root.title("Email Send Notification.")      # Window title
    root.minsize(265, 50)                       # Pop-up window size

    # Check message result
    if email_result == True:
        text = tk.Label(root,text = 'Email sent successfully!')
    else:
        text = tk.Label(root,text = 'Failed to send email!')

    text.config(font = ("Times", 12))
    text.pack(padx = 40, pady = 40)
    root.after(5000, root.destroy)              # Value is in milliseconds (change to desired value)

email_send_confirmation(send)

Cheers!

1 Like