Is there anyway to have a custom command send an email with a pdf attachment? I don't see this built into the GUI, but maybe there is a way to script it?

Thanks!
Ryan

by (920 points)

1 Answer

by (216k points)
0 votes

Hello Ryan,

Yes, that's possible using a PowerShell script and a built-in PowerShell cmdlet called Send-MailMessage. Here's an ecxample of a PowerShell script that sends an attachment specified by $filePath to a recipient specified by $to. Also, modify the following to match your requirements:

  1. $from - specifies the message sender
  2. $smtpServer - specifies the SMTP server to use to send the message,
  3. $messageSubject - specifies the message subject,
  4. $messageBody - specifies the text in the message body
$filePath = "\\SERVER\Share\MyFile.pdf" # TODO: modify me
$to = "recipient@example.com" # TODO: modify me

# Email message setings
$from = "noreply@domain.com" # TODO: modify me
$smtpServer = "mail.example.com" # TODO: modify me
$messageSubject = "My Subject" # TODO: modify me
$messageBody = "Message Text" # TODO: modify me

if(!(Test-Path -Path $filePath))
{
    $Context.LogMessage("File not found.", "Error")
    return
}

# Send message
Send-MailMessage -To $to -from $from -SmtpServer $smtpServer -Subject $messageSubject -Body $messageBody -Attachments $filePath

To run the script in a Custom Command, use the Run a program or PowerShell script action.

by (920 points)
0

That's awesome! Thank You!

Can you help me with one change? I'd like the recipient to be the target object email field, but if the email field is blank I'd like to use the custom text attribute #6.

by (216k points)
0

Ryan,

Sure!

$filePath = "d:\Projects\adaxes\trunk\source\adaxes\Service\Adaxes.sln" # TODO: modify me

# Email message setings
$from = "noreply@domain.com" # TODO: modify me
$smtpServer = "mail.softerra.com" # TODO: modify me
$messageSubject = "My Subject" # TODO: modify me
$messageBody = "Message Text" # TODO: modify me

# Get recipient address
try
{
    $to = $Context.TargetObject.Get("mail")
}
catch
{
    try
    {
        $to = $Context.TargetObject.Get("adm-CustomAttributeText6")
    }
    catch
    {
        $Context.LogMessage("The recipient doesn't have an e-mail address.", "Error")
        return
    }
}

if(!(Test-Path -Path $filePath))
{
    $Context.LogMessage("File not found.", "Error")
    return
}

$initiator = $Context.BindToObjectByDN("%adm-InitiatorDN%")

try
{
    $initiatorTitle = $initiator.Get("personalTitle")
}
catch
{
    # TODO: what to do if the initiator doesn't have a title
}

try
{
    $initiatorPhone = $initiator.Get("telephoneNumber")
}
catch
{
    # TODO: what to do if the initiator doesn't have a phone number
}

# Send message
Send-MailMessage -To $to -from $from -SmtpServer $smtpServer -Subject $messageSubject -Body $messageBody -Attachments $filePath
by (920 points)
0

Thanks! Another question...is there anyway I can get the initiator's title and phone number in a value reference for use within the email message text?

by (216k points)
0

Ryan,

We've updated the script above. The title and phone number will be assigned to $initiatorTitle and $initiatorPhone respectively.

by (920 points)
0

You're the best! Thanks so much!

Related questions

Hello, We've previously had assistance in implementing a method to transfer email addresses to a specified user via a powershell script for our on-prem Exchange. This has ... $user.SetInfo() Please let me know if you require more clarification. Regards, Josh

asked Aug 25 by jtop (720 points)
0 votes
1 answer

Now that version 2025.1 allows you to type in an external email address in the "Forward to" field, how can you restrict that? We only allow our admins to set ... in Exchange Online, and an Exchange SE server + EntraID Connect to sync the Exchange attributes.

asked Jul 10 by felix (190 points)
0 votes
1 answer

Hello, we are using the script from https://www.adaxes.com/questions/12406/send-approval-request-to-manager-of-group-member?show=12406#q12406 to request approval from a ... the text in the Submitted message without the default message going out. Thank you!

asked Jun 27 by sphelpsjr (20 points)
0 votes
1 answer

I would like to configure a scheduled report to send to a mail contact object. However, these mail contact objects are not showing up when I try to set them as the recipients. Is this possible or is there another way to go about this?

asked Jun 19 by scoutcor (180 points)
0 votes
1 answer

Just recently built a new server, installed 2025.1, and restored configuration from a backup of our other server running 2023.2. I updated the web interface address in ... d6d4f3bd7654 and I'm able to approve/deny from that interface without issue. Any ideas?

asked May 13 by msinger (230 points)
0 votes
1 answer