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

I'm wanting to modify the subject of the approval email that is sent to an approver so it stands out better. What I would like is to add the name of the user ... , %name% or anything else related to name is that of the person being emailed the approval.

asked Mar 14 by matt_nz (20 points)
0 votes
1 answer

I have 2 scheduled reports that are supposed to be emailed to a distribution group with 4 members. Only 2 of the members are receiving the email. I ran a Message Trace in ... to. Where should I start looking for the problem? Never mind. I figured it out.

asked Mar 12 by jmatthews (210 points)
0 votes
1 answer

We have 3 email domains. 1 primary and 2 subsidary companies. I'm needing to automate setting the email domain during user creation based on department. See attached ... to be able to automate this for ongoing maintenance if users move between departments.

asked Mar 7 by browndervilleb (20 points)
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 Feb 14 by cewilson (300 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 (790 points)
3,678 questions
3,361 answers
8,499 comments
549,338 users