E-mail can not only be sent through Web browsers but with you console Window too!. So the next question that you might ask is why a Console? There are different reasons why people uses a console window to sent emails. These might be due to the simplicity or with an intention of bulk mailing. When the email sending program inside a program loop he/she can send a hell lot of emails one by one to all his receipts. As a whole, the scope of sending emails with programming has a whole bunch of usages and advantages that might seem to be stupid from a normal person’s assumptions! You know what I meant! LOL.

Below is a complete Source code of a Python program to send email from a computer. Go through it first and then I will explain it to you. Before you continue with this first make sure that the Acess to other systems must be granted by your email service providers.

Important Note: Before you continue with this code, first make sure that the “Allow access to less secure Apps” feature is granted to your account by the mail service providers. In my case, I was using the Gmail service and I enabled this option first from this LINK HERE. If you haven’t yet done this, at least don’t forget to enable it before executing the python script!.

# Import smtplib for the actual sending function
import smtplib

from email.header import Header
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.

me = 'Senders Email Here'  #Content To Be Changed Here
password ='Password Of The Sender'   #Content To Be Changed Here
subject='Subject of the Mail'    #Content To Be Changed Here
recipients = ['Recipient1,Recipient3,Recipient3']  #Content To Be Changed Here
sender = smtplib.SMTP(host='smtp.gmail.com', port=587)  #content To BE Changed Here

fp = open('message.txt', 'rb')   #Content To Be Changed Here
msg = MIMEText(fp.read(), 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['To'] = ', '.join(recipients)
# Send the message via our own SMTP server.
sender = smtplib.SMTP(host='smtp.gmail.com', port=587)
sender .ehlo()
sender .starttls()
sender .ehlo()
sender .login(me, password)
sender .sendmail(me, recipients, msg.as_string())
sender .send
print (msg.as_string())
sender .quit()

About the code

If you have observed closer you can see that the program uses the smtplib library to send emails. If you haven’t yet installed that library on your system and to know more about the library, visit their Website HERE.  In the above program, you will have to make changes to five variables before execution. i.e “me“, “password“, “subject“, “recipients“and the “sender“.

  1. me – e-mail address of the sender (Which means your email address)
  2. password – The password of your email account(The sender’s email account)
  3. subject – The subject of the email
  4. recipients – An array of email address to whom the emails are to be sent.
  5. sender – This is the most important variable to care about. Before sending emails through an SMPT server, you must know the domain name of the server and the port through which the data is to be transferred to the server. In my case, I was using the Gmail service with the domain name”smtp.gmail.com” with the port address “587“.

Once you are done with this you can run the program from the command line. You can also use your skills to make this program more customized for your needs. Make use of files to save the email addresses of the receipts and use can use your program to get data from the files and sent them the email without many headaches. You can save a hell lot of time if you manage to promote your contents with this kinda methods.

If you are running into any exceptions or errors, please let me know! Feel free to comment anything on this 🙂


Read More :