Dieses PHP Skript konvertiert Visitenkarten, die als .eml Dateien vorliegen in das VCARD-Format (.vcf). Diese können dann auf einfache Weise in alle gängigen Emailprogramme importiert werden.
Der Quellcode kann über die englische Version dieses Posts heruntergeladen werden: EML to VCF Converter
This PHP script converts contacts saved as .eml files to VCARDs (.vcf) that can easily be imported to any email client.
<?php
/**
* EML2VCFConverter.php
*
* Converts contacts saved as .eml files to VCARDs (.vcf) that can easily be
* imported to any email client.
* Copyright (C) 2010 Matthias Kerstner, info@kerstner.at
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* DESCRIPTION:
* Converts contacts saved as .eml files to VCARDs (.vcf) that can easily be
* imported to any email client. Expects .eml files to be UTF-8 encoded.
* The generated VCARD-files will be saved using UTF8-encoding by default. As
* programs such as Windows Contacts for some strange reasons don't handle UTF-8
* encoded VCARDs correctly you can optionally change the default encoding of
* the generated VCARDs (UTF-8) to ISO-8859-1.
*
* USAGE:
* Be sure to set SRCFOLDER. If DSTFOLDER is not specified generated .vcf files
* will be placed inside a folder named "converted" in your SRCFOLDER. Note that
* existing .vcf files will be overwritten! Optionally you can set DEBUG to
* either true or false. Be sure to set PHP's execution timeout to a value high
* enough in order to give the script time to finish.
*
* CHANGELOG:
* 1.1: minor code optimizations
* 1.0: initial release
*
* @filesource EML2VCFConverter.php
* @author Matthias Kerstner, info@kerstner.at
* @version 1.1
*/
/**
* EDIT HERE
*/
define("SRCFOLDER", "");
define("DSTFOLDER", "");
define("UTF8DSTENCODING", true); //possible values: true, false (=ISO-8859-1)
define("DEBUG", true); //possible values: true, false
/**
* DO NOT EDIT BELOW THIS LINE!!
*/
/**
* Courtesy of Justin Cook
* http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/
*/
function get_string_between($string, $start, $end) {
$string = " ".$string;
$ini = strpos($string,$start);
if($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
function debug($msg) {
if(DEBUG)
echo "debug: ".$msg."<br>";
}
debug("using source folder '".SRCFOLDER."'");
$dh = @opendir(SRCFOLDER);
if(!$dh)
die("ERROR: failed to open source folder");
debug("reading source folder...");
if(strlen(DSTFOLDER) < 1) {
debug("no destination folder specified, using default...");
$dstFolder = SRCFOLDER."/converted";
} else
$dstFolder = DSTFOLDER;
if(!file_exists($dstFolder)) {
if(!mkdir($dstFolder, 0777, true))
die("ERROR: could not create destination folder '$dstFolder'");
else
debug("created destination folder '$dstFolder'...");
} else
debug("using existing destination folder '$dstFolder'...");
$dstFoldername = substr($dstFolder, strripos($dstFolder, "/")+1);
$i = 0;
while(($srcFile = readdir($dh)) !== false) {
$srcFilename = basename($srcFile, ".eml");
if($srcFilename == $dstFoldername || $srcFilename == "." || $srcFilename == "..")
continue;
else {
$srcFile = SRCFOLDER."/".$srcFile;
$handle = fopen($srcFile, "r");
$vcfContent = fread($handle, filesize($srcFile));
fclose($handle);
$vcfContent = iconv("UTF-8", "ISO-8859-1", $vcfContent);
$vcfContent = get_string_between($vcfContent, "BEGIN:VCARD", "END:VCARD");
$vcfContent = "BEGIN:VCARD".$vcfContent."END:VCARD";
if(UTF8DSTENCODING)
$vcfContent = iconv("ISO-8859-1", "UTF-8", $vcfContent);
$dstFile = $dstFolder."/".$srcFilename.".vcf";
if(($fp = fopen($dstFile, "w+")) === false)
die("ERROR: could not create file '$dstFile'");
fputs($fp, $vcfContent);
fclose($fp);
$i++;
}
}
closedir($dh);
debug("Files written: $i");
echo "done!";
?>