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

We are currently using the script below to check for a unique username/upn. If the name is not unique, it adds a number to the end. In the Adaxes logs, everything appears ... .LogMessage("The username has been changed to " + $uniqueUPN ` + ".", "Information")

asked 4 days ago by cewilson (270 points)
0 votes
1 answer

We are using the below snippet to grab the email of a single custom attribute object. Can I get guidance on the best way to modify this to get all the emails of each ... "The user specified in parameter 'MyParameter' has no email address. ", "Information") }

asked Dec 23, 2024 by msheppard (720 points)
0 votes
1 answer

I've got a custom task to run and check if the license count falls below a threshold, then send an email if the former returns true. The issue is that the email is ... count, return true if threshold below 100 Email code (with email redacted at the end):

asked Dec 18, 2024 by apruitt (140 points)
0 votes
1 answer

I have a number of custom Powershell commands that run during user onboarding, and while the Powershell script runs successfully, the Adaxes execution log for the command will ... Adaxes when this happens so the command doesn't show that it ran successfully?

asked Dec 16, 2024 by cwyant-hfg (40 points)
+1 vote
1 answer

feature request: Allow scheduled reports to have any email address as a recipient.

asked Nov 24, 2024 by PaulPCGuy7 (30 points)
3,628 questions
3,315 answers
8,392 comments
548,728 users