>我有一个简单的脚本,用于发送带有用户上传的附件的电子邮件。 如何修改脚本,以便它通过电子邮件发送以 php 生成的文件(例如,在这种情况下,我想从数据库中提取联系信息,生成该文件的 vCard 并将其附加到电子邮件中。
查看公司.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("rn", $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>";
?>
邮件.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("rn", $message), implode("rn", $headers));
}
?>
我试图改变
echo "<label for='file'>File</label>";
echo "<input type='file' name='file' id='file'>";
自
$file = "getVcard.php?CompanyID=" . $CompanyID;
生成 vcard,但当我这样做时,电子邮件不会发送。
有什么建议吗?
谢谢
营汤1988
这是我的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:VCARDn";
echo "VERSION:4.0n";
echo "KIND:orgn";
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:enn";
if($ResultsLists['CompanyNotes'] != ""){
echo "NOTE:" . $ResultsLists['CompanyNotes'] . "n";
}
if($ResultsLists['CompanyWebsite'] != ""){
echo "URL:" . $ResultsLists['CompanyWebsite'] . "n";
}
echo "END:VCARD";
?>
更新:是否可以将 vcf 文件保存到临时位置,然后自动将临时文件附加到电子邮件(在将临时文件附加到电子邮件之前重命名临时文件)
最简单的解决方案是手动传递电子名片的数据:
$file = array(
'type' => 'text/plain',
'name' => 'vcard.txt',
'tmp_name' => 'http://yourdomain/getVcard.php?CompanyID=' . $CompanyID
);
但这是一个非常肮脏的解决方案。从长远来看,最好使用像phpmailer这样的类。