locked
error with variables using Send-MailMessage to send bulk emails RRS feed

  • Question

  • hi,

    i'm trying to send email with powershell, but to multiple contacts and every email has to be personalised (name, email, password)

    i was able to get this version (with no variables), for one email address working with no problems

    $credential = Get-Credential
    $Body = @"
    <html>
    	<body bgcolor=white style="topmargin=0;">
    		<div align=center style="margin- margin-right:auto;">
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FBFBFB; margin- margin-right:auto; width:100%;border:0;align:center;">
    				<tr>
    					<td style="padding:15px; text-align:center; width: 100%;border:0;"><img src="cid:logo.png"></td>
    				<tr>
    			</table>
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FBFBFB; margin- margin-right:auto; width:100%;border:0;">
    				<tr>
    					<td style="width:35px;border:0;"></td>
    					<td style="background:#ffffff; height:315px;border- border- border-right: 2px solid WhiteSmoke; border-bottom: 2px solid WhiteSmoke; text-align:left; padding:48px;"align=center>
    						<p style="line-height:33.0pt; font-family:Calibri;max-width:480pt;">
    							<b>
    								<span style="font-size:28.5pt;">Bonjour name votre compte vient d&#39;&ecirc;tre cr&egrave;&egrave;</span>
    							</b>
    						</p>
    						<p style="line-height:33.0pt font-size:11.0pt;font-family:Calibri; margin- max-width:480pt;">
    							<span>
    								Nom d&#39;utilisateur: <b>username</b><br>
    								Mot de passe temporaire: <b>password</b> 
    							</span>
    						</p>
    						<p style="line-height:33.0pt font-size:11.0pt;font-family:Calibri; margin- max-width:480pt; margin-bottom:40pt;">
    							<span>La présidence de votre BREI a dû recevoir une formation sur les différents outils à vous transmettre.</span>
    						</p>
    						<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FFFFFF; margin- margin-right:auto; width:100%; border:0;">
    							<tr>
    								<td style="width:2%;border:0;"></td>
    								<td style="width:46%; border:0;text-align:center;">
    									<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;background:#D83B01;">
    										<tr style="border:0; ">
    											<td style="width:5%;border:0;"></td>
    											<td style="border-bottom:10px solid #D83B01;border-text-align:center;border-border-right:0;">
    												<a href="https://www.office.com/?auth=2" target="_blank" style="text-decoration: none;">
    													<strong>
    														<span style='font-family:"Calibri",sans-serif;color:white;font-weight:normal;'>Connexion à Office 365</span>
    													</strong>
    												</a>
    											</td>
    											<td style="width:5%;border:0;"></td>
    										</tr>
    									</table>						
    								</td>
    								<td style="width:4%;border:0;"></td>
    								<td style="width:46%; border:0;text-align:center;">
    									<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;background:#5359af;">
    										<tr style="border:0; ">
    											<td style="width:5%;border:0;"></td>
    											<td style="border-bottom:10px solid #5359af;border-text-align:center;border-border-right:0;">
    												<a href="https://teams.microsoft.com/downloads" target="_blank" style="text-decoration: none;">
    													<strong>
    														<span style='font-family:"Calibri",sans-serif;color:white;font-weight:normal;'>Télécharger Teams</span>
    													</strong>
    												</a>
    											</td>
    											<td style="width:5%;border:0;"></td>
    										</tr>
    									</table>							
    								</td>
    								<td style="width:2%;border:0;"></td>
    							</tr>
    						</table>
    					</td>
    					<td style="width:35px;border:0;"></td>
    				<tr>
    			</table>
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;max-width:480pt;background:#FBFBFB; margin- margin-right:auto; width:100%; height:35px;">
    				<tr>
    					<td style="border:0;"></td>
    				<tr>
    			</table>	
    		</div>			
    	</body>
    </html>
    "@
    Send-MailMessage -To "user@hotmail.com" -from "sender@bnei.fr" -SMTP "smtp.office365.com" -port "587" -UseSsl -Credential $credential  -Subject "test email" -BodyAsHtml -body $body -Attachments "E:\tmp\file\logo.png"

    However when i import the CSV :

    • the variable in th body will not get replaced
    • and i'll get the folowing error (but i will still recieve the emails)
    Send-MailMessage : Cannot validate argument on parameter 'To'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    At line:79 char:106
    + ... rs\test.csv" | foreach {Send-MailMessage -To $_.StreetAddress -from " ...
    +                                                  ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage

    Here is a copy of the script for multiple users and the variables

    $credential = Get-Credential
    $Body = @"
    <html>
    	<body bgcolor=white style="topmargin=0;">
    		<div align=center style="margin- margin-right:auto;">
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FBFBFB; margin- margin-right:auto; width:100%;border:0;align:center;">
    				<tr>
    					<td style="padding:15px; text-align:center; width: 100%;border:0;"><img src="cid:logo.png"></td>
    				<tr>
    			</table>
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FBFBFB; margin- margin-right:auto; width:100%;border:0;">
    				<tr>
    					<td style="width:35px;border:0;"></td>
    					<td style="background:#ffffff; height:315px;border- border- border-right: 2px solid WhiteSmoke; border-bottom: 2px solid WhiteSmoke; text-align:left; padding:48px;"align=center>
    						<p style="line-height:33.0pt; font-family:Calibri;max-width:480pt;">
    							<b>
    								<span style="font-size:28.5pt;">Bonjour $_.DisplayName votre compte vient d&#39;&ecirc;tre cr&egrave;&egrave;</span>
    							</b>
    						</p>
    						<p style="line-height:33.0pt font-size:11.0pt;font-family:Calibri; margin- max-width:480pt;">
    							<span>
    								Nom d&#39;utilisateur: <b>$_.SignInName</b><br>
    								Mot de passe temporaire: <b>$_.Password</b> 
    							</span>
    						</p>
    						<p style="line-height:33.0pt font-size:11.0pt;font-family:Calibri; margin- max-width:480pt; margin-bottom:40pt;">
    							<span>La présidence de votre BREI a dû recevoir une formation sur les différents outils à vous transmettre.</span>
    						</p>
    						<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FFFFFF; margin- margin-right:auto; width:100%; border:0;">
    							<tr>
    								<td style="width:2%;border:0;"></td>
    								<td style="width:46%; border:0;text-align:center;">
    									<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;background:#D83B01;">
    										<tr style="border:0; ">
    											<td style="width:5%;border:0;"></td>
    											<td style="border-bottom:10px solid #D83B01;border-text-align:center;border-border-right:0;">
    												<a href="https://www.office.com/?auth=2" target="_blank" style="text-decoration: none;">
    													<strong>
    														<span style='font-family:"Calibri",sans-serif;color:white;font-weight:normal;'>Connexion à Office 365</span>
    													</strong>
    												</a>
    											</td>
    											<td style="width:5%;border:0;"></td>
    										</tr>
    									</table>						
    								</td>
    								<td style="width:4%;border:0;"></td>
    								<td style="width:46%; border:0;text-align:center;">
    									<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;background:#5359af;">
    										<tr style="border:0; ">
    											<td style="width:5%;border:0;"></td>
    											<td style="border-bottom:10px solid #5359af;border-text-align:center;border-border-right:0;">
    												<a href="https://teams.microsoft.com/downloads" target="_blank" style="text-decoration: none;">
    													<strong>
    														<span style='font-family:"Calibri",sans-serif;color:white;font-weight:normal;'>Télécharger Teams</span>
    													</strong>
    												</a>
    											</td>
    											<td style="width:5%;border:0;"></td>
    										</tr>
    									</table>							
    								</td>
    								<td style="width:2%;border:0;"></td>
    							</tr>
    						</table>
    					</td>
    					<td style="width:35px;border:0;"></td>
    				<tr>
    			</table>
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;max-width:480pt;background:#FBFBFB; margin- margin-right:auto; width:100%; height:35px;">
    				<tr>
    					<td style="border:0;"></td>
    				<tr>
    			</table>	
    		</div>			
    	</body>
    </html>
    "@
    Import-Csv -Path "E:\tmp\file\test.csv" | foreach {
    Send-MailMessage -To $_.StreetAddress -from "user@bnei.fr" -SMTP "smtp.office365.com" -port "587" -UseSsl -Credential $credential  -Subject "test email" -BodyAsHtml -body $body -Attachments "E:\tmp\bureau national des élèves ingénieurs\logo.png"
    Write-Output "sending email $_.DisplayName"
    } | Export-Csv -Path "E:\tmp\bureau national des élèves ingénieurs\NewAccountResults.csv"

    here is a copy (anonymised) of the CSV that i'm importing (it is the export from new-msoluser)

    Password,ExtensionData,AlternateEmailAddresses,AlternateMobilePhones,AlternativeSecurityIds,BlockCredential,City,CloudExchangeRecipientDisplayType,Country,Department,DirSyncProvisioningErrors,DisplayName,Errors,Fax,FirstName,ImmutableId,IndirectLicenseErrors,IsBlackberryUser,IsLicensed,LastDirSyncTime,LastName,LastPasswordChangeTimestamp,LicenseAssignmentDetails,LicenseReconciliationNeeded,Licenses,LiveId,MSExchRecipientTypeDetails,MSRtcSipDeploymentLocator,MSRtcSipPrimaryUserAddress,MobilePhone,ObjectId,Office,OverallProvisioningStatus,PasswordNeverExpires,PasswordResetNotRequiredDuringActivate,PhoneNumber,PortalSettings,PostalCode,PreferredDataLocation,PreferredLanguage,ProxyAddresses,ReleaseTrack,ServiceInformation,SignInName,SoftDeletionTimestamp,State,StreetAddress,StrongAuthenticationMethods,StrongAuthenticationPhoneAppDetails,StrongAuthenticationProofupTime,StrongAuthenticationRequirements,StrongAuthenticationUserDetails,StrongPasswordRequired,StsRefreshTokensValidFrom,Title,UsageLocation,UserLandingPageIdentifierForO365Shell,UserPrincipalName,UserThemeIdentifierForO365Shell,UserType,ValidationStatus,WhenCreated
    password1,System.Runtime.Serialization.ExtensionDataObject,System.Collections.Generic.List`1[System.String],System.Collections.Generic.List`1[System.String],,FALSE,,,,department1,,person1,,,firstname1,,,FALSE,FALSE,,surname1,24/03/2020 21:36,,FALSE,,,,,,,,,None,FALSE,,,,,,,,,,user1,,,email1,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationMethod],System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationPhoneAppDetail],,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationRequirement],,TRUE,24/03/2020 21:36,,FR,,user1,,Member,Healthy,
    password2,System.Runtime.Serialization.ExtensionDataObject,System.Collections.Generic.List`1[System.String],System.Collections.Generic.List`1[System.String],,FALSE,,,,department2,,person2,,,firstname2,,,FALSE,FALSE,,surname2,24/03/2020 21:36,,FALSE,,,,,,,,,None,FALSE,,,,,,,,,,user2,,,email2,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationMethod],System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationPhoneAppDetail],,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationRequirement],,TRUE,24/03/2020 21:36,,FR,,user2,,Member,Healthy,
    password3,System.Runtime.Serialization.ExtensionDataObject,System.Collections.Generic.List`1[System.String],System.Collections.Generic.List`1[System.String],,FALSE,,,,department3,,person3,,,firstname3,,,FALSE,FALSE,,surname3,24/03/2020 21:36,,FALSE,,,,,,,,,None,FALSE,,,,,,,,,,user3,,,email3,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationMethod],System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationPhoneAppDetail],,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationRequirement],,TRUE,24/03/2020 21:36,,FR,,user3,,Member,Healthy,
    password4,System.Runtime.Serialization.ExtensionDataObject,System.Collections.Generic.List`1[System.String],System.Collections.Generic.List`1[System.String],,FALSE,,,,department4,,person4,,,firstname4,,,FALSE,FALSE,,surname4,24/03/2020 21:36,,FALSE,,,,,,,,,None,FALSE,,,,,,,,,,user4,,,email4,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationMethod],System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationPhoneAppDetail],,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationRequirement],,TRUE,24/03/2020 21:36,,FR,,user4,,Member,Healthy,
    password5,System.Runtime.Serialization.ExtensionDataObject,System.Collections.Generic.List`1[System.String],System.Collections.Generic.List`1[System.String],,FALSE,,,,department5,,person5,,,firstname5,,,FALSE,FALSE,,surname5,24/03/2020 21:37,,FALSE,,,,,,,,,None,FALSE,,,,,,,,,,user5,,,email5,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationMethod],System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationPhoneAppDetail],,System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationRequirement],,TRUE,24/03/2020 21:37,,FR,,user5,,Member,Healthy,



    Friday, March 27, 2020 7:57 PM

