I have this command which spits out a pdf file and you can see that it is saving in my C: drive:
$DownloadReportResponse = Invoke-WebRequest -Method Post -Uri $DownloadReportUrl -ContentType "application/json" -Headers $headers -Body $DownloadReportRequestBody -OutFile "C:\Neoload\TestResult-($DateForTestResult).pdf" -TransferEncoding "deflate"
Is there way to script this so it can send the file to certain email address as well?
I have this example script to send email, where would I assign the file so it is attached in the email?
###########Define Variables########
$fromaddress = "myGmail#gmail.com"
$toaddress = "myGmail#gmail.com"
#$CCaddress = "myGmail#gmail.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = $DownloadReportResponse
$smtpserver = "smtp.labtest.com"
####################################
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
You can use the build in Send-MailMessage command. Just make sure that the attachment is a path to your file
$EmailSettings=#{
SMTPServer="smtp.labtest.com"
From="myGmail#gmail.com"
To="myGmail#gmail.com"
Subject="ACtion Required"
Body=(get-content .\content.htm )
Attachments="C:\Neoload\TestResult-($DateForTestResult).pdf"
BodyAsHtml=$true
}
Send-MailMessage #EmailSettings
Related
How to change this script to attach 5 attachment and to send email :
Pls help in changing the script accordingly to accept more attachment and to send email
###########Define Variables########
$fromaddress = "donotreply#labtest.com"
$toaddress = "Aishwarya.Rawat#labtest.com"
$bccaddress = "Vikas.sukhija#labtest.com"
$CCaddress = "Mahesh.Sharma#labtest.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = "C:\sendemail\test.txt"
$smtpserver = "smtp.labtest.com"
####################################
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
According to your main question about modifying the code to attach multiple file
assuming that all your attachments exist under C:\sendemail\
we will modify the variable attachment to include all the files under C:\sendemail\ as follow
$attachment = (Get-ChildItem C:\sendemail\ -File).FullName
# or if you would like $attachment = #("C:\sendemail\file1.txt","C:\sendemail\file2.txt","C:\sendemail\file3.txt")
and then we will loop through the files of attachement to create Net.Mail.Attachment object and add it to the message like that:
foreach ($att in $attachment){
$attach = new-object Net.Mail.Attachment($att)
$message.Attachments.Add($attach)
}
so your final code will be something like that:
###########Define Variables########
$fromaddress = "donotreply#labtest.com"
$toaddress = "Aishwarya.Rawat#labtest.com"
$bccaddress = "Vikas.sukhija#labtest.com"
$CCaddress = "Mahesh.Sharma#labtest.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = (Get-ChildItem C:\sendemail\ -File).FullName
$smtpserver = "smtp.labtest.com"
####################################
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
foreach ($att in $attachment){
$attach = new-object Net.Mail.Attachment($att)
$message.Attachments.Add($attach)
}
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
If you are not limited to Powershell V1, you could use the Send-MailMessage command
Send-MailMessage -From $fromaddress -to $toaddress -Cc $CCaddress -Bcc $bccaddress -SmtpServer $smtpserver -Body $body -Subject $Subject
I've been using the below script to send email and sometimes it works, sometimes it does not. I'm not sure exactly why it is intermittent and with this error:
"ERROR: The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required"
This is the script I'm using:
$AWS_ACCESS_KEY = "KEYYYYYYYYYY"
$AWS_SECRET_KEY = "SECRET_KEYYYYYYYYYYYYYY"
$SECURE_KEY = $(ConvertTo-SecureString -AsPlainText -String $AWS_SECRET_KEY -Force)
$creds = $(New-Object System.Management.Automation.PSCredential ($AWS_ACCESS_KEY, $SECURE_KEY))
$smtp = "email-smtp.us-east-1.amazonaws.com"
$from = "noreply#domain.com"
$to = "to_you#domain.com"
$bcc = "its_me#domain.com"
$subject = "Hello There"
$body = "Hey Friend!<br><br>"
$body += "Body<br><br>"
$body += "Regards,<br>"
$body += "Bye<br>"
#Sending email
Sleep 2
Send-MailMessage -From $from -To $to -Bcc $bcc -Subject $subject -Body $body -BodyAsHtml -SmtpServer $smtp -Credential $creds -UseSsl -Port 587
What could be the issue? Has anyone seen this before? Any log in AWS console that I could look at?
Thanks!
I also faced a similar issue managed to resolve with the below codes.
$smtpServer = "email-smtp.us-east-1.amazonaws.com"
$smtpPort = 587
$username = "************"
$password = "***********************"
$from = "alerts#test.com"
$to = "recipient#domain.com"
$subject = "Amazon SES Test Email"
$body = "Body Amazon SES Test Email"
$cc = "xyz#domain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtp.EnableSsl = $true
$smtp.Credentials = new-object Net.NetworkCredential($username, $password)
$msg = new-object Net.Mail.MailMessage
$msg.From = $from
$msg.To.Add($to)
$msg.CC.Add($cc)
$msg.Subject = $subject
$smtp.Send($msg)
$msg.Body = $body
I want to send a CSV file in this e-mail.
I can send the email with no problem, but I tried a lot to send attachment with it, but at the end the e-mail was sent without the attachment.
function sendmail($Body) {
$Smtp = New-Object System.Net.Mail.SmtpClient
$MailMessage = New-Object System.Net.Mail.MailMessage
$Smtp.Host = "smtp-server"
$MailMessage.From = "sender#example.com"
$MailMessage.To.Add("recipient#example.org")
$MailMessage.Subject = "Hello"
$MailMessage.Body = $Body
$smtp.Port = 25
$smtp.EnableSsl = $false
$Smtp.Send($MailMessage)
}
$Body = "12334"
sendmail $Body
The e-mail just have to look like this:
Hello, all the information are in the file Example.csv
Send-MailMessage has a direct way of handling the attachments. Below is the sample.
Send-MailMessage -From "User01 <user01#example.com>" `
-To "User02 <user02#example.com>", `
"User03 <user03#example.com>" `
-Subject "Sending the Attachment" `
-Body "Forgot to send the attachment. Sending now." `
-Attachment "data.csv" -SmtpServer smtp.fabrikam.com
OR, you should use System.Net.Mail.MailMessage, then you have to use:
$EmailFrom = "<user#domain.tld>"
$EmailTo = "<user#domain.tld>"
$EmailSubject = "<email subject"
$SMTPServer = "smtphost.domain.tld"
$SMTPAuthUsername = "username"
$SMTPAuthPassword = "password"
$emailattachment = "<full path to attachment file.csv>"
function send_email {
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.From = ($emailfrom)
$mailmessage.To.Add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
$mailmessage.Attachments.Add($attachment)
#$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername", "$SMTPAuthPassword")
$SMTPClient.Send($mailmessage)
}
Refer THIS if required.
While trying to attach the .CSV file and run the code, i get the error as "Exception calling "Send" with "1" argument(s): "Unable to send to all recipients."
I have tried with .txt file ($attachment = "\path1\test.txt") and the same code is executed without any error (but I have not received any mail)
$date_value = Get-Date -format yyyy_MM_dd
$file_Name = 'test_'+ $date_value.ToString() + '.csv'
$attachment = "\\path1\test.CSV"
$smtpserver = "server.com"
$size=((Get-item $attachment ).length) -as [Int]
$from = "mail.com"
[string[]]$To= "xyz#xyz.com"
$CC="xyz#xyz.com"
$Subject = "Test"
$body=#"
Hi,
Test
Regards,
Team
"#
$message = new-object System.Net.Mail.MailMessage ( $From , $To )
$message.CC.Add($cc)
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
if ($size -gt 0){
$smtp.Send($message)
}
$attach.Dispose()
$message.Dispose()
$smtp.Dispose()
Expectation: .CSV file should get attached and send in the email
Have you tried the Send-MailMessage cmdlet with the -Attachment option? Its really simple.
Send-MailMessage -To $To -From $from -Cc $CC -Subject $Subject -Attachments $attachment -Body $body -SmtpServer $smtpserver
I am trying to write a powershell script that will be kicked off by a scheduled task, the script will return details of our database backups, save these to a .htm file and also email the results.
So far getting the backup info and saving to a .htm file works fine, but I can't figure out how to get the body of the email to look like the .htm file. The email body at the moment looks like the raw html code.
So far:
$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 10px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 10px;border-style: solid;border-color: black;}"
$a = $a + "</style>"
$date = ( get-date ).ToString('yyyyMMdd')
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST\SQLX64"
$dbs=$s.Databases
#Retrieves the last backup dates - both for FULL and LOG backups
$backups = $dbs | SELECT Name,LastBackupDate, LastLogBackupDate | ConvertTo-HTML -head $a -body "<H2>Database Backup Details $date </H2>" | Out-File $("D:\SQL Backup Log Script\Logs\"+ $date +"_DB01 Backup Log.htm")
$EmailFrom = "IT###.net"
$emailto = "#####.net"
$Subject = "DB01 SQL Backup Log"
$Body = Get-Content ("D:\SQL Backup Log Script\Logs\"+ $date +"_Backup Log.htm")
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("##", "##");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Thanks,
Charlotte.
collected all the answers in one piece:
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("##", "##");
$message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
$message.IsBodyHtml = $true;
$SMTPClient.Send($message)
Try using the Send-MailMessage cmdlet instead, which allows you to specify the -BodyAsHtml parameter:
Send-MailMessage `
-From $EmailFrom -To $EmailTo `
-Subject $Subject -Body $Body -BodyAsHtml $true `
-Credential $Credentials
Alternatively you will have to use the SmtpClient.Send(MailMessage) method instead and create a MailMessage object where you can set the MailMessage.IsBodyHtml property:
var client = New-Object System.Net.Mail.SmtpClient
# ...
var message = New-Object System.Net.Mail.MailMessage
message.IsBodyHtml = $true
# ...
$client.Send($message)
Try add this line before send() method:
$SMTPClient.isbodyhtml= $true
EDIT after comment:
You have to use this object:
$SMTPClient = New-Object System.Net.Mail.MailMessage