email_sender.py – Source Code Library | Python

email_sender.py - python source code

email_sender.pypython source code

@AppName: CtechF Email Sender
@Creater: admin@ctechf.com
@Date: 6/17/2019

✓ Source Code Can be used free, for commercial and non-commercial use
✓ You can make modifications to source and re-publish it.
✓ Crediting isn’t required, but linking back is greatly appreciated and allows us to gain exposure.

Source Code by admin@ctechf.com from https://ctechf.com
Contact: admin@ctechf.com

1) What is Python?

CtechF - Python

Python is a scripting language and that is used for creating engineering-analysis tools, web development (server-side), software development, system scripting, and animation software. It designed by Guido van Rossum and first released in 1991. It’s an interpreted, High-level. General-purpose language. Python runs on Windows, Mac OS, and Linux.

Python Tutorial: Introduction for Beginners (Part 1)
Python Tutorial: Introduction for Beginners (Part 2)
SQL Tutorial: Introduction for beginners (Basic)
Python Tutorial: How to Connect MySQL Database in Python

2) What is GUI?

python gui programming

GUI (Graphical User Interface) is an interface that used an icon, windows, menus, buttons or other visual indicators to interact with hardware. all version of Microsoft Windows is a GUI, MS-DOS is a command line. GUI was first developed at Xerox PARC by Alan Kay and Douglas Engelbart in 1981. GUI Operating Systems (OS ) is mainly navigated using a mouse.

Python Tutorial: Python GUI Programming (Tkinter)

3)  Send Emails Using Python

send emails using python

SMTP (Simple Mail Transfer Protocol) is a communication protocol for mail transmission. It defined in 1982 by RFC 821. SMTP as a method to send mail from one user to another. Python comes with the pre-installed “smtblib” module for sending emails using the Simple Mail Transfer Protocol (SMTP). The module defines an SMTP client session object that can be used to send mail to any computer with an SMTP or ESMTP listener. In this post, you’ll learn how to send emails using python.

Python Tutorial: Send Emails Using Python

Import

import email, smtplib, os
from tkinter import *
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from tkinter import filedialog
from tkinter import messagebox

Global Variables

file_location = [] #Save Attachments Links
file_name = [] #Save Attachments Names
email_to = [] #Save Reciver Email Address

email_from = "" #Save Sender Email Address
password = "" #Save Sender Email Password (Plain Text)

Create SMTP Connection

smOb = smtplib.SMTP('smtp.gmail.com', 587)
smOb.starttls()

Login to the Gmail – Function

email_sender.py - python source code
def getLogin():
        while True:
                email_from = input("Enter Your Email Address and Press Enter (GMAIL ONLY): ")
                password = input("Enter Password and Press Enter : ")
                try:
                        smOb.login(email_from, password) #Logging to the Gmail
                        print("Successfully logged into your Gmail Account")
                        break
                except Exception as e:
                        print(e)
                        print("")

File Attachment – Function

def dopen():
        dfile_location = (filedialog.askopenfilename(initialdir = "/",title = "Open file",filetypes = (("All files","*.*"),("Text files","*.txt")))) #Open Dialog Box
        if (dfile_location != ''):
                if (dfile_location in file_location):
                        messagebox.showinfo("Already Attached!", "Files already attahed in this Email")
                else:
                        file_location.append(dfile_location) #Add Attachment location to the list
                        dfile_name = os.path.basename(dfile_location) #Get Attachment Name to the Variable
                        file_name.append(dfile_name) #Add Attachment Name to the List

                        lbl_attach = Label(row2, text=dfile_name + " - Successfully Attached", anchor='w')
                        lbl_attach.pack()

Add Emails to the list – Function

def addEmail():
   if (txtTo.get() != ''):
           demail = txtTo.get()
           if (demail in email_to):
                messagebox.showinfo("Email Added!", "Email Exists in the list")
           else:
                email_to.append(demail) #Email add to the list
                email_add_msg = demail + " - Email Added to List"
                lbl_attach = Label(row2, text=email_add_msg, anchor='w')
                lbl_attach.pack()
                enty_to.delete(0, END) #Clear To: Text

