Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Trying to send an email to multiple recipients.

$
0
0

I've been trying to do a very basic task: take in a bunch of space separated names from one of the csv columns i'm ingesting elsewhere, and send them an email. Basic right? Well, whenever i try i end up with either only one recipient, or this error;

----------

Send-MailMessage : An invalid character was found in the mail header: ','.

At \\share\tool.ps1:106 char:9

+         Send-MailMessage -To $recipients -Cc $CCrecipients -From

$senderemail -S ...

----------

 

I basically have two functions, one to actually send the mail, and the other to set it up based on values sent from a different part. I have a few different attempts commented, and i've basically narrowed it down to whatever is going to TO.

The entry obtained from the CSV looks like this with no extra spaces, but a dynamic number of aliases "alias1 alias2 alias3 alias4"

What am I doing wrong? It seems so simple, but i'm definitely messing something up.

 

Function Send-Email([String] $smtpServer, [String[]] $recipients, [String[]]$CCrecipients, [String] $senderemail, [String] $subject, [String] $body)

{

#DEBUG

write-host value of recipients is ($recipients)

pause

        Send-MailMessage -To $recipients -Cc $CCrecipients -From $senderemail -Subject $subject  -Body $body  -SmtpServer $smtpServer -BodyAsHtml    

}

 

Function prepareEmail {

param ([string]$_Alias, [string]GlobalAlias)

[String[]]$_tempalias= @()

 

#$_Alias.TrimStart(" ")

$_Alias.Split(" ") | Foreach {

 

#Debugging

write-host inside ($_) 

 

$_tempalias += "$_@MyCompany.com, "

#Others I've tried

#$_tempalias += "$([char]34)<$_@MyCompany.com>$([char]34), "

#$_tempalias += "$([char]34)$_@MyCompany.com$([char]34), "

}

#$_tempalias = $_tempalias.TrimEnd(" ")

#$_tempalias = $_tempalias.TrimEnd(",")

#$_tempalias = $_tempalias.TrimStart(" ")

$_tempalias[1] = ($_tempalias[1]).TrimStart(" ")

$_tempalias[-1]= ($_tempalias[-1]).trimend(" ")

$_tempalias[-1]= ($_tempalias[-1]).trimend(",")

Write-Host last element is ($_tempalias[-1])

write-host tempalias should be ($_tempalias)

write-host ($_tempalias.GetType())

pause

$body =  "<b><font color=red>THIS IS A TEST</b></font> <br>"

 

$_from = "admin@mycompany.com"

$_smtp = "OURSMTPSERVER"

$_subject = "test email "

 

 

    Send-Email -smtpServer $_smtp -recipients $_tempalias -CCrecipients $GlobalAlias -senderemail $_from -subject $_subject -body $body

}

Viewing all articles
Browse latest Browse all 6937

Trending Articles