locked
PowerShell email function RRS feed

  • Question

  • Hi  , 

    I try to create a sent mail function which will be use by some looping code ,  it was written in PowerShell code as below  , which when i fire the logic in PowerShell ISE , the body message and Email subject where i receive do not have value


    function SendOutlookMail([string]$EmailTo , [string]$EmailSubject ,  [string]$BodyMessageLine1) 
    {
    
    $Outlook = New-Object -ComObject Outlook.Application
    $Mail = $Outlook.CreateItem(0)
    $Mail.To = $EmailTo
    $Mail.Subject = $EmailSubject
    $Mail.Body = $BodyMessageLine1
    $Mail.Send()
    $Outlook.Quit()
    
    }
    # call the function and pass in value to sent mail 
    SendOutlookMail("Some1@email.com" , "Email Subject" , "Body Message")


    if i hard code the value by updating this 2 line , everything work fine , where my receive mail body and subject got value

    $Mail.Subject = "Email Subject"
    $Mail.Body =  "Body Message"
    
    


    what is going on ?

    Tuesday, June 14, 2016 8:15 AM

Answers

  • Hi,

    The issue is with how you are calling the function SendOutlookMail. Do it this way.

    # call the function and pass in value to sent mail 
    SendOutlookMail "Some1@email.com" , "Email Subject" , "Body Message"

    We don't use commas between arguments.  Commas will cause the line to be parsed as an array.


    \_(ツ)_/

    Tuesday, June 14, 2016 1:16 PM

All replies

  • Hi,

    The issue is with how you are calling the function SendOutlookMail. Do it this way.

    # call the function and pass in value to sent mail 
    SendOutlookMail "Some1@email.com" "Email Subject" "Body Message"


    Tuesday, June 14, 2016 9:18 AM
  • function SendOutlookMail([string]$EmailTo,[string]$EmailSubject,[string]$BodyMessageLine1) 
    {
    $Outlook = New-Object -ComObject Outlook.Application
    $Mail = $Outlook.CreateItem(0)
    $Mail.To = $EmailTo
    $Mail.Subject = $EmailSubject
    $Mail.Body = $BodyMessageLine1
    $Mail.Send()
    $Outlook.Quit()
    
    }
    # call the function and pass in value to sent mail 
    SendOutlookMail "Some1@email.com"  "Email Subject"  "Body Message"


    Tuesday, June 14, 2016 9:21 AM
  • Hi,

    The issue is with how you are calling the function SendOutlookMail. Do it this way.

    # call the function and pass in value to sent mail 
    SendOutlookMail "Some1@email.com" , "Email Subject" , "Body Message"

    We don't use commas between arguments.  Commas will cause the line to be parsed as an array.


    \_(ツ)_/

    Tuesday, June 14, 2016 1:16 PM
  • If this is going to be used in a loop then it will not work reliably because you are recreating the Outlook object reeatedly.  This will eventually crash PowerShell and possibly Outlook.

    Here is how to do this:

    $Outlook = New-Object -ComObject Outlook.Application
    
    function Send-OutlookMail{
    	Param(
    		[string]$To, 
    		[string]$Subject,
    		[string]$Body
    	)
    	$Mail = $Outlook.CreateItem(0)
    	$Mail.To = $To
    	$Mail.Subject = $Subject
    	$Mail.Body = $Body
    	$Mail.Send()
    }
    
    # loop here
    # call the function and pass in value to sent mail 
    foreach (...) {
    	# other code
    	Send-OutlookMail -To Some1@email.com -Subject 'Email Subject' -Body 'Body Message'
    }
    
    $Outlook.Quit()


    \_(ツ)_/


    • Edited by jrv Tuesday, June 14, 2016 1:26 PM
    Tuesday, June 14, 2016 1:25 PM
  • Hi,

    The issue is with how you are calling the function SendOutlookMail. Do it this way.

    # call the function and pass in value to sent mail 
    SendOutlookMail "Some1@email.com" , "Email Subject" , "Body Message"

    We don't use commas between arguments.  Commas will cause the line to be parsed as an array.


    \_(ツ)_/

    True, I had tested without commas. While pasting i took what the user had posted. Edited the response. 
    Tuesday, June 14, 2016 4:55 PM
  • # call the function and pass in value to sent mail 
    SendOutlookMail "Some1@email.com" "Email Subject" "Body Message"

    Thanks , remove the commas make it happen :D
    Monday, June 27, 2016 3:04 AM