Bulk-Export Mails from Apple Mail

Apple’s mail client has an annoying limitation: If you want to export mail messages as .eml or .emlx you have to do it one by one, i.e. dragging one message at a time from the Mail App’s window to a Finder window. It won’t let you drag several messages at the same time.

applemail-icon

However with a small script this limitation can be circumvented easily.

Update 2017-03-29:

This script export your messages as .emlx. If you prefer .eml as export format, then please see Bulk-Export Mails from Apple Mail, Part II which provides an AppleScript for that. (Which is also a bit easier to use, as a side note.) The differences between .eml and .emlx are also explained there.

If you prefer .emlx, then read on.

Usage:
  1. In Mail.app create a local (“On my Mac”) mailbox named “Export”.
  2. Drag all the messages you want to export into the “Export” mailbox.
  3. Run the script (double-click).
    • The script will export all messages from the “Export” mailbox to a new “Exported Mails” folder on your desktop.
    • Messages will be renamed as [date] [subject] [author]. You can customize the renaming pattern in the script. Spotlight has to be enabled for this to work.
  4. Delete the messages from the “Export” mailbox, if desired.
  5. Move the exported messages from the “Exported Mails” folder on your desktop to the desired location.

If the script doesn’t run or crashes your computer, make sure permissions of the script are set correctly to 755.

Please note:

With macOS 10.12 (Sierra) Apple has changed the way how Mail is filing the messages. For example, on my system the “Export” mail box is now in a folder named with an account ID: ~/Library/Mail/V4/0CB67705-6830-4E6B-B129-8834DE89F521/Export.mbox

That means you will probably have to open the Mail folder in ~/Library and look for the exact location of Export.mbox. Then set the correct location in the script.

Download (1.1.2 / 2017-02-08)

This is the content of the script:

As you can see several different renaming patterns are already provided. Just uncomment/comment the corresponding lines in the script to your need.

#!/bin/bash

# Version 1.1.2
# El Capitan, Sierra

# What it does:

# Exports contents of an Apple Mail mailbox as individual emlx files to a local
# destination. Exported messages will be time-stamped and renamed according to
# their spotlight metadata.

# Usage:

# 1) In Apple Mail create a local ('on my Mac') mailbox and name it "Export".
# 2) Copy or move the messages you want to export into that mailbox.
# 3) Run this script.
# 4) You should find the exported and renamed files in a folder "Exported Mails"
#    on your Desktop.

# Note: Set your preferred time-stamp format, naming scheme, etc. by
# un/commenting the corresponding lines in the script below.


## Script

## Define source and target directories
# macOS 10.12
# It may look like this (find out the correct ID of the folder on your system):
# sourceDir=~/Library/Mail/V4/0CF67405-6830-4A6B-B162-8934DE09F521/Export.mbox
# OS X 10.11
sourceDir=~/Library/Mail/V3/Mailboxes/Export.mbox
# For older systems use:
# sourceDir=~/Library/Mail/V2/Mailboxes/Export.mbox
expDir=~/Desktop/Exported\ Mails

## Make target directory if not already present
mkdir "$expDir"

## Copy mail messages from source mailbox to target directory
find "$sourceDir" -name '*.emlx' -exec ditto {} "$expDir" \;

## Index target directory
mdimport "$expDir"
sleep 1

## Rename exported mails by Spotlight data
for f in "$expDir"/*.emlx
  do

    ## Get message author's name

    aut=$(mdls -name kMDItemAuthors -raw "$f" |
    head -n 4 | tr -ds '\n"()' ' ' |
    sed '
    s/^ //
    s/\\U[0-9a-f]\{4\}//g')

    ## Or: author's address:

    # aut=$(mdls -name kMDItemAuthorEmailAddresses -raw "$f" |
    # head -n 4 | tr -ds '\n"()' ' ' | sed 's/^ //')


    ## Get addressee names

    # adr=$(mdls -name kMDItemRecipients -raw "$f" |
    # head -n 4 | tr -ds '\n"()' ' ' |
    # sed '
    # s/^ //
    # s/\\U[0-9a-f]\{4\}//g' |
    # head -c 40)

    ## Or: addressee addresses

    # adr=$(mdls -name kMDItemRecipientEmailAddresses -raw "$f" |
    # head -n 4 | tr -d '[:space:]"()' | head -c 40)


    ## Get message subject

    suj=$(mdls -name kMDItemSubject -raw "$f" | head -c 80 |
    sed 's/\//∕/g')


    ## Content creation date to time stamp

    ccd=$(mdls -name kMDItemContentCreationDate -raw "$f")

    ## POSIX time (complete)
    # ccd=$(date -j -u -f '%Y-%m-%d %H:%M:%S %z' "$ccd" +"%s")

    ## UTC (basic, truncated year)
    # ccd=$(date -j -u -f '%Y-%m-%d %H:%M:%S %z' "$ccd" +"%y%m%dT%H%M%SZ")

    ## Local time (basic, truncated year, w/o time zone)
    # ccd=$(date -j -f '%Y-%m-%d %H:%M:%S %z' "$ccd" +"%y%m%dT%H%M%S")

    ## Local time (basic, truncated year/time, w/o time zone)
    ccd=$(date -j -f '%Y-%m-%d %H:%M:%S %z' "$ccd" +"%y%m%dT%H%M")

    ## Local time (truncated year, w/ short time zone)
    # ccd=$(date -j -f '%Y-%m-%d %H:%M:%S %z' "$ccd" +"%y%m%dT%H%M%S%z" |
    #   head -c 16)


    ## Final naming of exported messages

    # msgName="$ccd [$aut] $suj"
    msgName="$ccd $suj [$aut]"
    # msgName="$ccd [$aut → $adr] $suj"
    # msgName="$ccd [$aut - $adr] $suj"
    # msgName="$ccd $suj [$aut - $adr]"
    # msgName="$suj [$aut - $adr - $ccd]"

    mv "$f" "${f%/*}"/"$msgName"."${f#*.}"

    aCounter=$[aCounter + 1]

  done

  afplay '/System/Library/Sounds/Glass.aiff'
  osascript -e 'display notification with title "'"$aCounter"'" & " mails exported"'

  echo "Done"
  exit 0