Home > Uncategorized > Python GMail SMTP Send Email Script

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+="\n\n\n_________________________\nMail generated By  PyGmS "+ver+"\n       - Made By Djays\n        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()
Advertisements
Categories: Uncategorized
  1. Matt
    June 22, 2009 at 4:32 pm

    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: “)

    • djays
      June 22, 2009 at 5:14 pm

      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. jcypher
    June 28, 2009 at 1:39 am

    Thanks, this worked fine for me…just what I was looking for.

  3. November 11, 2009 at 6:53 am

    excellet article.never stop blogging…
    cheers

  4. Paul Hopkinson
    March 16, 2010 at 12:56 pm

    This is fantastic , never used python before and has this running within 5 mins !

  5. Brian
    April 1, 2010 at 8:24 pm

    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

    • djays
      April 1, 2010 at 8:32 pm

      Open Python console -> try
      import email

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

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: