Reading HTML emails with PowerShell RRS feed

  • Question

  • Hi,

    Im attempting to write a script to read specific parts of an email. This is to create user accounts in AD based on an email received from our recruitment system.

    The system currently issues an HTML formated email when someone is recruited. Formate similar to this...

    Candidate name: [%ApplicantFirstname%] [%ApplicantSurname%]<o:p></o:p>

    Known as:  [%OFF_CANDIDATE_KNOWN_%] <o:p></o:p>

    Start date: [%OFF_START_DATE%]<o:p></o:p>

    Recruitment method: [%OFF_RECRUITMENT_METH%]<o:p></o:p>

    Job title: [%VacancyTitle%]<o:p></o:p>

    Employment type: [%OFF_EMPLOYMENT_TYPE_%]<o:p></o:p>

    Functional Director Area/Team: [%VacancyStructure%]<o:p></o:p>

    Reports to (manager name): [%OFF_REPORTS_TO_MANA%]<o:p></o:p>

    the % variables are automaticlaly replaced in the email when it gets sent. 

    The script i've got so far is...

    $senderEmailAddress = "senderemail@domain.com"
    add-Type -assembly "Microsoft.Office.Interop.Outlook" #setup the outlook assembly
    $Outlook = New-Object -comobject Outlook.Application #connect to outlook
    $namespace = $Outlook.GetNameSpace("MAPI") #set the namespace to use mapi
    $recruitmentEmails = $namespace.folders.item(2).folders.item(2).folders.item('test').items #select the test subfolder from inbox and all the items (emails) within that folder
    ForEach($email in $recruitmentEmails){
        If($email.senderEmailAddress -eq $senderEmailAddress){
            $emailHTMLBody = $email.htmlbody
            $emailHTMLBody | Where-Object {$_ -match 'Candidate Name:'} | ForEach {$matches[0]}
        }#end If sender email match block
    }#end of forEach loop

    i can read the email and get the body of the email fine, but what i need to do is just select the bit after the headings, so for example the bit after Candidate Name or Known As: etc. The match works but only reports back the match. 

    The email is HTML formated. 

    Any help appreciated. 


    Denis Cooper


    Help keep the forums tidy, if this has helped please mark it as an answer

    Blog: http://www.windows-support.co.uk  Twitter:   LinkedIn:

    Thursday, May 30, 2019 2:15 PM

All replies