在PHP中格式化VCard [英] Formatting VCard in PHP

查看:173
本文介绍了在PHP中格式化VCard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过PHP生成一个VCard,并将它们发送给用户。我写了一个带有硬编码数据的初始脚本,但最终结果将从MySQL中填入VCard。

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.

当与合法的VCard(从另一个站点下载并进行测试),它们看起来几乎相同,但是当我尝试导入我生成的VCard时,它显示没有数据。实际上,如果我在手机上打开它,它甚至不认识到它是一个vcard,而是将我发送到一个破损的Google文档。

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.

ve从维基百科获取了一些代码来格式化vcard,一切似乎都很好。您看到我的格式有错误吗?我尝试了不同的换行符 - 没有用。想法?

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);
}
?>

更新1:这让我更困惑,但也许会帮助你调试我已将我的(坏)生成的VCard下载到我的电脑,以及从不同的网站下载的一个好的VCard。如预期的那样,我生成的一个没有数据打开,但是好的工作正常。然后,我创建了一个.vcf扩展名的第三个空文件,并将文本从我的(坏)文件复制到该空文件中。我打开那个文件,所有的数据显示完美。要进一步测试,我将文本从好的VCard文件复制到我的坏文件中,并且仍然没有打开数据。所以,它似乎是一些关于编码或一些其他文件的东西,我不明白。这不是权限 - 这一切都是一样的。

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.

更新2:我更改了PHP,这样就迫使我下载了VCard发电子邮件下载的文件打开非常好,所以错误是发生在我如何编码(正确的单词?)文件,或GMail如何解释它。

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.

更新3:修正:。我不知道为什么会这样 - 因为我可以发现的其他教程就是相反的 - 但是有一些关键的改变。首先,我将电子邮件中的编码从base64更改为8bit,并将附件内容更改为传递给电子邮件功能的字符串(使其为8位形式,而不是64位)。这使得VCard在我的桌面上有效和可读。要让它在我的Android上阅读,我不得不将$ fileatt_type变量更改为text / x-vcard,否则Gmail认为它是一个文档。

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.

推荐答案

我最近不得不在android和iphone上使用vcards。我使用以下功能,这是上面列出的修改版本。这将发送vcards,iphone上的gmail和邮件将能够打开。他们也在Thunderbird工作。

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);
}

这篇关于在PHP中格式化VCard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