Send emails from a csv file (grouping records with the same email addresses)

Writing a script to send emails from a csv which contains record details and email addresses using the Send-Mailmessage cmdlet.
That part is no trouble, however, the csv file contain records that can have the same email address.
Is there a way/method I could send the records with the same email address once instead of sending individual email messages?
Thanks
Data in the csv file
Record number, description, email
1234, Test 1, [email protected]
5678, Test 2, [email protected]
1245, Test 3, [email protected]
4578, Test 4, [email protected]
$data = Import-csv c:\records.csv
ForEach($address in $data)
Send-mailmessage -To $address.emails -from [email protected] -subject "List of records" -Body $data -SmtpServer 192.168.1.1

The following code does what you are looking for:
$data = Import-csv c:\records.csv$mx = "192.168.1.1"
$subject = "List of records"
$from = "[email protected]"
$data| Group-Object email|Select-Object Name, @{n='msg'; e={$_.Group| Select-Object -Property "record number", description|ConvertTo-Csv -notypeinformation|Out-String}}|
ForEach-Object {Send-MailMessage -to $_.Name -Body $_.msg -SmtpServer $mx -Subject $subject -From $from}
I also recommend no forcing everything into one line.  "One-liner" means "one pipeline" not putting all code on one unreadable line.
Your code should look like this:
$data|
Group-Object email|
Select-Object Name, @{
n='msg'; e={
$_.Group| Select-Object -Property "record number", description|
ConvertTo-Csv -notypeinformation|Out-String
}|
ForEach-Object {
Send-MailMessage -to $_.Name -Body $_.msg -SmtpServer $mx -Subject $subject -From $from
Now we can see the code and see that you are still unnecessarily converting back and forth.
To get the group look at how I did it.
$body=$_.Group | Format-Table |Out-String
Isn't that much easier?
Once you master the pipeline these things will become second nature.  In PowerShell it is not necessary to write lots of code most of the time.
¯\_(ツ)_/¯

Similar Messages

  • Is it possible to send notes from an iTouch to a computer with the same email?

    I have several Notes on my iTouch I would like to send to myself so that I'll be able to access them on my computer, but will this work if the iTouch and computer (Mackbook Pro) both use the same Email accounts?
    Assistance is much appreciated.

    Just setup the iCloud on the Mac with the same iCloud ID. They will sync to the Mac's Notes app.
    If no iCloud on the the iPod (you need iOS 5 and later and that requires a 3G or later iPod) you will have to email them using an account accessible on the Mac

  • How do i delete email from my iPhone,ipad and computer at the same time?

    How do i delete email from my iPhone,ipad and computer at the same time?

    Go to http://support.google.com/mail/bin/answer.py?hl=en&answer=77702. It tells you how to set up gmail on iPhone.  Then go to gmail settings then "accounts and imports" and add your AT&T POP email account  to import mail from AT&T to gmail

  • Can i migrate an existing (hosted) email account to iCloud and continue to use the same email address?

    Can I migrate an existing (hosted) email account to iCloud and continue to use the same email address?

    No. Although you could migrate your mail to iCloud via IMAP, iCloud does not support sending emails from your own domain - only iCloud's own @me.com and @mac.com addresses can be used.

  • I forgot my digital signature password. How do I create a new one with the same email.

    I can not remember my digital signature password. I've attempted to create a new one with the same email address. The message I receive is that i already have a digital signature with that email. I would like to use the same email address.

    You probably mean that you forgot your digital certificate password and I presume that this is a self-signed certificate that you yourself created, not a certificate that you received from a Certificate Authority (CA). If this is true then you can create a new self-signed digital certificate (Digital ID in Acrobat-speak) the same way you created the first one: In Acrobat XI it is Edit->Preferences->Signatures->Identities&Trusted Certificates->More. Then click on "Add ID" button and follow the prompts. You can create as many self-signed certificates with the same credentials (name, email, etc.) as you wish. You'd have to figure out how to distinguish them, though.

  • DTP error: No Message: Filter Out New Records with the Same Key

    Hello SDN,
    I have looked up solutions for this problem, but didnt find anything suitable. I am getting this error in multiple places: when i load pure master data into IOs like 0VENDOR, 0PLANT etc. or when I load DSOs like 0FIAP_O03.
    When I checked the load in sdn, one of the solutions was to apply note: 1489178 , but in this case, the user had nothing selected in his 'semantic key' list. Whereas, in my case.. 0PLANT is checked ...eventhough the other fields are disabled. So, I think my DTP is accurate.. as 'plant' is the only key field here.  But it still fails .. and so does 0VENDOR load. Moreover... the DSO load is crucial for our reports in Prod and its failure is a bigger issue for us.
    Has anyone seen this problem? Is there another solution other than the above note?
    Regards.

    Hello Arvind,
    If I select that option, will it delete one record if it finds 2 records with the same semantic key in 1 data packet. Also, will it delete a record if it finds that there is already a record existing in the data target with the same semantic key.
    I think, both of the above scenarios will result in incorrect data. From my understanding, 'Handle Duplicate Keys' is mostly used to filter out master data.. as in some instances.. you might get duplicate entries.
    But, in my case, the same error is happening in the case of data load from 0fi_ap_4 --> 0fiap_o03. What happens is... the overall status of the DTP becomes green.. but when you look at details section, the process stops at this message which is in yellow. Because of this, the DTP status in the PC remains yellow.. and finally turns red.. and the PC turns red.
    From my knowledge, it appears more like a bug. I was hoping to find a SAP NOTE which has some manual instructions that can resolve the issue, since the above NOTE asks for a support pack upgrade.. which would need a whole lotta testing.
    Regards.

  • Record filtered in advance as error records with the same key exist

    Dear Masters
       I am getting below error for master data (0MATERIAL_ATTR), while loading data from PSA to Info object through DTP (I have set the Handle duliocate record also). When I checked that error data in Error Stack, I am getting below error for all records(100)
    "Record filtered in advance as error records with the same key exist"
    Could u please help me to resolve this issue
    Thanks in Advance
    Raja

    Hi
    Thanks for reply
    I have loaded the Delta data in PSA. In PSA data was loaded successfully. I got the failure only when I loaded the from PSA to Maste data
    Thanks in advance
    Raja

  • How do i transfer the same video from my phone to my ipad with the same id account

    How do i transfer the same video from my phone to my ipad with the same Apple ID .

    Your question doesn't make a lot of sense without a little more information. You can't transfer a video from the phone to the iPad by connecting the devices. There are WiFi transfer apps that will let you send a video from one device to another one. I get the feeling that isn't what you are really asking though because that has absolutely nothing to do with your Apple ID.
    If you want to download the same purchased movie to the iPad that you downloaded to the phone, go to the purchased tab of the iTunes Store on the iPad. Tap the Movies Button. Tap Not on this iPad. Find the movie and tP the cloud icon next to the movie icon and start downloading.
    You must be signed into the same ID on the iPad that you bought the movie with on the iPhone.

  • I created my Apple ID in Ireland, but would now like to switch to a German Apple ID, keeping the same email address. How do I delete my old Irish Apple ID, so I can create a new German one with the same email?

    I created my Apple ID in Ireland, but would now like to switch to a German Apple ID, keeping the same email address. How do I delete my old Irish Apple ID, so I can create a new German one with the same email?

    Welcome to the Apple Community.
    Click on your account name in the top right corner of the iTunes store in the iTunes application on your computer, enter your password and click the "view account" button. Navigate to the Apple ID summary where you have the option to "change country or region".
    In order to change your location, you will need a credit card registered to an address in the location you are trying to change to.
    You cannot change your location if you have credit on your account, if you are unable to spend your credit and leave a balance of zero, contact Apple, which you can do through iTunes Store Support.

  • I purchased songs from 2 different computers using itunes with the same apple id. I had 1 iPod I used for these but I just recently got an iPhone and I was wondering why it wasn't letting me put my purchased songs on my iPhone?

    I purchased songs from 2 different computers using itunes with the same apple id. I had 1 iPod I used for these but I just recently got an iPhone and I was wondering why it wasn't letting me put my purchased songs on my iPhone?

    You can transfer iTunes content from an iTunes library on a single computer only.
    Since these songs were purchased with the same iTunes account, you can download the song that is not in your iTunes library on the computer you will be syncing your iPhone with. Or you can download the song or songs direct on your iPhone.
    http://support.apple.com/kb/HT2519?viewlocale=en_US&locale=en_US

  • Can two ipods with the same email address face time independantly?

    Can two ipods with the same email address face time independantly?

    Hey Bumblebee, To each other, no-- would be like trying to call yourself on the same phone. To different persons on other devices??? Not sure you'll have to try, I'm guessing if both are online you may well dial up on both, (once had the same address on the my computer and iPod, both would ring when called, this stopped when I changed the iPod's address). If you change one of the iPod's e-mail address you should be fine. You'll have to do a little experimenting. Hope this helps, Good luck. Cheers.

  • Can 2 people set up apple id with the same email address

    My husband and I share the same email address can we set up our apple ids with the same email and different passwords?

    No. You can share the same Apple ID for iTunes content, but for everything else...FaceTime, iMessage, iCloud, etc., you really need separate Apple ID's & these iD's must be verifiable email addresses. If you don't set things up that way, you'll have a mess on your hands.

  • Can I have more than 1 susbcriber with the same email address?

    I have developed a BC secure zone in Muse as a members only area. This works fine. I have subscribed all the members and all is OK except that I find if I try to subscribe 2 members with the same email address (e.g. a married couple with a shared email address), the first subscription is overwritten by the second. Is there a solution or do I just live with it?

    Hi Neil,
    This is the default behavior of the system because email is the unique identifier. You can't have more than 1 subscriber with the same email address.
    Regards,
    Aish

  • Is there any option to recover a file we saved with the same name that other by mistake in a MAC PC

    is there any option to recover a file we saved with the same name that other by mistake in a MAC PC

    There's a slim chance. +*Stop using the drive IMMEDIATELY.+*
    Then see: [Data Recovery|http://web.me.com/pondini/AppleTips/Recover.html].

  • I reply to an email on my iPad. When I view the same email on the PC in MS outlook there is no indication that I have replied to it. Has anyone found a solution for this?

    I reply to an email on my iPad. When I view the same email on the PC in MS outlook there is no indication that I have replied to it.
    Is there an email app that I can use?
    Has anyone found a solution for this?

    This seems to be an annoying part of the newer OS.  When I first got my Pearl in 2008, I didn't have that occur.  However once the OS got upgraded, and now with the Tour, it seems like that is the way it works.  Not clear to me why they would have thought this is preferable.

Maybe you are looking for

  • Problem Opening Pixel Bender Filter

    I just recently downloaded this Pixel Bender Filter for Photoshop CS4 (Win 32-bit). I downloaded it and finished installing with the adobe extension manager. However when I tried to use the pixel bender filter, photoshop becomes non responding and sa

  • I am unable to create a new Citadel database

    I am using DSC module 8.5 and am trying to create a new database using MAX.  I am following the instructions, but when I select "Finish" to create the database, the progress meter pops up and stays at 0%.  I should add that there was not a default da

  • Tansferring Library from PC to laptop

    I just bought a Dell Laptop and want to transfer my iTunes library from my current Dell PC to my new laptop. Does anyone know how to do that? Do I copy onto a disk? Zip drive? Please inform if you know. Thanks!

  • Edit append to table draw

    I have an append to table draw that I need to populate with data when creating document records (draw) using bapi_document_create2.  How can I include the append fields, or at the very least, edit them later, without doing direct table updates.

  • Nvidia-dkms module for GeForce 610M

    Hello everybody, I always use nvidia-dkms package as driver for my GeForce 610M GPU But recently when I try to load nvidia module modprobe produce this error: # modprobe nvidia modprobe: ERROR: could not insert 'nvidia': No such device so  dmesg | ta