Sent Email

def sentEmail():
        message = MIMEMultipart("alternative") #Create MIMEMultipart
        if (txtSub.get() != ''):
                message["Subject"] = txtSub.get() #Get Subject Text

                
        plain_body = ""  #Plain Email
        #html_body = ""  #HTML Email

         #Get Message Text
        if (txtBod.get() != ''):
                plain_body = txtBod.get() + """

         Sent By: CtechF Email Sender
        """
                
        
        message.attach(MIMEText(plain_body, 'plain'))
        #message.attach(MIMEText(html_body, 'html'))
                
        #Attachment
        count_attach = len(file_location) #Count Attachment
        for i in range(count_attach):
                email_attach = MIMEBase("application", "octet-stream") 
                attach_file = open(file_location[i], "rb") #Get Attachment File
                email_attach.set_payload(attach_file.read())
                encoders.encode_base64(email_attach) #Encode
                file_name_now = file_name[i]
                email_attach.add_header(
                    "Content-Disposition",
                    f"attachment; filename= {file_name_now}",
                )
                message.attach(email_attach) #Attach File to the email
                

        try:
                email_message = message.as_string()
                for email in email_to:
                        smOb.sendmail(email_from, email, email_message) #Send Email
                        email_msg = "Email sent: "+email 
                        lbl_attach = Label(row2, text=email_msg, anchor='w')
                        lbl_attach.pack()
                del email_to[0:] #Clear Email List
                del file_location[0:] #Clear Attachment List
                del file_name[0:] #Clear Attachment Name List
                enty_sub.delete(0, END)
                enty_bod.delete(0, END)
                lbl_attach = Label(row2, text="============Data Cleared============", anchor='w')
                lbl_attach.pack()
        except Exception as e:
                print (e)

Quit – Function

def quit():
        smOb.quit()

Call back getLogin Function

getLogin()

GUI

scr = Tk()
scr.title("CtechF Email Sender")
scr.geometry('450x600')

#Variables  
txtTo = StringVar()
txtSub = StringVar()      
txtBod = StringVar()    

#To:
row_to = Frame(scr)
lbl_to = Label(row_to, width=10, text="To: ", anchor='w')
lbl_to.pack(side=LEFT)
enty_to = Entry(row_to, textvariable=txtTo)
enty_to.pack(side=LEFT, expand=YES, fill=X)
btnAddemail = Button(row_to, text = 'Add Email', command=addEmail)
btnAddemail.pack(side=RIGHT, fill=X)
row_to.pack(side=TOP, fill=X, padx=5, pady=5)

#Subject:
row_sub = Frame(scr)
lbl_sub = Label(row_sub, width=10, text="Subject: ", anchor='w')
lbl_sub.pack(side=LEFT)
enty_sub = Entry(row_sub, textvariable=txtSub)
enty_sub.pack(side=RIGHT, expand=YES, fill=X)
row_sub.pack(side=TOP, fill=X, padx=5, pady=5)

#Message
row_bod = Frame(scr)
lbl_bod = Label(row_bod, width=10, text="Message: ", anchor='w')
lbl_bod.pack(side=LEFT)
enty_bod = Entry(row_bod, textvariable=txtBod)
enty_bod.pack(side=RIGHT, expand=YES, fill=X)
row_bod.pack(side=TOP, fill=X, padx=5, pady=5)

#Buttons
row1 = Frame(scr)
row1.pack(side=TOP, fill=X, padx=5, pady=5)
btnAttach = Button(row1, text = 'Attach File', command=dopen)
btnAttach.pack(side=LEFT, expand=YES, fill=X)
btnAttach = Button(row1, text = 'Quit', command=scr.destroy)
btnAttach.pack(side=RIGHT, expand=YES, fill=X)

