send-mailmessage with big body text

cawasaki
cawasaki used Ask the Experts™
on
hi,

i need to send an email with send-maimessage command.

the body is in 10 lines.

my script get a user name value=$samaccountname, i need in my body text to use this value.


for exemple in line 1:  the user $samaccountname is here.....
.............
...........
.............
...........

are this possible?

thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
and in the script i have a for loop to select every Active directory user, so the email message must integrate the for loop
Yes you should be able to do that.


Send-MailMessage -to "user@email.com" -Subject "Subject" -from sender@email.com -body "$samaccountname" -smtpserver mailserver.com

Author

Commented:
no, i have a body with 10 line!

in the text body i need to use $samaccountname value!
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

And that variable is in the body

Send-MailMessage -to "user@email.com" -Subject "Subject" -from sender@email.com -body "$samaccountname" -smtpserver mailserver.com

Author

Commented:
no and no :)

where is my body text in 10 lines????
maybe if you actually tried explaining what you were trying to do we could figure out what the code needs to be.

But good luck getting other experts to help you when you have such a bad attitude toward those that try to assist.

Good luck buddy.

Author

Commented:
sorry, its not a bad attitude but my bad english!

here what i need to do with my script:


$datediff = 10

$reportPath = "c:\expire.csv"
$DatePast = (get-date).AddDays($dateDiff)

#Email

$Body = @"

<b>The account $displayname (dont vous êtes le responsable) will expire in $expiration .</b>
<br>
<br>
You need to contact service desk if you still need this account :
<br>
<br>
                - in this address www.test.com
<br>
<br>
                - or by phone in 060606060606
<br>
<br>

blablabalbalablabalablablabalabla..................

"@


$FromAddress = "test@test.com" 
$SMTPServer = "10.10.10.10"



$Expiring = Get-QADUser -AccountExpiresBefore $datePast -AccountExpiresAfter (Get-Date) -sizelimit 0
foreach ($Acct in $expiring) {
	
		$expiration = $acct.accountexpires.tostring()
				if ($acct.Manager -ne $null) {
			$recipients = (Get-QADUser $acct.Manager).mail
			$displayname= $acct.displayname
		
		
		Send-MailMessage -Subject "Account Expiring" -To $recipients -From $FromAddress -SmtpServer $SMTPServer -Body $Body
	} 

else {
		#Assumes file path is valid and file exits.
		$acct.Name +","+$expiration | Out-File -FilePath $reportPath -Append 
	}
}
                                            

Open in new window

Author

Commented:
the problem in this script is when i receive the email, the value $displayname and $expiration in body text is not good, it not get the value in powerhsell script.

thanks

Author

Commented:
any helpplz?
Ops Team Office 365/Messaging
Commented:
Hi, bellow code should be what you are looking for.

$DateDiff = 10
$DatePast = (Get-Date).AddDays($DateDiff)
$ReportPath = "c:\expire.csv"
$FromAddress = "test@test.com"
$SMTPServer = "10.10.10.10"

$expiring = Get-QADUser -AccountExpiresBefore $DatePast -AccountExpiresAfter (Get-Date) -SizeLimit 0
foreach ($acct in $expiring) {
	$expiration = $acct.AccountExpires.ToString()
	if ($acct.Manager -ne $null) {
		$recipients = (Get-QADUser $acct.Manager).mail
		$displayname = $acct.DisplayName		
		#Email body
$body = @"
The account <b>$displayname</b> (dont vous etes le responsable) will expire at $expiration.<br><br>
You need to contact Service Desk if you still need this account:<br><br>
	- in this address: www.test.com<br><br>
	- or by phone in 060606060606<br><br>
IT Team
"@
		Send-MailMessage -Subject "Account Expiring" -To $recipients -From $FromAddress -SmtpServer $SMTPServer -Body $body -BodyAsHtml -Priority High
	} 
else {
	#Assumes file path is valid and file exits.
	$acct.Name + "," + $expiration | Out-File -FilePath $reportPath -Append
	}
}

Open in new window

Author

Commented:
thanks its perfect

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial