Managing large volumes of contact data can be a challenge. Recently, I worked on a project to convert contacts in eml to vcf for a client in South-East London. The issue arose because the macOS Contacts app kept crashing while syncing with the server. The contacts were stored on a Kerio Connect server, and to make them usable, I needed an efficient way to automate the conversion process. This guide details the solution I developed to streamline this task, ensuring smooth data handling for IT professionals.
This guide outlines an efficient way to automate conversion of contacts in .eml to .vcf by using a custom shell script, tailored for IT service providers managing customer data or system migrations.
Kerio Connect stores contacts as individual .eml files in a structured directory. A look at the folder structure using tree reveals how these files are stored:
Desktop james$ tree .
Contacts Folder
│ ├── #assoc
│ ├── #msgs
│ │ ├── 00000001.eml
│ │ ├── 00000004.eml
│ │ ├── 00000005.eml
│ │ ├── 00000006.eml
│ │ ├── 00000007.eml
│ │ ├── 00000008.eml
│ │ ├── ...
│ │ ├── ...
│ │ ├── ...
│ │ ├── 00000504.eml
Each .eml file contains standard email headers, followed by the actual vCard data. The vCard content lies between the BEGIN:VCARD and END:VCARD tags:
To make these contacts usable in .vcf format, I needed to:
After reviewing outdated and poorly formatted scripts online (such as https://palatum.org/2014/01/kerio-contacts-export-script-eml-to-vcf), I created a modern bash script tailored for IT consultants and system administrators. It efficiently handles large datasets and avoids crashes in macOS Contacts or similar applications. This script is ideal for IT-managed service providers (MSPs) needing to migrate or process client contact data.
What the script does:
The script includes error handling and leverages the find command with the -exec flag for robust processing. It successfully converted over 20,000 contacts in under a minute, proving its efficiency in real-world scenarios.
Here’s the full bash script, complete with comments, designed for IT administrators and developers managing .eml to .vcf conversions:
#! /bin/bash
# .eml contact to vcard converter, 2019-12-08 James Spong / FatMac IT Solutions
# Test first, use wisely and remember; with great power, comes great responsibility
# ask user for source folder and assign to var
read -p "Enter the absolute file path of the folder containing the .eml Files": SRC
# assign variables
BACKUP="$SRC/eml-backup"
ORIGINAL="$SRC/eml-originals"
DEST="$SRC/eml-to-vcf-output"
MERGED="$SRC/merged-vcfs"
# make new BACKUP directory
mkdir -p $BACKUP
# make new ORIGINAL directory
mkdir -p $ORIGINAL
# make new DEST directory
mkdir -p $DEST
# make new MERGED directory
mkdir -p $MERGED
# change directory to SRC
cd $SRC
# move SRC emls to ORIGINAL directory
find $SRC -name '*.eml' -exec mv {} $ORIGINAL \;
# backup ORIGINAL directory as tar.gz, save in BACKUP directory
tar --exclude="$BACKUP" --exclude="$DEST" --exclude="$MERGED" -zcvf $BACKUP/eml-files.tar.gz $ORIGINAL
# change directory to ORIGINAL
cd $ORIGINAL
# echo status and loop through command
for vcard in *.eml
do
echo "processing $vcard"
# change DOS CR/LF to *nix line endings and strip the header of the message
# then output the „correct“ VCards to the backup directory
sed -e 's/.$// ; 1,/^$/ d' < $vcard | iconv -f UTF8 -t MACROMAN > $DEST/${vcard/%eml/vcf}
done;
# merge all vcards into one .vcf
# cat $DEST/*.vcf > $MERGED/merged-vcards.vcf
find $DEST -name "*.vcf" -exec cat '{}' ';' > $MERGED/merged-vcards.vcf
echo ""
echo " ___ / / /"
echo "\ / |__ /\ |__| / / / "
echo " | |___ /~~\ | | . . . "
echo " "
echo ""
This script is particularly useful for IT professionals handling:
The process is fast, efficient, and ensures data integrity—key priorities for IT solutions providers and MSPs.
Whether you’re an IT administrator, developer, or managed service provider, this script streamlines contact data management. If you’ve got questions, suggestions, or ideas for improvement, feel free to share them in the comments below.
Want more tips for IT professionals? Stay tuned for future posts covering shell scripting, server migrations, and other IT solutions.
Happy scripting!
your script is awesome! it saved my life today!!