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
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 }
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 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 }
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
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
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
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
}
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
$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.
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?