发送电子邮件在PHP与从数据库生成的附加文件(Send email in php with atta

2019-09-17 18:06发布

我有一个与附件用户上传发送电子邮件的简单脚本。 如何修改脚本,以便它将电子邮件是在PHP中产生(对于一个例子的文件,在这种情况下,我想拉从数据库中的联系人信息,生成文件的电子名片,并将它连接到电子邮件。

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;

生成电子名片,但我这样做时,邮件不发送。

有什么建议么?

谢谢,

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文件保存到一个临时位置,然后自动将临时文件附加到电子邮件(重命名该临时文件将其附加到电子邮件之前)

Answer 1:

最简单的解决办法是用手来传递你的电子名片的数据:

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

但是,这是一个非常肮脏的解决方案。 从长远来看,这将是更好地使用一类像PHPMailer的 。



文章来源: Send email in php with attached file that is generated from database