Function to send mail on behalf of another person or distribution list.
; #FUNCTION# ====================================================================================================================
; Name...........: _OL_Wrapper_SendMailBehalf
; Description ...: Creatse and sends a mail (wrapper function).
; Syntax.........: _OL_Wrapper_SendMail($oOL[, $sFrom = ""[, $sTo = ""[, $sCc= ""[, $sBCc = ""[, $sSubject = ""[, $sBody = ""[, $sAttachments = ""[, $iBodyFormat = $olFormatUnspecified[, $iImportance = $olImportanceNormal]]]]]]]]])
; Parameters ....: $oOL - Outlook object returned by a preceding call to _OL_Open()
; $sFrom - Optional: The sender
; $sTo - Optional: The recipient(s), separated by ;
; $sCc - Optional: The CC recipient(s) of the mail, separated by ;
; $sBCc - Optional: The BCC recipient(s) of the mail, separated by ;
; $sSubject - Optional: The Subject of the mail
; $sBody - Optional: The Body of the mail
; $sAttachments - Optional: Attachments, separated by ;
; $iBodyFormat - Optional: The Bodyformat of the mail as defined by the OlBodyFormat enumeration (default = $olFormatUnspecified)
; $iImportance - Optional: The Importance of the mail as defined by the OlImportance enumeration (default = $olImportanceNormal)
; Return values .: Success - Object of the sent mail
; Failure - Returns 0
; | Sets @error:
; | 1 - $iBodyFormat is not a number
; | 2 - $sBody is missing
; | 4 - $sTo, $sCc and $sBCc are missing
; | 1xxx - Error returned by function _OL_FolderAccess
; | 2xxx - Error returned by function _OL_ItemCreate
; | 3xxx - Error returned by function _OL_ItemModify
; | 4xxx - Error returned by function _OL_ItemRecipientAdd
; | 5xxx - Error returned by function _OL_ItemAttachmentAdd
; | 6xxx - Error returned by function _OL_ItemSend
; | Sets @extended:
; | -2147352567 - No permission to send email on behalf of $sFrom
; Author ........: water
; Modified.......:
; Remarks .......: This is a wrapper function to simplify sending an email. If you have to set more properties etc. you have to do all steps yourself
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _OL_Wrapper_SendMailBehalf($oOL, $sFrom = "", $sTo = "", $sCc = "", $sBCc = "", $sSubject = "", $sBody = "", $sAttachments = "", $iBodyFormat = $olFormatPlain, $iImportance = $olImportanceNormal)
If Not IsInt($iBodyFormat) Then SetError(1, 0, 0)
If StringStripWS($sBody, 3) = "" Then SetError(2, 0, 0)
If StringStripWS($sTo, 3) = "" And StringStripWS($sCc, 3) = "" And StringStripWS($sBCc, 3) = "" Then SetError(3, 0, 0)
; Access the default outbox folder
Local $aFolder = _OL_FolderAccess($oOL, "", $olFolderOutbox)
If @error Then Return SetError(@error + 1000, @extended, 0)
; Create a mail item in the default folder
Local $oItem = _OL_ItemCreate($oOL, $olMailItem, $aFolder[1], "", "Subject=" & $sSubject, "BodyFormat=" & $iBodyFormat, "Importance=" & $iImportance, "SentOnBehalfOfName=" & $sFrom)
If @error Then Return SetError(@error + 2000, @extended, 0)
; Set the body according to $iBodyFormat
If $iBodyFormat = $olFormatHTML Then
_OL_ItemModify($oOL, $oItem, Default, "HTMLBody=" & $sBody)
Else
_OL_ItemModify($oOL, $oItem, Default, "Body=" & $sBody)
EndIf
If @error Then Return SetError(@error + 3000, @extended, 0)
; Add recipients (to, cc and bcc)
Local $aRecipients
If $sTo <> "" Then
$aRecipients = StringSplit($sTo, ";", 2)
_OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, $aRecipients)
If @error Then Return SetError(@error + 4000, @extended, 0)
EndIf
If $sCc <> "" Then
$aRecipients = StringSplit($sCc, ";", 2)
_OL_ItemRecipientAdd($oOL, $oItem, Default, $olCC, $aRecipients)
If @error Then Return SetError(@error + 4000, @extended, 0)
EndIf
If $sBCc <> "" Then
$aRecipients = StringSplit($sBCc, ";", 2)
_OL_ItemRecipientAdd($oOL, $oItem, Default, $olBCC, $aRecipients)
If @error Then Return SetError(@error + 4000, @extended, 0)
EndIf
; Add attachments
If $sAttachments <> "" Then
Local $aAttachments = StringSplit($sAttachments, ";", 2)
_OL_ItemAttachmentAdd($oOL, $oItem, Default, $aAttachments)
If @error Then Return SetError(@error + 5000, @extended, 0)
EndIf
; Send mail
_OL_ItemSend($oOL, $oItem, Default)
If @error Then Return SetError(@error + 6000, @extended, 0)
Return $oItem
EndFunc ;==>_OL_Wrapper_SendMailBehalf
Edit: Added note showing that @extended=-2147352567 means you don't have permission to send that email.