0 votes

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

0 votes
by (216k points)

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.

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.

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
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?

0

Ryan,

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

0

You're the best! Thanks so much!

Related questions

0 votes
1 answer

Hello, I'm wondering if it's possible to export a list of all users in AD along with their email addresses to an Excel spreadsheet and then schedule that export to append ... address that wasn't previously used. Please let me know if this is possible. Thanks!

asked Apr 11 by sjjb2024 (60 points)
0 votes
1 answer

The default pattern format we need should be :First letter of User firstname concatinated to user lastname and pd.sandiego.gov as in jdoe@pd.sandiego.gov

asked Jan 23 by hhsmith (40 points)
0 votes
1 answer

Hello team, I created this custom command and action The manager is not notified by email about a new approval, only visible in portal Once the Manager approves it, the whole ... can I build a flow of two approvers? First manager, then owner of target group

asked Oct 20, 2023 by wintec01 (1.1k points)
0 votes
1 answer

Hi team, I am trying to set a primary smtp address to a user based on input during creation. If someone set a specific email in form, this is stored in customAttributeText3 and ... a specifc email address as primary? Do I need to run a PS command to set it?

asked Oct 13, 2023 by wintec01 (1.1k points)
0 votes
1 answer

A little bit of context: There are 3 departments that share 1 Active Directory. Now each department has its own OU. I would like to have an email sent when a user is ... if this is possible without Powershell? If not, is there a pre-existing script for this?

asked Oct 3, 2023 by Cas (150 points)
3,346 questions
3,047 answers
7,782 comments
544,982 users