Python GMail SMTP Send Email Script

Here is a small script that i made some time ago , it uses GMail’s mail servers to send an email.

#!/usr/bin/python
# PyGms 1.0
# Gmail SMTP  Script Made by Djays


import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

ver = "v1.0"
gmail_user = ""	#Username
gmail_pwd  = ""		#Password
gmail_alt  = " "		#Alias ID
gmail_alias = ""+gmail_alt		#nickname
mailing_list = ""
class mailz:
  def __init__(self):
    self.files=[""]
    self.mail = MIMEMultipart()

  def attach(self,fil):
    self.files.append(fil)

  def mailprep(self,to, subject, text):
    self.to = to
    self.mail['From'] = gmail_alias
    self.mail['To'] = to
    self.mail['Subject'] = subject
    if (mailtoBcc !="") :
      self.mail['BCC'] = mailtoBcc

    text+="nnn_________________________nMail generated By  PyGmS "+ver+"n       - Made By Djaysn        http://djsh.net"

    self.mail.attach(MIMEText(text))
    print self.files

    for attach in self.files:
      if (attach != "") :
          part = MIMEBase('application', 'octet-stream')
          part.set_payload(open(attach, 'rb').read())
          Encoders.encode_base64(part)
          part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(attach))
          self.mail.attach(part)

  def sendmail(self):
    self.mailServer = smtplib.SMTP("smtp.gmail.com", 587)
    self.mailServer.ehlo()
    self.mailServer.starttls()
    self.mailServer.ehlo()
    self.mailServer.login(gmail_user, gmail_pwd)
    self.mailServer.sendmail(gmail_user, self.to, self.mail.as_string())
    self.mailServer.close()

mailto   =  raw_input("Enter Recepients (Seperated By a Comma): ")
mailtoBcc=  raw_input("Enter BCCRecepients (Seperated By a Comma): ")
mailsubj =  raw_input("Enter Subject: ")
mailmsg  =  raw_input("Enter Message: ")
mailattch = ""
newmail  = mailz()

while 1:
 mailattch = ""
 c = raw_input("Attach a File ? (y/n)")
 if c == 'n':
   break
 if c != 'y':
   continue
 mailattch = raw_input("Enter Path of file: ")
 newmail.attach(mailattch)
newmail.mailprep(mailto,mailsubj,mailmsg)

newmail.sendmail()

7 thoughts on “Python GMail SMTP Send Email Script”

  1. Does this work for you? I’ve written my own script and tried this script and can’t get the messages to actually send.

    If I view my “Sent” folder in Gmail, the messages are there, but they never arrive in the inboxes of the addresses I send them to. I’ve had this problem for about a year… I keep coming back thinking it will work _this time_… still no luck.

    Also, I added:

    from getpass import getpass

    in the imports and changed lines 12-13 to:

    gmail_user = raw_input(“From email: “)
    gmail_pwd = getpass(“Password: “)

    1. Yes it works perfectly for me (i dont keep untested stuff online) , i added raw_input and getpass and it still works fine. Probably your firewall/router is blocking it , u can send me the script and i can verify if it works. (Or u can try it on some other network/config)

      PS : It doesnt appear in sent mail if SMTP is used

  2. Hi,
    Just what I’ve been looking for.
    But I get an error message:
    Traceback (most recent call last):
    File “”, line 6, in
    builtins.ImportError: No module named MIMEMultipart
    I’ve never used Python 3 before. Sp it’s probably just me that have done something wrong

    1. Open Python console -> try
      import email

      If you are using Windows then install Python 2.6 package not 3.0

Leave a Reply

Your email address will not be published. Required fields are marked *