Send Multiple attachments with varable items in email

Hello
I am tring to send multiple attachments with an email the has varibles generated in from a csv file to help with puting the persons name and date in the body of the email.
Here is what i have so far. Thanks

$PSEmailServer = ‘smtp-relay.gmail.com
Import-Csv E:\Scripts\Email-MOU\GET_EMAIL_MOUTEST.csv |`
foreach {$s = $.EMAIL;
$name = $
.FULL_NAME;
$DT = $_.DATE_TIME;
$file += “E:\Scripts\Email-MOU\Attachments*.pdf”

Send-MailMessage -From FromAddress@gmail.com
-Subject “Laptop Appointment Reminder with MOU” -To $s
-Attachments $file
-Body "Hello %user,
A new Laptop Pickup Appointment has been recorded for %DT .
Pleaser review the following MOU attachments prior to coming for your appointment.

Thank You!

Please DO NOT REPLY to this Message " `

    -Port 25 `
    -Priority Normal `
    -SmtpServer $PSEmailServer }

Barr,
Welcome to the forum. :wave:t4:

As you can read in the documentation for the parameter -Attachments there are no wildcard characters allowed. You should provide all files as an array.

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Thanks I am not getting any where with the following:

$PSEmailServer = 'smtp-relay.gmail.com'
Import-Csv E:\Scripts\Email-MOU\GET_EMAIL_MOUTEST.csv |`
   foreach {$s = $_.EMAIL;
            $name = $_.FULL_NAME;
            $DT = $_.DATE_TIME;
            [array]$attachments = Get-ChildItem "E:\Scripts\Email-MOU\Attachments";

 Send-MailMessage `
        -From FromAddress@gmail.com `
        -Subject "Laptop Appointment Reminder with MOU" `
        -To $s `
        -Attachments $attachments.fullname `    
        -Body "Hello %user,
A new Laptop Pickup Appointment has been recorded for %DT .
Please bring in your current Laptop, power cord that goes with the laptop, and BE SURE TO BACKUP ALL YOUR DATA prior to coming.
Pleaser review the following MOU attachments prior to coming for your appointment.
Thank You!
***Please DO NOT REPLY to this Message*** " `
        -Port 25 `
        -Priority Normal `
        -SmtpServer $PSEmailServer }

What’s in your [array]$attachments when you output it to the console? It should contain only all fullnames of the files you want to attach.

You have a typo in there:

[array]$attachments
-Attachments $attachemts.fullname ` 

i get the following error: I updated the orginal code above. Thanks

Send-MailMessage : Cannot validate argument on parameter 'Attachments'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At E:\Scripts\Email-MOU\Email_w_MOU.ps1:16 char:22
+         -Attachments $attachmets.fullname `
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage

The 2 files are in the following dirrectory
E:\Scripts\Email-MOU\Attachments

As Olaf stated, you need the full path. Try changing this:

Get-ChildItem "E:\Scripts\Email-MOU\Attachments"

To this:

(Get-ChildItem "E:\Scripts\Email-MOU\Attachments").FullName

thanks i get this now when i make that change.

Send-MailMessage : Cannot validate argument on parameter 'Attachments'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At E:\Scripts\Email-MOU\Email_w_MOU.ps1:17 char:22
+         -Attachments $attachments.fullname `
+                      ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage

O M G.

I’d recommend to do a big step back and start with learning the very basics of PowerShell first. It will save you from a lot of wasted time and frustrations.

$SendMailMessageSplat = @{
    From        = 'FromAddress@gmail.com'
    Subject     = "Laptop Appointment Reminder with MOU"
    To          = $s
    Attachments = (Get-ChildItem "E:\Scripts\Email-MOU\Attachments").FullName
    Body       = $Body
    Port        = 25
    Priority    = 'Normal'
    SmtpServer  = $PSEmailServer
}

Send-MailMessage @SendMailMessageSplat

Please read about splatting!

And please read about best practice and style. It is considered very bad style to use backticks as line continuation characters

1 Like

Here is what am tring that does prompts me for every email item listed.
from, subject, to etc… Just I am tring to insert varibles in the email body from the csv file as well.
Thanks

$PSEmailServer = 'smtp-relay.gmail.com'
Import-Csv E:\Scripts\Email-MOU\GET_EMAIL_MOUTEST.csv |`
   foreach {$s = $_.EMAIL;
            $name = $_.FULL_NAME;
            $DT = $_.DATE_TIME;
        $Body = "Hello %user,
A new Laptop Pickup Appointment has been recorded for %DT .
Please review the following MOU attachments prior to coming for your appointment.

Thank You!
***Please DO NOT REPLY to this Message*** " ; 
           

$SendMailMessageSpalt = @{
    From        = 'Fromeaddress@gmail.com'
    Subject     = "Laptop Appointment Reminder with MOU"
    To          = $s
    Attachments = (Get-ChildItem "E:\Scripts\Email-MOU\Attachments").FullName
    $Body       = $Body
    Port        = 25
    Priority    = 'Normal'
    SmtpServer  = $PSEmailServer
}

Send-MailMessage @$SendMailMessageSpalt
}

