[code language="powershell"] function sendMail { Write-Host “Sending Email” $Image = “Path1” $Image1 = “Path2” #Embed Image $att1 = new-object Net.Mail.Attachment($Image) $att1.ContentType.MediaType = “image/png” $att1.ContentId = “Attachment” $att2 = new-object Net.Mail.Attachment($Image1) $att2.ContentType.MediaType = “image/png” $att2.ContentId = “Attachment1” #Mail body $body = “<table style='width:100%'> <tr> <td align=left valign=top><img src='cid:Attachment' /></td> </tr> <tr> <td align=left valign=top>My Test<br> Directory: <a href='url'>Link</a><br><br></td> </tr> <tr> <td align=left valign=top>Email inviata $(Get-Date -format F) <br><br></td> </tr> <tr> <td align=left valign=top>Do-Not Reply<br></td> </tr> <tr> <td align=left valign=top><img src='cid:Attachment1' /></td> </tr> </table>” #SMTP server name $smtpServer = “server.smtp...” #Creating a Mail object $msg = new-object Net.Mail.MailMessage #Add attachment to the mail $msg.Attachments.Add($att1) $msg.Attachments.Add($att2) #Creating SMTP server object $smtp = new-object Net.Mail.SmtpClient($smtpServer) #Email structure $msg.From = “email@sender.ext“ $msg.ReplyTo = “reply@sender.ext“ $msg.To.Add(“emailto@sender.ext“) $msg.subject = “Subject Email” $msg.body = $body $msg.IsBodyHTML = $true #Sending email $smtp.Send($msg) } #Calling function sendMail exit [/code]
Lo script in Poweshell appena mostrato genera una email con queste caratteristiche:
- Testo Formattato in HTML, e tabella all’interno (risulta più ‘responsive’, anche se non lo è);
- 2 immagini allegate ed inserite nel corpo della email (si possono inserire N immagini, bisogna associare l’attributo ‘attX’ ‘ImageX’ e AttachmentX)
#Embed Image
$attx = new-object Net.Mail.Attachment($ImageX)
$attx.ContentType.MediaType = “image/png”
$attx.ContentId = “Attachment”; - Un link nel corpo dell’email (può essere una URL si un sito, o un link locale;
- La data cui è stata generata l’email (formattata giorno-mese-anno-ora-minuti-secondi);
- Attributi email: indirizzo del mittente, indirizzo per reply (può essere diverso dal mittente), indirizzo/i dei destinatari;
- Server smtp non autenticato, su porta 25 e sena cifratura.
Lo script può essere schedulato normalmente con lo scheduler di Windows, funziona con tutte le versioni di powershell (consigliata comunque sempre l’ultima).
Il consiglio è di non inserire ne’ troppe immagini e ne’ troppo grandi. Quindi rimanere con un numero inferiore a 5, con formati png e con una grandezza di non più di 10kB.