Avatar of cawasaki
cawasaki
 asked on

send-mailmessage with big body text

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
Email ProtocolsExchangePowershellScripting Languages

Avatar of undefined
Last Comment
cawasaki

8/22/2022 - Mon
cawasaki

ASKER
and in the script i have a for loop to select every Active directory user, so the email message must integrate the for loop
Joseph Daly

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
cawasaki

ASKER
no, i have a body with 10 line!

in the text body i need to use $samaccountname value!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Joseph Daly

And that variable is in the body

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

ASKER
no and no :)

where is my body text in 10 lines????
Joseph Daly

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
cawasaki

ASKER
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

cawasaki

ASKER
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
cawasaki

ASKER
any helpplz?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Ciprian Lozonschi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
cawasaki

ASKER
thanks its perfect