Sending email through Maya2012 (using Python and Outlook express)

How to send email using Outlook express through Maya.

We will be using python to send an email through Outlook express and to initiate Outlook express Maya python we will need a module called as : win32com.
For Maya 2012 64 bit :  pywin32-217.win-amd64-py2.6.exe
For Maya 2012 32 bit :  pywin32-217.win32-py2.6.exe  

Its a good idea to install python32bit and python64bit on your machine because you may requires different versions depending upon libraries you are using for your tools and sometimes while loading win32api in Maya2012 64bit so might see .dll errors

all others : http://sourceforge.net/projects/pywin32/files/pywin32/Build%20217/
( To install these executable you will also need Python 2.6 64bit or 32bit installed on your machine )

After installing Python and pywin32 run Maya2012.
Now when you will import win32com module in Maya, your system should know where this module exists therefore  we will add the path of this module in system path.

following is the code to do that :


new_path = "C:/Python26/Libs/2.6/pywin32x217_64bit"
new_path1 = new_path + "/win32"
new_path2 = new_path + "/win32/lib"

if not (new_path in path):
    sys.path.append(new_path)
if not (new_path1 in path):
    sys.path.append(new_path1)
if not (new_path2 in path):
    sys.path.append(new_path2)    
try:
    import win32com
    import win32com.client
except:
    print(sys.exc_info())
    print("\n\nwin32com Library not loaded\n\n")
sys.path.remove(new_path)
sys.path.remove(new_path1)
sys.path.remove(new_path2)
if not (new_path in path):
  sys.path.append(new_path)
  try:
    import win32com
    print "Loaded"
  except:
    print(sys.exc_info())
    print("\n\nwin32 Library not loaded\n\n")
sys.path.remove(new_path)

Now, your Outlook can be configured in different ways.

1. Using Microsoft Exchange :


application = win32com.client.Dispatch("Outlook.Application")
Msg = application.CreateItem(0)
Msg.To = "recipient@domain.com"
Msg.CC = "CCrecipient@domain.com"
Msg.BCC = "BCCrecipient@domain.com"
Msg.Subject = "Subject of your email"
Msg.Body = "Body Content of your email"
attachment1 = "Path to attachment no#1"
attachment2 = "Path to attachment no#2"
Msg.Attachments.Add(attachment1)
Msg.Attachments.Add(attachment2)
Msg.Send()

2 . Using MAPI :

dispatcher = win32com.client.Dispatch("Mapi.Session")
application = win32com.client.Dispatch("Outlook.Application")
dispatcher.Logon("Outlook2010")
Msg = application.CreateItem(0)
Msg.To = "recipient@domain.com"
Msg.CC = "CCrecipient@domain.com"
Msg.BCC = "BCCrecipient@domain.com"
Msg.Subject = "Subject of your email"
Msg.Body = "Body Content of your email"
attachment1 = "Path to attachment no#1"
attachment2 = "Path to attachment no#2"
Msg.Attachments.Add(attachment1)
Msg.Attachments.Add(attachment2)
Msg.Send()

3. Using SMTP :


from smtplib import SMTP, SMTPException

sender = 'sender@domain.com'
receiver = 'receiver@domain.com' 
subject = 'Subject of your email'
messageBody = 'Body Content of your email'
                       
message =  "From: Sender <" + sender + "> \n"
message += "To: To receiver <" + receiver + "> \n"
message += "Subject: " + subject + "\n" 
message += messageBody
try:
    smtpObj = SMTP('smtp.domain')
    smtpObj.sendmail(sender, receiver, message)     
    print "Successfully sent email" 

except SMTPException:
    print "Error: unable to send email"



In function form :

def sendEmail(self, sender, receiver, subject, messageBody, smtpHost):
    from smtplib import SMTP, SMTPException
                                
    message =  "From: Sender <" + sender + "> \n" 
    message += "To: To receiver <" + receiver + "> \n"
    message += "Subject: " + subject + "\n" 
    message += messageBody 
    try:
        smtpObj = SMTP(smtpHost)
        smtpObj.sendmail(sender, receiver, message)      
        print "Successfully sent email" 
    except SMTPException:
        print "Error: unable to send email"

        print sys.exc_info()


Sometimes due to long text or text formatting email sending fails (SMTPServerDisconnected("Connection unexpectedly closed")), for those cases following mime function can be used.



def sendEmail_mime(self, sender, receiver, subject, messageBody, smtpHost):
    import smtplib
    from email.mime.text import MIMEText

    msg = MIMEText(messageBody)
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = receiver
    try:
        # Send the message via our own SMTP server, but don't include the envelope header.
        s = smtplib.SMTP(smtpHost)
        s.sendmail(sender, [receiver], msg.as_string())
        s.quit()
        print "Successfully sent email" 
    except SMTPException:
        print "Error: unable to send email"
        print sys.exc_info()








Comments

Popular posts from this blog

Bounding Box in PIL (Python Image Library)

Dictionary vs KeyValuePair vs Struct - C#

Rendering order and Z-sorting