I'm trying to generate a VCard via PHP, and them email it out to the user. I wrote an initial script with hard-coded data, but the end-result will fill in the VCard from MySQL.
When viewing the VCard side-by-side with a legitimate VCard (downloaded and tested from another site) they look pretty much identical, but when I try and import my generated VCard it shows up with no data. Actually, if I open it on my phone, it doesn't even recognize that it is a vcard, and instead just sends me to a broken Google Doc.
I've borrowed some code from wikipedia for formatting the vcard, and everything seems fine. Do you see any errors in my formatting? I've tried different line breaks - to no avail. Ideas?
Here is the code for my generation / mail:
<?php
$content = "BEGIN:VCARD\r";
$content .= "VERSION:3.0\r";
$content .= "CLASS:PUBLIC\r";
$content .= "FN:Joe Wegner\r";
$content .= "N:Wegner;Joe ;;;\r";
$content .= "TITLE:Technology And Systems Administrator\r";
$content .= "ORG:Wegner Design\r";
$content .= "ADR;TYPE=work:;;21 W. 20th St.;Broadview ;IL;60559;\r";
$content .= "EMAIL;TYPE=internet,pref:__munged__@wegnerdesign.com\r";
$content .= "TEL;TYPE=work,voice:__munged__\r";
$content .= "TEL;TYPE=HOME,voice:__munged__\r";
$content .= "URL:http://www.wegnerdesign.com\r";
$content .= "END:VCARD";
mail_attachment("Joe Wegner.vcf", $content, "__munged__@wegnerdesign.com", "__munged__@wegnerdesign.com", "Wegner Design Contacts", "__munged__@wegnerdesign.com", "Joe Wegner's Contact Info", "");
function mail_attachment($filename, $content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$fileatt_type = "application/octet-stream";
$headers = "FROM: ".$from_mail;
$data = chunk_split(base64_encode($content));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
echo "sending message";
mail($mailto, $subject, $message, $headers);
}
?>
Update 1: This makes me more confused, but perhaps it will help you debug. I've downloaded my (bad) generated VCard to my computer, as well as a good VCard downloaded from a different website. As expected, my generated one opens with no data, but the good one works fine. I then created a third empty file with a .vcf extension, and copied the text from my (bad) file into that empty file. I opened that file, and all the data showed perfectly. To test even further, I copied the text from the good VCard file into my bad file, and it still opened with no data. So, it appears it's something about encoding or some other file thing that I don't understand. It's not permissions - that's all identical.
Update 2: I changed my PHP so that it would force me to download the VCard as well as email it. The downloaded file opens perfectly fine, so the error is either happening in how I'm encoding (right word?) the file, or how GMail is interpretting it.
Update 3: Fixed : Figured it out. I'm not sure why this is - because every other tutorial I can find out there says the opposite - but there were a few key changes. First, I changed the encoding on the email from base64 to 8bit, and I changed the attachment content to just be the string passed to the email function (so that it is in 8bit form, not 64). That made the VCard valid and readable on my desktop. To get it to read on my android I had to change the $fileatt_type variable to "text/x-vcard", otherwise Gmail thinks it is a document.
I had to get vcards working in android and iphone recently. I used the following function which is a modified version of the one listed above. This will send vcards that both gmail and mail on the iphone will be able to open. They work in Thunderbird as well.
function mail_attachment($filename, $content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$fileatt_type = "text/x-vcard";
$headers = "FROM: ".$from_mail;
$data = $content;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Transfer-Encoding: 8bit\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$filename}\"\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
//echo "sending message";
mail($mailto, $subject, $message, $headers);
}
You are missing a \n at the end of each line. If you look at a normal vCard (with notepad++ for example), it has a CR and LF at the end of each line, the one you create only has CR ('\r'). this works for me:
$content = "BEGIN:VCARD\r\n";
$content .= "VERSION:3.0\r\n";
$content .= "CLASS:PUBLIC\r\n";
$content .= "FN:Joe Wegner\r\n";
$content .= "N:Wegner;Joe ;;;\r\n";
$content .= "TITLE:Technology And Systems Administrator\r\n";
$content .= "ORG:Wegner Design\r\n";
$content .= "ADR;TYPE=work:;;21 W. 20th St.;Broadview ;IL;60559;\r\n";
$content .= "EMAIL;TYPE=internet,pref:joe@wegnerdesign.com\r\n";
$content .= "TEL;TYPE=work,voice:7089181512\r\n";
$content .= "TEL;TYPE=HOME,voice:8352355189\r\n";
$content .= "URL:http://www.wegnerdesign.com\r\n";
$content .= "END:VCARD\r\n";
I wrote a vCard validator based on the RFC which could help. It's not complete, but the cleaned files are at least nicely compatible with the tools and services I've tried (Gmail, gnokii and some others I can't remember). HTH.
来源:https://stackoverflow.com/questions/6190249/formatting-vcard-in-php