Powershell send file as attachment of a mail

11,453

Send-Mailmessage accepts file paths to attach.

Send-MailMessage -SmtpServer xxxxxxxxxx -To [email protected] -From [email protected] -Subject "test mail" -Body "file name is: $new_file" -attachment $new_file
Share:
11,453
rschirin
Author by

rschirin

Updated on July 30, 2022

Comments

  • rschirin
    rschirin over 1 year

    I need to check if a file exist into a path.

    if yes, I need to send it as attachment of mail.

    this my code now:

    $file='STREAM_preg.txt'
    $pathfile ='c:\Users\root\Desktop'
    $dest ='C:\Users\root\Desktop\test'
    
    cd $pathfile
    if(Test-Path $file){
            $datestamp = get-date -uformat "%Y%m%d%H%M"
        Copy-Item $file $dest\$datestamp"_"$file
        $new_file = echo $datestamp"_"$file
        echo $new_file
          # Send-MailMessage -SmtpServer xxxxxxxxxx -To [email protected] -From [email protected] -Subject "test mail" -Body "file name is: $new_file"
    }else{
            echo false
    }
    
  • ArchGriffin
    ArchGriffin about 11 years
    Just for completeness: -Attachments string[] The path and file names of files to be attached to the email message. Use this parameter or pipe the path/file names.