Python GMail SMTP Send Email Script
April 8, 2009
4 comments
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()
Categories: Uncategorized


Recent Comments