rodrigocf
rodrigocf

Reputation: 2099

Send myself an email using win32com.client

I have a template from code I've gathered from different places to send emails in various of my scripts:

import win32com.client

##################################################
##################################################
##################################################
##################################################
##################################################
#this is a mutipurpose email template

def email_template(recipient, email_subject, mail_body, attachment1, attachment2):
    Format = { 'UNSPECIFIED' : 0, 'PLAIN' : 1, 'HTML' : 2, 'RTF'  : 3}
    profile = "Outlook"
    #session = win32com.client.Dispatch("Mapi.Session")
    outlook = win32com.client.Dispatch("Outlook.Application")
    #session.Logon(profile)
    mainMsg = outlook.CreateItem(0)
    mainMsg.To = recipient
    mainMsg.BodyFormat = Format['RTF']

    #########################
    #check if there is a mail body
    try:
        mainMsg.Subject = email_subject
    except:
        mainMsg.Subject = 'No subject'

    #########################
    #check if there is a mail body
    try:
        mainMsg.HTMLBody = mail_body
    except:
        mainMsg.HTMLBody = 'No email body defined'

    #########################
    #add first attachement if available
    try:
        mainMsg.Attachments.Add(attachment1)
    except:
        pass

    #########################
    #add second attachement if available
    try:
        mainMsg.Attachments.Add(attachment2)
    except:
        pass

    mainMsg.Send()  #this line actually sends the email

Works perfectly. Simple. However I have a slight problem, I'm building a script that needs to send the user an email. Using this template, how do I get the users outlook email? I mean like something like just using "me" and it will get my address.

Thanks!!

Upvotes: 0

Views: 2898

Answers (2)

Midun
Midun

Reputation: 21

We can use the CurrentUser Property of Namespace as Eugene said. The following code suggests a simple implementation of the property

import win32com.client
outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
name = outlook.getNamespace('Mapi')
me = name.CurrentUser()
mail.To = me
mail.Subject = 'Automated Email'
mail.Body = "Hi,"+'\n'+"Python Mail Automation Testing"
mail.Send()

here "me" will be replaced with the current user's email-id. Basically, sending a mail to the sender himself.

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

The CurrentUser property of the Namespace or Account class allows to get the display name of the currently logged-on user as a Recipient object. The Recipient class provides the Address property which returns a string representing the e-mail address of the Recipient.

In case of Exchange server you may need to call more properties and methods:

  1. Use the AddressEntry property of the Recipient class.
  2. Call the GetExchangeUser method of the AddressEntry class which returns an ExchangeUser object that represents the AddressEntry if the AddressEntry belongs to an Exchange AddressList object such as the Global Address List (GAL) and corresponds to an Exchange user.
  3. Get the PrimarySmtpAddress property value. Returns a string representing the primary Simple Mail Transfer Protocol (SMTP) address for the ExchangeUser.

Finally, I'd recommend using the Recipients property of the MailItem class which returns a Recipients collection that represents all the recipients for the Outlook item. The Add method creates a new recipient in the Recipients collection.

 Sub CreateStatusReportToBoss()  
  Dim myItem As Outlook.MailItem  
  Dim myRecipient As Outlook.Recipient 
  Set myItem = Application.CreateItem(olMailItem) 
  Set myRecipient = myItem.Recipients.Add("Eugene Astafiev")
  myItem.Subject = "Status Report"  
  myItem.Display  
 End Sub

Don't forget to call the Resolve or ResolveAll methods of the Recipient(s) class to get your recipients resolved against the address book.

Upvotes: 1

Related Questions