Monday, 16 June 2014

Send email with Outlook in Python

We can send email by outlook with filling all details by python code. and we can implement send email functionality in our project easily.

First we have to give details like from, to  subject and attachment then this all details will be filled in outlook with python code. please see the below code.


import win32com.client
from win32com.client import Dispatch, constants
 
const=win32com.client.constants
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "I AM SUBJECT!!"
newMail.Body = "I AM IN THE BODY\nSO AM I!!!"
newMail.To = "abc@abc.com"
attachment1 = r"E:\test\logo.png"

newMail.Attachments.Add(Source=attachment1)
newMail.display()

#newMail.send()

8 comments:

  1. hey,
    how to i just open the new message window in outklook , so that i can manually enter the recipient name and send manually?

    ReplyDelete
    Replies
    1. This program is for only sending email with pre defined subject and messages in pgm and send through outlook application. here we are using published api (win32) and we are accessing those api ,sending email through it. It will not open outlook application and u can't provide details manually.

      If you want to open outlook application or any windows application. please go through "AutoItLibrary" module in python. which will provide the access to control any windows application (like open and close app and click button).

      Delete
  2. Is there any way i can set the account(multiple accounts in outlook) from which mail has to be sent

    ReplyDelete
  3. Is there any way i can select the account(Multiple accounts in outlook) from which the mail has to be sent

    ReplyDelete
    Replies
    1. You can send mail from multiple accounts in outlook. only u have to pass email id in to "newMail.From" if multiple accounts are connected.

      Delete
  4. Is there way to read the email address from the excel file and send email in python?

    ReplyDelete
    Replies
    1. First read excel sheet cell by cell from openpyxl or xlrd third party module and find the email address through regular expression. and send email body whatever you want.

      Delete