All replies

  • Variables in strings that are objects with property references must be enclosed in "$()" the subexpression evaluation operator.  If that is missing the string will not work as expected.

    You cannot use a street address for a mail address.

    Your CSV is full of garbage and is not a legal CSV.  What you need to do is actually think about and look at what you are doing.  Just b;indly copying and pasting things always leads to this kind of failure.


    \_(ツ)_/


    • Edited by jrv Friday, March 27, 2020 8:46 PM
    Friday, March 27, 2020 8:44 PM
  • correct me if i'm wrong :
    i should enclose the varables in the -body, for example $(DisplayName) ?

    don't worry i know that i can't use the street address as an email address, i just use that field for stocking another email address for contacting the users (if needed).

    that csv is the extract from azure after creating the accounts, i thought that i could reuse it to save doing copy/paste

    in what way is the CSV not legal ?

    Friday, March 27, 2020 11:11 PM

  • in what way is the CSV not legal ?

    Look at the contents.  They are not useful.

    "System.Runtime.Serialization.ExtensionDataObject"

    What is that doing in a CSV file.


    \_(ツ)_/

    Saturday, March 28, 2020 3:09 AM
  • i've cleaned up the CSV and the code which has got rid of multiple errors, but i've still got one problem.

    but first here is the new script and the new CSV

    $credential = Get-Credential
    $Body = @"
    <html>
    	<body bgcolor=white style="topmargin=0;">
    		<div align=center style="margin-left:auto; margin-right:auto;">
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FBFBFB; margin-left:auto; margin-right:auto; width:100%;border:0;align:center;">
    				<tr>
    					<td style="padding:15px; text-align:center; width: 100%;border:0;"><img src="cid:logo.png"></td>
    				<tr>
    			</table>
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FBFBFB; margin-left:auto; margin-right:auto; width:100%;border:0;">
    				<tr>
    					<td style="width:35px;border:0;"></td>
    					<td style="background:#ffffff; height:315px;border-left:0; border-top:0; border-right: 2px solid WhiteSmoke; border-bottom: 2px solid WhiteSmoke; text-align:left; padding:48px;"align=center>
    						<p style="line-height:33.0pt; font-family:Calibri;max-width:480pt;">
    							<b>
    								<span style="font-size:28.5pt;">Bonjour $_.DisplayName votre compte vient d&#39;&ecirc;tre cr&egrave;&egrave;</span>
    							</b>
    						</p>
    						<p style="line-height:33.0pt font-size:11.0pt;font-family:Calibri; margin-top:21pt; max-width:480pt;">
    							<span>
    								Nom d&#39;utilisateur: <b>$_.SignInName</b><br>
    								Mot de passe temporaire: <b>$_.Password</b> 
    							</span>
    						</p>
    						<p style="line-height:33.0pt font-size:11.0pt;font-family:Calibri; margin-top:21pt; max-width:480pt; margin-bottom:40pt;">
    							<span>La présidence de votre BREI a dû recevoir une formation sur les différents outils à vous transmettre.</span>
    						</p>
    						<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FFFFFF; margin-left:auto; margin-right:auto; width:100%; border:0;">
    							<tr>
    								<td style="width:2%;border:0;"></td>
    								<td style="width:46%; border:0;text-align:center;">
    									<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;background:#D83B01;">
    										<tr style="border:0; ">
    											<td style="width:5%;border:0;"></td>
    											<td style="border-bottom:10px solid #D83B01;border-top:10px solid #D83B01;text-align:center;border-left:0;border-right:0;">
    												<a href="https://www.office.com/?auth=2" target="_blank" style="text-decoration: none;">
    													<strong>
    														<span style='font-family:"Calibri",sans-serif;color:white;font-weight:normal;'>Connexion à Office 365</span>
    													</strong>
    												</a>
    											</td>
    											<td style="width:5%;border:0;"></td>
    										</tr>
    									</table>						
    								</td>
    								<td style="width:4%;border:0;"></td>
    								<td style="width:46%; border:0;text-align:center;">
    									<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;background:#5359af;">
    										<tr style="border:0; ">
    											<td style="width:5%;border:0;"></td>
    											<td style="border-bottom:10px solid #5359af;border-top:10px solid #5359af;text-align:center;border-left:0;border-right:0;">
    												<a href="https://teams.microsoft.com/downloads" target="_blank" style="text-decoration: none;">
    													<strong>
    														<span style='font-family:"Calibri",sans-serif;color:white;font-weight:normal;'>Télécharger Teams</span>
    													</strong>
    												</a>
    											</td>
    											<td style="width:5%;border:0;"></td>
    										</tr>
    									</table>							
    								</td>
    								<td style="width:2%;border:0;"></td>
    							</tr>
    						</table>
    					</td>
    					<td style="width:35px;border:0;"></td>
    				<tr>
    			</table>
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;max-width:480pt;background:#FBFBFB; margin-left:auto; margin-right:auto; width:100%; height:35px;">
    				<tr>
    					<td style="border:0;"></td>
    				<tr>
    			</table>	
    		</div>			
    	</body>
    </html>
    "@
    
    Import-Csv -Path "E:\tmp\bureau national des élèves ingénieurs\test.csv"| foreach {Send-MailMessage -To $_.StreetAddress -from "webmaster@bnei.fr" -SMTP "smtp.office365.com" -port "587" -UseSsl -Credential $credential  -Subject "test email" -BodyAsHtml -body $body -Attachments "E:\tmp\bureau national des élèves ingénieurs\logo.png" | Write-Output "sending email $_.DisplayName"}

    Password,DisplayName,SignInName,StreetAddress
    pswd1,user1,login1,glennmckenna2001@hotmail.com
    pswd2,user2,login2,glennmckenna2001@hotmail.com
    pswd3,user3,login3,glennmckenna2001@hotmail.com
    pswd4,user4,login4,glennmckenna2001@hotmail.com
    pswd5,user5,login5,glennmckenna2001@hotmail.com
    


    This is what the email looks like when i receive it

    if i enclose the variables in $(), as you said i get the following error :

    DisplayName : The term 'DisplayName' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:17 char:75
    + ...         <span style="font-size:28.5pt;">Bonjour $(DisplayName) votre  ...
    +                                                       ~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (DisplayName:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    so my current problems are that:

    -> $_.DisplayName, $_.SignInName and $_.Password aren't replaced with the values in the CSV

    -> Write-Output "sending email $_.DisplayName" doesn't work at all

    Saturday, March 28, 2020 2:56 PM
  • I said enclose the variable in the subexpression operator; not just the variable's name.

    \_(ツ)_/

    Saturday, March 28, 2020 3:21 PM
  • i've the following ways of writing it and none of them worked

    • ($_.DisplayName)
    • $(_.DisplayName)
    • $_(.DisplayName)
    • $_.(DisplayName)
    Saturday, March 28, 2020 3:41 PM
  • i've the following ways of writing it and none of them worked

    • ($_.DisplayName)
    • $(_.DisplayName)
    • $_(.DisplayName)
    • $_.(DisplayName)

    You have not done what I said.

    If your variable is "$_.DisplayName" then just enclose it in "$()".  You keep moving things around without thinking of the rule>  This "$()" is an operator.

    Look it up in the help to get a more complete discussion of what it does.

    help about_operators

    This is why it is critical that you actually sit down and learn PowerShell.  There are 100+ basic items that you must know before trying to use PowerShell.  You cannot learn this from copying code or asking questions in a forum.  It must be studied just like and other engineering technology, science or mathematics.


    \_(ツ)_/

    Saturday, March 28, 2020 3:50 PM
  • i've done a little research (but not finished).

    did you mean to write it like this : $($_.Password)

    if yes, it will execute with no error but in the email, where the username / email /password are meant to be, it will just showup as blank

    here is what the email looks like :

    

    Ps: i know that i can't just go copying and pasting code from any were, that is why i'm looking to learn and when i'm in difficulty i reach to forums for help which will allow me to learn from my errors

    PPs: thanks for the help and the fast answers you have been giving me so far


    Saturday, March 28, 2020 5:08 PM
  • Ok. Now that we have cleared up that issue we need to look at how you understand expandable strings.  How do you think the variable is going to get into you string?  The variable doesn't exist when you create $body.  Again, this is what you would have learned in chapter one of any training in PowerShell.  Guessing never works with a technology when you have no real skill sets in science, math or technology.  You cannot gain a skill by guessing,  You must acquire it by training,  Of course if you want to reproduce all of man's learning over the last few hundred years by trial and error methods good luck.  It will take you a few lifetimes to do that.

    Here is a much better way to solve you issue:

    $template = 'This {0} is a {1} to {2}'
    
    'apple','orange','pear' |
        ForEach-Object{
            $template -f $_, 'good', 'fruit'
        }


    \_(ツ)_/

    Saturday, March 28, 2020 5:25 PM
  • I am not going to rewrite and redesign you whole effort but I will give you this one more hint at how we do this in PowerShell.

    $sendMailMessage = @{
        To = $_.StreetAddress
        From = 'webmaster@bnei.fr'
        SmtpServer = 'smtp.office365.com'
        Port = 587
        UseSsl = $true
        Credential = $credential
        Subject = 'test email'
        BodyAsHtml = $true
        Attachments = 'E:\tmp\bureau national des élèves ingénieurs\logo.png'
    }
    
    Import-Csv 'E:\tmp\bureau national des élèves ingénieurs\test.csv' | 
        ForEach-Object{
            $body = $bodyTemplate -f $_.SignInName,$_.DisplayName,$_.Password
            Send-MailMessage -Body $body @sendMailMessage
            Write-Output "sending email $_.DisplayName"
        }
    
    Just fix your body to support the replacement.


    \_(ツ)_/

    Saturday, March 28, 2020 5:33 PM
  • "-f" operator is documented here:

    help about_operators

    The other is documented here:

    help about_splatting


    \_(ツ)_/

    Saturday, March 28, 2020 5:37 PM
  • your links aren't working, are these the pages that you were trying to link ?

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7

    many thanks for the second hint, don't worry i wasn't asking for you to redo all of it :)

    if i understand correctly

    • the splat $body (from the old version) becomes $bodyTemplate
    • and i replace the variables in the splat by {0} (for SignInName), {1} (for DisplayName) and {2} (for Password)

    are these two correct ?

    Saturday, March 28, 2020 7:32 PM
  • Great news, it works !

    i only had to change the position the "To" variable, here is the final solution :

     $sendMailMessage = @{
         From = 'webmaster@bnei.fr'
         SmtpServer = 'smtp.office365.com'
         Port = 587
         UseSsl = $true
         Credential = $credential
         Subject = 'test email'
         BodyAsHtml = $true
         Attachments = 'E:\tmp\bureau national des élèves ingénieurs\logo.png'
     }
    
     Import-Csv 'E:\tmp\bureau national des élèves ingénieurs\test.csv' |
         ForEach-Object{
             $body = $bodyTemplate -f $_.SignInName,$_.DisplayName,$_.Password
             Send-MailMessage -Body $body -To $_.StreetAddress @sendMailMessage
             Write-Output "sending email $_.DisplayName"
         }would you done this code another way ?

    many thanks for your help

    totally off subject,

    • would you of done this script another way ?
    • would you advise a place / channel for learning powershell ?
    Saturday, March 28, 2020 9:11 PM
  • Those aren't links. "Help" is a Powershell cmdlet;  "help about_operators" and "help about_splatting" will show you (in a Powershell console) information (not coincidently) about "operators" and "splatting".

    The links you posted are for Powershell 7. Make sure, when you're using Microsoft's site, that you've chosen (from the "Version" drop-down list) the version of Powershell you're using.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Saturday, March 28, 2020 9:32 PM
  • ok i think i might of said another stupid thing (about the links) #sorry :)

    thanks for info for the links, hadn't even realised...
    Saturday, March 28, 2020 9:38 PM
  • Great news, it works !

    i only had to change the position the "To" variable, here is the final solution :

     $sendMailMessage = @{
         From = 'webmaster@bnei.fr'
         SmtpServer = 'smtp.office365.com'
         Port = 587
         UseSsl = $true
         Credential = $credential
         Subject = 'test email'
         BodyAsHtml = $true
         Attachments = 'E:\tmp\bureau national des élèves ingénieurs\logo.png'
     }
    
     Import-Csv 'E:\tmp\bureau national des élèves ingénieurs\test.csv' |
         ForEach-Object{
             $body = $bodyTemplate -f $_.SignInName,$_.DisplayName,$_.Password
             Send-MailMessage -Body $body -To $_.StreetAddress @sendMailMessage
             Write-Output "sending email $_.DisplayName"
         }would you done this code another way ?

    many thanks for your help

    totally off subject,

    • would you of done this script another way ?
    • would you advise a place / channel for learning powershell ?

    This code you just posted can't possible work.  It has a number of errors and typos.

    Your issues are exactly why we constantly insist that people posting in this and othr forums take teh time to learn th technology.  Trying to help someone with absolutely no technical knowledge is extremely difficult and frustrating.

    Here is a free book to help you learnbasic Powershell.  It is for PS3 but the information is the same for PS5 and later.

    THe book is written by two excellent and experience admin with extensive experience with PowerShell and it is well written.


    \_(ツ)_/

    Sunday, March 29, 2020 8:47 AM
  • https://docs.microsoft.com/en-us/Exchange/recipients/user-mailboxes/recover-deleted-messages?view=exchserver-2019

    How to delet trash messages.


    hassansayedissa

    Please do not post unrelated messages in forums.  If you have a question please open a new topic and ask your question.

    \_(ツ)_/

    Sunday, March 29, 2020 9:16 AM
  • Many thanks for the book

    here is the final version of the code :

    $credential = Get-Credential
    $bodyTemplate = @"
    <html>
    	<body bgcolor=white style="topmargin=0;">
    		<div align=center style="margin-left:auto; margin-right:auto;">
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FBFBFB; margin-left:auto; margin-right:auto; width:100%;border:0;align:center;">
    				<tr>
    					<td style="padding:15px; text-align:center; width: 100%;border:0;"><img src="cid:logo.png"></td>
    				<tr>
    			</table>
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FBFBFB; margin-left:auto; margin-right:auto; width:100%;border:0;">
    				<tr>
    					<td style="width:35px;border:0;"></td>
    					<td style="background:#ffffff; height:315px;border-left:0; border-top:0; border-right: 2px solid WhiteSmoke; border-bottom: 2px solid WhiteSmoke; text-align:left; padding:48px;"align=center>
    						<p style="line-height:33.0pt; font-family:Calibri;max-width:480pt;">
    							<b>
    								<span style="font-size:28.5pt;">Bonjour {0} votre compte vient d&#39;&ecirc;tre cr&eacute;&eacute;</span>
    							</b>
    						</p>
    						<p style="line-height:33.0pt font-size:11.0pt;font-family:Calibri; margin-top:21pt; max-width:480pt;">
    							<span>
    								Nom d&#39;utilisateur: <b> {1}</b><br>
    								Mot de passe temporaire: <b> {2}</b> 
    							</span>
    						</p>
    						<p style="line-height:33.0pt font-size:11.0pt;font-family:Calibri; margin-top:21pt; max-width:480pt; margin-bottom:40pt;">
    							<span>La pr&eacute;sidence de votre BREI a d&ucirc; recevoir une formation sur les diff&eacute;rents outils &agrave; vous transmettre.</span>
    						</p>
    						<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="max-width:480pt;background:#FFFFFF; margin-left:auto; margin-right:auto; width:100%; border:0;">
    							<tr>
    								<td style="width:2%;border:0;"></td>
    								<td style="width:46%; border:0;text-align:center;">
    									<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;background:#D83B01;">
    										<tr style="border:0; ">
    											<td style="width:5%;border:0;"></td>
    											<td style="border-bottom:10px solid #D83B01;border-top:10px solid #D83B01;text-align:center;border-left:0;border-right:0;">
    												<a href="https://www.office.com/?auth=2" target="_blank" style="text-decoration: none;">
    													<strong>
    														<span style='font-family:"Calibri",sans-serif;color:white;font-weight:normal;'>Connexion &agrave; Office 365</span>
    													</strong>
    												</a>
    											</td>
    											<td style="width:5%;border:0;"></td>
    										</tr>
    									</table>						
    								</td>
    								<td style="width:4%;border:0;"></td>
    								<td style="width:46%; border:0;text-align:center;">
    									<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;background:#5359af;">
    										<tr style="border:0; ">
    											<td style="width:5%;border:0;"></td>
    											<td style="border-bottom:10px solid #5359af;border-top:10px solid #5359af;text-align:center;border-left:0;border-right:0;">
    												<a href="https://teams.microsoft.com/downloads" target="_blank" style="text-decoration: none;">
    													<strong>
    														<span style='font-family:"Calibri",sans-serif;color:white;font-weight:normal;'>T&eacute;l&eacute;charger Teams</span>
    													</strong>
    												</a>
    											</td>
    											<td style="width:5%;border:0;"></td>
    										</tr>
    									</table>							
    								</td>
    								<td style="width:2%;border:0;"></td>
    							</tr>
    						</table>
    					</td>
    					<td style="width:35px;border:0;"></td>
    				<tr>
    			</table>
    			<table role=”presentation” cellspacing=”0″ cellpadding=”0″ border=”0″ style="border:0;max-width:480pt;background:#FBFBFB; margin-left:auto; margin-right:auto; width:100%; height:35px;">
    				<tr>
    					<td style="border:0;"></td>
    				<tr>
    			</table>	
    		</div>			
    	</body>
    </html>
    "@
    $sendMailMessage = @{
        From = 'webmaster@bnei.fr'
        SmtpServer = 'smtp.office365.com'
        Port = 587
        UseSsl = $true
        Credential = $credential
        Subject = 'test email'
        BodyAsHtml = $true
        Attachments = 'E:\tmp\bureau national des élèves ingénieurs\logo.png'
    }
    
    Import-Csv 'E:\tmp\bureau national des élèves ingénieurs\test.csv' | 
        ForEach-Object{
            $body = $bodyTemplate -f $_.SignInName,$_.DisplayName,$_.Password
            Send-MailMessage -Body $body -To $_.StreetAddress @sendMailMessage
            Write-Output "sending email $_.DisplayName"
        }

    could you give me any indications to the errors/typos present ?

    for info at the moment it produces what i want

    the only error that i think that there might be is on the Write-Output cause it give me this output :

    sending email @{Password=pswd5; DisplayName=user5; SignInName=login5; StreetAddress=user5@hotmail.com}.DisplayName

    Sunday, March 29, 2020 12:59 PM
  • Read the book and all of this will become clear.  This forum is not a good place to get personal training.  I gave you some pointers to get your interest and a book so you could learn basic PowerShell.  It is now up to you to do your part.

    \_(ツ)_/

    Sunday, March 29, 2020 1:59 PM