# Mail from powershell

**URL:** <https://forums.powershell.org/t/mail-from-powershell/17794>\
**Category:** PowerShell Help\
**Created:** [October 20, 2021, 2:37pm UTC](https://forums.powershell.org/t/mail-from-powershell/17794 "2021-10-20T14:37:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![skoeld](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@skoeld](https://forums.powershell.org/u/skoeld)\
**Post date:** [October 20, 2021, 2:37pm UTC](https://forums.powershell.org/t/mail-from-powershell/17794/1 "2021-10-20T14:37:18Z")

</div>

Works with a script that will find files and then email them to different users.  
This works with the exception that when I want to use a csv file containing several servers, only one email from one server is sent to the others and I can not find where I am doing wrong.  
Someone who could be kind and help me on the right path?

```auto
#Import data from file
$servers = Import-Csv -Path C:\Scripts\Program\Servers.csv -Delimiter ";" -Encoding UTF8

foreach ($server in $servers) {
Invoke-Command -ComputerName $server.Name {

Set-Location \\$env:COMPUTERNAME\d$\Integrations

$SMTPServer = 'smtp.mygroup.com'
$EmailFrom = "test@mygroup.com"
$files = Get-ChildItem -Path .\ -Recurse -Filter *.xml -Force

$ExpiredBody = @(
    $files |
    Where-Object {$_.Directory -like "*Error*"} |
            Select-Object PsPath, LastWriteTime |
        Sort-Object PsPath
)

$head = @'
<style>
    TABLE{width: 100%;border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
    TH{border-width: 1px;padding: 1px;border-style: solid;border-color: black;}
    TD{border-width: 1px;padding: 1px;border-style: solid;border-color: black;}
</style>
'@

$ExtendExpiredBody = $ExpiredBody | ConvertTo-HTML -Head $head

$EmailExpiredBody = "Listed below are files that are in a state of Error $($env:COMPUTERNAME)" 
if ($ExpiredBody) {
    $EmailExpiredBody = "<br> Listed below are Error files that have been reported $($env:COMPUTERNAME)" 

$EmailBody = $EmailExpiredBody + $ExtendExpiredBody
$EmailBody = $EmailBody + ($env:userdomain, $env:username, $env:COMPUTERNAME) 
$EmailSubject = "Files that is reporting error in computer $($env:COMPUTERNAME)"
}
if ($ExpiredBody.Count -ge 1) {
    $SendMailMessageProps = @{
        From = $EmailFrom
        To = $server.mejl
        Subject = $EmailSubject
        Body = $EmailBody
        BodyAsHtml = $true
        SmtpServer = $SMTPServer
    }
    Send-MailMessage @SendMailMessageProps
}
}
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 20, 2021, 2:47pm UTC](https://forums.powershell.org/t/mail-from-powershell/17794/2 "2021-10-20T14:47:45Z")

</div>

With

```auto
Set-Location \\$env:COMPUTERNAME\d$\Integrations

```

you set the current working directory to the drive D: on the local computer this script runs on. You may change this to

```auto
Set-Location \\$($server.Name)\d$\Integrations

```

A better option would be to provide the desired path to the according cmdlets like `Get-ChildItem` and not working with the working directory. 😉

---

<div class="post-metadata">

**Author:** ![skoeld](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@skoeld](https://forums.powershell.org/u/skoeld)\
**Post date:** [October 20, 2021, 4:07pm UTC](https://forums.powershell.org/t/mail-from-powershell/17794/3 "2021-10-20T16:07:18Z")

</div>

Hm I have tried that with sam result.  
Still got mail from one server and if I move the two last brackets to an uper location then I got one mail but only one not from all the servers in the csv file.

Totaly stuck with this I have been reading this so much so I got blind 😂

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 20, 2021, 5:07pm UTC](https://forums.powershell.org/t/mail-from-powershell/17794/4 "2021-10-20T17:07:18Z")

</div>

Hmmm … if I got everything right this should be enough:

```auto
$serverList = Import-Csv -Path C:\Scripts\Program\Servers.csv -Delimiter ';' -Encoding UTF8
$SMTPServer = 'smtp.mygroup.com'
$EmailFrom = 'test@mygroup.com'
$head = @'
<style>
    TABLE{width: 100%;border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
    TH{border-width: 1px;padding: 1px;border-style: solid;border-color: black;}
    TD{border-width: 1px;padding: 1px;border-style: solid;border-color: black;}
</style>
'@

foreach ($server in $serverList) {
    $Result = 
    Invoke-Command -ComputerName $($server.Name) {
        Get-ChildItem -Path 'd:\Integrations' -Filter *.xml -Recurse -Force |
        Where-Object { $_.Directory -like "*Error*" } 
    }
    if ($Result) {
        $ExtendExpiredBody = @(
            $Result | 
                Select-Object -Property PSComputerName, LastWriteTime, Fullname | 
                    Sort-Object -Property FullName | 
                        ConvertTo-HTML -Head $head
        )
        $EmailExpiredBody = "<br> Listed below are Error files that have been reported <br>" 
        $EmailBody = $EmailExpiredBody + $ExtendExpiredBody

        $SendMailMessageProps = @{
            From = $EmailFrom
            To = $server.mejl
            Subject = "Files that is reporting error in computer $($Result[0].PSComputerName)"
            Body = $EmailBody
            BodyAsHtml = $true
            SmtpServer = $SMTPServer
        }
        Send-MailMessage @SendMailMessageProps
    }
}

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 20, 2021, 5:14pm UTC](https://forums.powershell.org/t/mail-from-powershell/17794/5 "2021-10-20T17:14:54Z")

</div>

I wonder if this is the double-hop problem?

You can’t authenticate against the mail server, and the servers that aren’t working are not in the list of servers that are allowed to relay.

@Olaf’s solution to move the sending of the mail to the machine you’re running the script from is what I would do too.

---

<div class="post-metadata">

**Author:** ![skoeld](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@skoeld](https://forums.powershell.org/u/skoeld)\
**Post date:** [October 20, 2021, 5:32pm UTC](https://forums.powershell.org/t/mail-from-powershell/17794/6 "2021-10-20T17:32:07Z")

</div>

This seems good.  
I can see the gain in reorder sending mail. I will remote and try this right away and the I get back here.

---

<div class="post-metadata">

**Author:** ![skoeld](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@skoeld](https://forums.powershell.org/u/skoeld)\
**Post date:** [October 20, 2021, 5:41pm UTC](https://forums.powershell.org/t/mail-from-powershell/17794/7 "2021-10-20T17:41:39Z")

</div>

After a minor adjustment, it works almost as intended. The information is coming out as it should now. Just need to arrange so that the sender name is also correct with the server name.  
Many thanks for the help.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:20pm UTC](https://forums.powershell.org/t/mail-from-powershell/17794/8 "2024-05-16T20:20:37Z")

</div>

Closed on May 16, 2024, 8:20pm UTC