#Frame
row2 = Frame(scr)
row2.pack(side=TOP, fill=Y, padx=5, pady=5)  

#Output
row4 = Frame(scr)
row4.pack(side=TOP, fill=X, padx=5, pady=5)
btnSend = Button(row4, text = 'Send Email', command=sentEmail)
btnSend.pack(side=RIGHT, expand=YES, fill=X)

scr.mainloop()

Complete Source Code

email_sender.py

"""
@AppName: CtechF Email Sender
@Creater: admin@ctechf.com
@Date: 6/17/2019

✓Source Code Can be used free, for commercial and non-commercial use
✓You can make modifications to source and republish it.
✓Crediting isn’t required, but linking back is greatly appreciated and allows us to gain exposure.


Source Code by admin@ctechf.com from https://ctechf.com

For more details visit: https://ctechf.com/email_sender-py-source-code-library-python/
Contact: admin@ctechf.com
"""


#import
import email, smtplib, os
from tkinter import *
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from tkinter import filedialog
from tkinter import messagebox

#Global Variables
file_location = [] #Save Attachments Links
file_name = [] #Save Attachments Names
email_to = [] #Save Reciver Email Address

email_from = "" #Save Sender Email Address
password = "" #Save Sender Email Password (Plain Text)

#Start SMTP Connection
smOb = smtplib.SMTP('smtp.gmail.com', 587)
smOb.starttls()

#Get Login
def getLogin():
        while True:
                email_from = input("Enter Your Email Address and Press Enter (GMAIL ONLY): ")
                password = input("Enter Password and Press Enter : ")
                try:
                        smOb.login(email_from, password) #Logging to the Gmail
                        print("Successfully logged into your Gmail Account")
                        break
                except Exception as e:
                        print(e)
                        print("")
        


#Attach File
def dopen():
        dfile_location = (filedialog.askopenfilename(initialdir = "/",title = "Open file",filetypes = (("All files","*.*"),("Text files","*.txt")))) #Open Dialog Box
        if (dfile_location != ''):
                if (dfile_location in file_location):
                        messagebox.showinfo("Already Attached!", "Files already attahed in this Email")
                else:
                        file_location.append(dfile_location) #Add Attachment location to the list
                        dfile_name = os.path.basename(dfile_location) #Get Attachment Name to the Variable
                        file_name.append(dfile_name) #Add Attachment Name to the List

                        lbl_attach = Label(row2, text=dfile_name + " - Successfully Attached", anchor='w')
                        lbl_attach.pack()
                
#Add Email to the list 
def addEmail():
   if (txtTo.get() != ''):
           demail = txtTo.get()
           if (demail in email_to):
                messagebox.showinfo("Email Added!", "Email Exists in the list")
           else:
                email_to.append(demail) #Email add to the list
                email_add_msg = demail + " - Email Added to List"
                lbl_attach = Label(row2, text=email_add_msg, anchor='w')
                lbl_attach.pack()
                enty_to.delete(0, END) #Clear To: Text


#Sent Email
def sentEmail():
        message = MIMEMultipart("alternative") #Create MIMEMultipart
        if (txtSub.get() != ''):
                message["Subject"] = txtSub.get() #Get Subject Text

                
        plain_body = ""  #Plain Email
        #html_body = ""  #HTML Email

         #Get Message Text
        if (txtBod.get() != ''):
                plain_body = txtBod.get() + """

         Sent By: CtechF Email Sender
        """
                
        
        message.attach(MIMEText(plain_body, 'plain'))
        #message.attach(MIMEText(html_body, 'html'))
                
        #Attachment
        count_attach = len(file_location) #Count Attachment
        for i in range(count_attach):
                email_attach = MIMEBase("application", "octet-stream") 
                attach_file = open(file_location[i], "rb") #Get Attachment File
                email_attach.set_payload(attach_file.read())
                encoders.encode_base64(email_attach) #Encode
                file_name_now = file_name[i]
                email_attach.add_header(
                    "Content-Disposition",
                    f"attachment; filename= {file_name_now}",
                )
                message.attach(email_attach) #Attach File to the email
                

        try:
                email_message = message.as_string()
                for email in email_to:
                        smOb.sendmail(email_from, email, email_message) #Send Email
                        email_msg = "Email sent: "+email 
                        lbl_attach = Label(row2, text=email_msg, anchor='w')
                        lbl_attach.pack()
                del email_to[0:] #Clear Email List
                del file_location[0:] #Clear Attachment List
                del file_name[0:] #Clear Attachment Name List
                enty_sub.delete(0, END)
                enty_bod.delete(0, END)
                lbl_attach = Label(row2, text="============Data Cleared============", anchor='w')
                lbl_attach.pack()
        except Exception as e:
                print (e)

