在php中发送带有从数据库生成的附件的电子邮件

时间:2012-06-04 01:40:49

标签: php email email-attachments vcard

我有一个简单的脚本,用于发送带有用户上传附件的电子邮件。如何修改脚本以便通过电子邮件发送在php中生成的文件(例如,在这种情况下,我想从数据库中提取联系人信息,生成文件的vCard并将其附加到电子邮件。

ViewCompany.php

<?php
// this auto-magically inserts header.html here
require('header.html');  


include('mail.php');

if(isset($_POST['name'], $_FILES['file'])){

    $body = array(

        "From: {$_POST['name']}",

        "Details:",
        "Name: {$_FILES['file']['name']}",
        "Size: {$_FILES['file']['size']}",
        "Type: {$_FILES['file']['type']}"

    );


    mail_file('receiver@email.com','sender@email.com','subject', implode("\r\n", $body), $_FILES['file']);

}
?>

<?php
echo    "<form action='' method='post' enctype='multipart/form-data'>";
echo        "<p>";
echo            "<label for='name'>Name</label>";
echo            "<input type='text' name='name' id='name'>";
echo        "</p>";
//          $file = "getVcard.php?CompanyID=" . $CompanyID;

echo        "<p>";
echo            "<label for='file'>File</label>";
echo            "<input type='file' name='file' id='file'>";
echo        "</p>";

echo        "<p>";
echo            "<input type='submit' value='Email file'>";
echo        "</p>";
echo    "</form>";
?>

mail.php

<?php

function mail_file($to, $from, $subject, $body, $file){

    $boundary = md5(rand());

    $headers = array(
        "MIME-Version: 1.0",
        "Content-Type: multipart/mixed; boundary=\"{$boundary}\"",
        "From: {$from}"
    );

    $message = array(
        "--{$boundary}",
        "Content-Type: text/plain",
        "Content-Transfer-Encoding: 7bit",
        "",
        chunk_split($body),
        "--{$boundary}",
        "Content-Type: {$file['type']}; name=\"{$file['name']}\"",
        "Content-Disposistion: attachment; fielname=\"{$file['name']}\"",
        "Content-Transfer-Encoding: base64",
        "",
        chunk_split(base64_encode(file_get_contents($file['tmp_name']))),
        "--{$boundary}--"
    );

    mail($to, $subject, implode("\r\n", $message), implode("\r\n", $headers));
}

?>

我试图改变

echo            "<label for='file'>File</label>";
echo            "<input type='file' name='file' id='file'>";

$file = "getVcard.php?CompanyID=" . $CompanyID;

生成vcard,但是当我这样做时,电子邮件不会发送。

有什么建议吗?

谢谢,

CampSoup1988


这是我的getVcard.php文件:

<?php
include "connection.php";
$CompanyID = $_GET['CompanyID'];
$CompanyID=$_GET['CompanyID']; // Collecting data from query string
if(!is_numeric($CompanyID)){ // Checking data it is a number or not
    echo "Data Error"; 
exit;
}
$query = "SELECT * FROM company where CompanyID = $CompanyID";
$result = mysql_query($query) or die(mysql_error());
header('Content-Type: text/vcard');
header('Content-Disposition: attachment; filename=vcardexport.vcf');
        while($ResultsLists = mysql_fetch_array($result)){  
echo                "BEGIN:VCARD\n";
echo                "VERSION:4.0\n";
echo                "KIND:org\n";
echo                "FN:" . $ResultsLists['CompanyName'] . "\n";
echo                "ORG:" . $ResultsLists['CompanyName'] . "\n";
echo                "ADR;TYPE=work;LABEL=\"". $ResultsLists['CompanyAddress1'] . "\n" . $ResultsLists['CompanyAddress2'] . "\n". $ResultsLists['CompanyCity'] . "\, " . $ResultsLists['CompanyState'] . " " . $ResultsLists['CompanyZipcode'] . "\n" . $ResultsLists['CompanyCountry'] . "\":" .  $ResultsLists['CompanyAddress2'] . ";" . $ResultsLists['CompanyAddress1'] . ";" . $ResultsLists['CompanyCity'] . ";" . $ResultsLists['CompanyState'] . ";" . $ResultsLists['CompanyZipcode'] . ";" . $ResultsLists['CompanyCountry'] . "\n";
                if($ResultsLists['CompanyPhone'] != ""){
echo                    "TEL;TYPE=\"work,voice\";VALUE=uri:tel:" . $ResultsLists['CompanyPhone'] . "\n";
                }
                if($ResultsLists['CompanyEmail'] != ""){
echo                    "WORK.EMAIL:" . $ResultsLists['CompanyEmail'] . "\n";
                }
echo                "WORK.LANG;PREF=1:en\n";
                if($ResultsLists['CompanyNotes'] != ""){
echo                    "NOTE:" . $ResultsLists['CompanyNotes'] . "\n";
                }
                if($ResultsLists['CompanyWebsite'] != ""){
echo                    "URL:" . $ResultsLists['CompanyWebsite'] . "\n";
                }
echo                "END:VCARD";
?>

UPDATE:是否可以将vcf文件保存到临时位置,然后自动将临时文件附加到电子邮件中(在将临时文件附加到电子邮件之前重命名临时文件)

1 个答案:

答案 0 :(得分:1)

最简单的解决方案是手动传递vcard的数据:

$file = array(
              'type' => 'text/plain',
              'name' => 'vcard.txt',
              'tmp_name' => 'http://yourdomain/getVcard.php?CompanyID=' . $CompanyID
        );

但这是一个非常肮脏的解决方案。从长远来看,最好使用像phpmailer这样的类。