I am trying to use the send-mailmessage in powershell by passing through data using variables. I have cut down the script to simplest form so i can get it to work however i get errors " The specified string is not in the form required for an e-mail address"
Any thoughts please ?
$email = ‘xxx.xxxx@lxxxxxx.com’
$smtpServer =‘mail.us.xxxx.com’
$subject= ‘subject’
$data = ‘This is a test’
Send-MailMessage To $email -subject $subject -Body $data -From $email -SmtpServer $smtpServer
To make your code easier to read, easier to understand and easier to maintain you should consider using splatting:
$SplatProps = @{
From = ‘sender@domain.com’
to = ‘recipient@otherdomain.com’
SmtpServer = ‘mail.us.xxxx.com’
subject = ‘subject’
Body = ‘This is a test’
}
[/quote]
Thanks Olaf I like the concept however I am still learning powershell at 60 years old so its hard to ingrain all these concepts. However will take alook.
I have another question but its related to my original so it makes sense for me to ask it here rather then starting a new thread.
When using the $body variable how does one start a new line. For example below i want to start a new line and display the users credentials. However it continues to stay on same line even if you use the $Body+= variable. What I want to achieve is to have new lines not have everything on one line
$Body = “Find below your logon details”
$Body += “Logon:$logon”
Does anyone know how to ensure that a new line is used in the mail ?