eml to vcf shell script

Convert contacts in eml to vcf


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.

The Problem


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:

eml to vcf shell script
Contents of a vCard in .eml format. Lines 1-4 show the email header, lines 6-14 show the vCard information

To make these contacts usable in .vcf format, I needed to:

  1. Extract the vCard data from each .eml file.
  2. Convert the extracted data from .eml into .vcf format.
  3. Merge the individual .vcf files into a single, unified vCard file for exporting to CSV or syncing with other systems.

The Solution

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.

  • 1. Prompts the user to specify the source folder containing the .eml files.
  • 2. Creates a backup (eml-backup) and compresses the .eml files into a .tar.gz archive.
  • 3. Moves the original .eml files to a dedicated folder (eml-originals).
  • 4. Converts each .eml file to .vcf format by removing email headers and re-encoding the data from UTF-8 to MacRoman.
  • 5. Outputs the converted .vcf files into a new folder (eml-to-vcf-output).
  • 6. Merges all individual .vcf files into a single group vCard (merged-vcards.vcf) for simplified handling.

The Script


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 ""

Why This Script Works for IT Service Providers

This script is particularly useful for IT professionals handling:

  • Contact migrations from Kerio Connect or similar mail servers.
  • Large-scale data conversions for business clients.
  • Creating unified contact files for syncing or exporting to CRM systems.

The process is fast, efficient, and ensures data integrity—key priorities for IT solutions providers and MSPs.

Final Thoughts

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!

5 2 votes
Article Rating

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Holger Bartsch

your script is awesome! it saved my life today!!

0
Would love your thoughts, please comment.x
()
x