Formatting VCard in PHP

后端 未结 3 1220
野性不改
野性不改 2021-01-03 17:30

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 M

相关标签:
3条回答
  • 2021-01-03 17:43

    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);
    }
    
    0 讨论(0)
  • 2021-01-03 17:47

    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.

    0 讨论(0)
  • 2021-01-03 17:59

    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";
    
    0 讨论(0)
提交回复
热议问题