Typo in @Olaf’s code; it should be:

Send-MailMessage @SendMailMessageSpalt

It should probably be @SendMailMessageSplat too but as that’s wrong in both places, it will still work :rofl:

You got me. :wink: Thank’s for checking. :+1:t4: :wink:

changed the code accordingly … :white_check_mark:

1 Like

here is the error i get when i use the full code below:

$PSEmailServer = 'smtp-relay.gmail.com'
Import-Csv E:\Scripts\Email-MOU\GET_EMAIL_MOUTEST.csv |`
   foreach { $s = $_.EMAIL;
                 $name = $_.FULL_NAME;
                 $DT = $_.DATE_TIME;
        $Body = "Hello %user,
A new Laptop Pickup Appointment has been recorded for %DT .
Please review the following MOU attachments prior to coming for your appointment.

Thank You!
***Please DO NOT REPLY to this Message*** " ; 
          

@SendMailMessageSpalt = @{
    From        = 'FromAddress@gmail.com'
    Subject     = "Laptop Appointment Reminder with MOU"
    To          = $s
    Attachments = (Get-ChildItem "E:\Scripts\Email-MOU\Attachments").FullName
    $Body       = $Body
    Port        = 25
    Priority    = 'Normal'
    SmtpServer  = $PSEmailServer
}

Send-MailMessage @SendMailMessageSpalt
}
At line:23 char:1
+ @SendMailMessageSpalt = @{
+ ~~~~~~~~~~~~~~~~~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@SendMailMessageSpalt' can be used only as an argument to a command. To reference variables in an 
expression use '$SendMailMessageSpalt'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : SplattingNotPermitted
 

The error message tells you exactly what’s wrong. You used @SendMailMessageSpalt instead of $SendMailMessageSpalt.

Ok i changed the bottom part to this:


$SendMailMessageSpalt = @{
    From        = 'FromAddress@gmai.com'
    Subject     = "Laptop Appointment Reminder with MOU"
    To          = $s
    Attachments = (Get-ChildItem "E:\Scripts\Email-MOU\Attachments").FullName
    $Body       = $Body
    Port        = 25
    Priority    = 'Normal'
    SmtpServer  = $PSEmailServer
}

Send-MailMessage @SendMailMessageSpalt
}

And i get this error:

Send-MailMessage : A parameter cannot be found that matches parameter name 'Hello %user,
A new Laptop Pickup Appointment has been recorded for %DT .
Pleaee bring in your current Laptop, power cord that goes with the laptop, and BE SURE TO BACKUP ALL YOUR DATA prior to coming to the Technonlogy Center.  
You will need your current laptop in order to receive a new laptop.  Please review the following MOU attachments prior to coming for your appointment.
Thank You!
Technology Services
***Please DO NOT REPLY to this Message*** '.
At line:34 char:18
+ Send-MailMessage @SendMailMessageSpalt
+                  ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SendMailMessage

This will be my last answer in this thread. You should urgently learn how to debug your own code and how to read error messages. Please learn the fundation of PowerShell.

    $Body       = $Body

Should be:

    Body       = $Body

Thanks for your help
Now i get
Send-MailMessage : Mailbox unavailable. The server response was: 5.7.1 Invalid credentials for relay

I use the same relay and the same port in another code that sends 1 file in an email but it does not use spalt. I do not get this error. So something is strange here.
Any idea?

It is working now. The email is working now. Thanks for you help…