Hello python experts!
I have a simple script that sends an email with no problem, in order to interface with vb6 I'm tried to modify the entire code into COM service...but I'm having trouble with this error..global name 'MIMEMultipart' is not defined which is on the send_email method. any help is appreciated..
class MicroBox:
_reg_clsid_ = '{BEA1AA48-1D0A-4D19-8E98-03C54F195B59}'
_reg_desc_ = "COM Server Demo V1.0"
_reg_progid_ = "Python.TestServer"
_public_methods_ = ['about','send_email']
_public_attrs_ = ['softspace', 'noCalls']
_readonly_attrs_ = ['noCalls']
def __init__(self):
self.softspace = 2
self.noCalls = 0
def about(self,arg1,arg2):
res=''
res="Demo Sending Email "+arg1+'"%011d"' % arg2
return res
def send_email(self,arg1):
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = 'fromsomeone@gmail.com'
password = "mypassword"
recipient = 'tosomeone@gmail.com'
subject = 'Python emaillib Test'
message = 'Images attached.'
msg = MIMEMultipart()
msg['Subject'] = 'My Emal and attchement Prototype'
msg['To'] = recipient
msg['From'] = sender
directory = "C:\\python26\\"
files = os.listdir(directory)
gifsearch = re.compile("image1.jpg", re.IGNORECASE)
files = filter(gifsearch.search, files)
for filename in files:
path = os.path.join(directory, filename)
if not os.path.isfile(path):
continue
img = MIMEImage(open(path, 'rb').read(), _subtype="jpg")
img.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(img)
part = MIMEText('text', "plain")
part.set_payload(message)
msg.attach(part)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
session.sendmail(sender, recipient, msg.as_string())
session.quit()
if __name__=='__main__':
global msg
import win32com.server.register
win32com.server.register.UseCommandLine(MicroBox)
import pythoncom
print pythoncom.CreateGuid() # different each time
import os, re
import sys
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
thanks in advance,
mbox_96