我有一个简单的脚本,用于发送带有用户上传附件的电子邮件。如何修改脚本,以便通过电子邮件发送在 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("\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>";
?>
邮件.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;
生成电子名片,但是当我这样做时,电子邮件不会发送。
有什么建议么?
谢谢,
营汤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: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";
?>
更新: 是否可以将 vcf 文件保存到临时位置,然后自动将临时文件附加到电子邮件(在将临时文件附加到电子邮件之前重命名)