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

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

asked 9 hours ago by PaulPCGuy7 (20 points)
0 votes
1 answer

Hello, Was wondering if there is a script for a business rule or a property pattern that can be used for this request. I am wondering how can I do a check ... $Context.LogMessage("The email address '$emailAddress' is available.", "Information") return $false }

asked Nov 13 by KoleArmstrong (20 points)
0 votes
1 answer

Is there a way to get an email alert before the Adaxes lisense expires? Ex.: our license expires 13.09.2025 and would like an alert to be sent 14 days before this date.

asked Oct 18 by Handernye (100 points)
0 votes
1 answer

For service accounts or accounts where multiple people need to know the password is expiring, is there a way to use the password expiration task to notify the members ... to work and the password notification to work, just having problems connecting the two.

asked Oct 10 by ajmilic (100 points)
0 votes
1 answer

Hi all, I've created a 'View User' command via the Web Interface Configurator. I don't want users to be able to make any changes, so I've created a Custom ... there any way to remove this, short of removing the entire Exchange section itself? Thanks, David

asked Sep 5 by dshortall (80 points)
3,549 questions
3,240 answers
8,232 comments
547,814 users