python - Send myself an email using win32com.client -


i have template code i've gathered different places send emails in various of scripts:

import win32com.client  ################################################## ################################################## ################################################## ################################################## ################################################## #this 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 mail body     try:         mainmsg.subject = email_subject     except:         mainmsg.subject = 'no subject'      #########################     #check if there 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 sends email 

works perfectly. simple. have slight problem, i'm building script needs send user email. using template, how users outlook email? mean using "me" , address.

thanks!!

the currentuser property of namespace or account class allows display name of logged-on user recipient object. recipient class provides address property returns string representing e-mail address of recipient.

in case of exchange server may need call more properties , methods:

  1. use addressentry property of recipient class.
  2. call getexchangeuser method of addressentry class returns exchangeuser object represents addressentry if addressentry belongs exchange addresslist object such global address list (gal) , corresponds exchange user.
  3. get primarysmtpaddress property value. returns string representing primary simple mail transfer protocol (smtp) address exchangeuser.

finally, i'd recommend using recipients property of mailitem class returns recipients collection represents recipients outlook item. add method creates new recipient in recipients collection.

 sub createstatusreporttoboss()     dim myitem outlook.mailitem     dim myrecipient 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 call resolve or resolveall methods of recipient(s) class recipients resolved against address book.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -