AnsweredAssumed Answered

How to send an email with multiple attachments and hyperlinks without paying for special software- Solved

Question asked by MichaelWiseman on Nov 29, 2016
Latest reply on Dec 1, 2016 by MichaelWiseman

The solution is to have Filemaker pass parameters to a .bat file that can handle it for you.

 

You need:

 

Outlook (some versions throw a fit when you try to access them from an external program. I am using Outlook 2010)

FM

Powershell ISE (native to all Windows computers)

notepad.

 

I use filefire, but you can be clever and use an export to write the .bat file.

 

1) Make a .bat file (a text file given a .bat extension) that contains the following (change the path for your needs)

@ powershell.exe -executionpolicy Bypass -file S:\POWERSHELL_SCRIPTS\Email.ps1 "{TO}|{CC}|{SUBJECT}|{LINK}|{MESSAGE}|{ATTACHMENTS}"

pause

 

2) open Powershell ISE, and paste the following. Save the file on the path from above.

 

function PSFunction([string]$Parameter)

{

  $parameters = $parameter.split("|")

$Outlook = New-Object -ComObject Outlook.Application

$Mail = $Outlook.CreateItem(0)

$Mail.To = $parameters[0]

 

 

$Mail.CC = $parameters[1]

$Mail.Subject = $parameters[2]

$Link = $parameters[3]

$Message = $parameters[4]

$Attachments=$parameters[5]

 

$Mail.HtmlBody=" "

if($link.length -gt 0){

$Mail.htmlbody = $Mail.htmlbody + "<a href="+$Link+ ">Link</a><br><br>"

}

 

if($message.length -gt0){

$message=$message.split("^^")

$Mail.htmlbody = $Mail.htmlbody + $Message

}

 

#Split Attachments

$Attachments=$attachments.split("^^")

write-host $Attachments

$n=0

foreach ($item in $attachments){

write-host $n

 

 

    if($item.length -gt 0){

    write-host $item

    $Mail.Attachments.Add($Attachments[$n])

    #$Mail.Attachments.Add($item.DirectoryName + "\" + $item.Name)

    write-host "added " $Attachments[$n]

    }

 

 

$n++

}

#$mail.Display()    <--- The Hash makes this a comment. Remove it and put it on the send so the dialog pops up.

$Mail.Send()

 

}

 

PSFunction  $args[0]

 

3. Make this script

$$Email_To, $$Email_CC, $$Email_Subject, $$Email_Link, $$Email_Message, $$Email_Attachments are the global parameters.

4. The FM script will modify the .BAT and write a custom .bat with the parameters loaded. It will then call the .bat which will call the Powershell (Under default settings, powershell scripts are not allowed to just be called unless you bypass them in the .BAT file. T

 

Outcomes