A
A
Alina Andreeva2017-10-02 15:09:30
PowerShell
Alina Andreeva, 2017-10-02 15:09:30

Email automation - what can be improved?

Good afternoon! There is a task that the letter was sent on behalf of the general mailbox - the script should take attachments from the folder, set the date a day earlier and send it automatically. It turned out like this for me, but I can’t overcome 2 points in any way - how to make an attachment of all files from a folder without listing them by name and how to make the date correct a day ago without minutes in the context of this task. Do not judge strictly, I'm a beginner, I use Google... #Incoming
message data:
$From = "[email protected]"
$To = "[email protected]"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587 "
$Username = "[email protected]"
$Password = "12345555"
$subject = "Summary {0}" -f $dateBegin
$body = "Regional Report for {0}"
$dateBegin = ((get-date).adddays(-1).toshortdatestring()) #forming
a message in html format:
$message = New-Object System.Net.Mail.MailMessage $From, $To
$message.Subject = $subject
$message.IsBodyHTML = $true
$message.Body = $body
$file = "C:\12\30.09.2017\30.09.2017.csv"
#Create two instances of class
$att = New-object Net.Mail. Attachment($file)
$mes = New-Object System.Net.Mail.MailMessage
$mes.Attachments.Add($att) #Sending
:
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort )
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$smtp.Send($message)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Ethril, 2017-10-02
@Ethril

how to make date correct one day ago without minutes

(get-date).adddays(-1).Date.toshortdatestring()
$mes = New-Object System.Net.Mail.MailMessage
(Get-ChildItem -Path 'C:\MyFolder').FullName | foreach { $mes.Attachments.Add( New-object Net.Mail.Attachment($_) ) }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question