#Quit
def quit():
        smOb.quit()

        
getLogin()

#GUI 
scr = Tk()
scr.title("CtechF Email Sender")
scr.geometry('450x600')

#Variables  
txtTo = StringVar()
txtSub = StringVar()      
txtBod = StringVar()    

#To:
row_to = Frame(scr)
lbl_to = Label(row_to, width=10, text="To: ", anchor='w')
lbl_to.pack(side=LEFT)
enty_to = Entry(row_to, textvariable=txtTo)
enty_to.pack(side=LEFT, expand=YES, fill=X)
btnAddemail = Button(row_to, text = 'Add Email', command=addEmail)
btnAddemail.pack(side=RIGHT, fill=X)
row_to.pack(side=TOP, fill=X, padx=5, pady=5)

#Subject:
row_sub = Frame(scr)
lbl_sub = Label(row_sub, width=10, text="Subject: ", anchor='w')
lbl_sub.pack(side=LEFT)
enty_sub = Entry(row_sub, textvariable=txtSub)
enty_sub.pack(side=RIGHT, expand=YES, fill=X)
row_sub.pack(side=TOP, fill=X, padx=5, pady=5)

#Message
row_bod = Frame(scr)
lbl_bod = Label(row_bod, width=10, text="Message: ", anchor='w')
lbl_bod.pack(side=LEFT)
enty_bod = Entry(row_bod, textvariable=txtBod)
enty_bod.pack(side=RIGHT, expand=YES, fill=X)
row_bod.pack(side=TOP, fill=X, padx=5, pady=5)

#Buttons
row1 = Frame(scr)
row1.pack(side=TOP, fill=X, padx=5, pady=5)
btnAttach = Button(row1, text = 'Attach File', command=dopen)
btnAttach.pack(side=LEFT, expand=YES, fill=X)
btnAttach = Button(row1, text = 'Quit', command=scr.destroy)
btnAttach.pack(side=RIGHT, expand=YES, fill=X)

#Frame
row2 = Frame(scr)
row2.pack(side=TOP, fill=Y, padx=5, pady=5)  

#Output
row4 = Frame(scr)
row4.pack(side=TOP, fill=X, padx=5, pady=5)
btnSend = Button(row4, text = 'Send Email', command=sentEmail)
btnSend.pack(side=RIGHT, expand=YES, fill=X)

scr.mainloop()

GitHub – https://github.com/ctechf/email_sender.py

Hi Friends,

@AppName: CtechF Email Sender
@Creater: admin@ctechf.com
@Date: 6/17/2019

Hope this Source Code (email_sender.py) will help you to learn something. If you enjoy my work, please Share this with your friends and anyone who might be interested in programming. Don’t forget to Comment and, Subscribe.

Source Code by admin@ctechf.com from https://ctechf.com
Contact: admin@ctechf.com

See you in the comment section.

Like ctechf Facebook fan page : https://www.facebook.com/ctechf

Also Read :

How to be a Programmer

Leave a Reply

35 Shares
Share
Tweet
Share
Pin
Share