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

well but your code does not show where you defined 'MIMEMultipart()' but its implemented on line 30.

well but your code does not show where you defined 'MIMEMultipart()' but its implemented on line 30.

Hi richieking,
Thanks for the reply, I believe it is on line 68. Can you help me out, appreciate it very much.

regards,
mbox_96

Put all your import on top of the page.
that is line 60 through 69, followed by line 59.
That should help solve your problem.
:)

Put all your import on top of the page.
that is line 60 through 69, followed by line 59.
That should help solve your problem.
:)

I tried to place code show below...

def send_email(self,arg1):
      SMTP_SERVER = 'smtp.gmail.com'
      SMTP_PORT = 587
      sender = 'fromsomeone@gmail.com'
      password = "mpassword"
      recipient = 'tosomeone@gmail.com'
      subject = 'Congratz you made it'
      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()
      return arg1

if __name__=='__main__':
   global msg
   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
   import win32com.server.register
   import pythoncom 
   win32com.server.register.UseCommandLine(MicroBox)      
   print pythoncom.CreateGuid() # different each time

I actually move some parts to one and another just to observe its output
but it still gives the error 'global name 'MIMEMultipart' is not defined'. And I noticed that when I place import sys in the send_email method it does not throw error in the VB side. But if when I place import smtplib the error says 'DLL load failed, the specified module cannot be found.

def send_emailt(self,arg1):
      import sys
      import smtplib  #Cause error here
      res=''     
      res="Test if it reach here "+arg1
      return res

Any ideas what I'm missing.

regards,
mbox_96

But if when I place import smtplib the error says 'DLL load failed, the specified module cannot be found.

That means you have to get that module. Very simple error.
Also as i tried to tell you. importing... You cant not implement a method from import in your code if you have not imported its module first. Therefore smtplib,os and re modules are meant to be on the top.
Just to be on the safe side.

Learn how to import before implement. Good rule of the thumb.
:)

Thanks, be back when I get it right..

regards,
mbox_96

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.