问题
I have a simple script for sending an email with an attachment that the user uploads. How do I modify the script so that it would email a file that is generated in php (for an example, in this case, I want to pull the contact information from the database, generate a vCard of the file and have it attached to the email.
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));
}
?>
I tried to change
echo "<label for='file'>File</label>";
echo "<input type='file' name='file' id='file'>";
to
$file = "getVcard.php?CompanyID=" . $CompanyID;
to generate the vcard, but when I do this, the email does not send.
Any suggestions?
Thank you,
CampSoup1988
This is my getVcard.php file:
<?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: Is it possible to save the vcf file to a temp location and then automatically attaching the temp file to an email (renaming the temp file before attaching it to the email)
回答1:
Easiest solution would be to pass the data of your vcard by hand:
$file = array(
'type' => 'text/plain',
'name' => 'vcard.txt',
'tmp_name' => 'http://yourdomain/getVcard.php?CompanyID=' . $CompanyID
);
But that is a very dirty solution. In the long run it will be better to use a class like phpmailer.
来源:https://stackoverflow.com/questions/10875464/send-email-in-php-with-attached-file-that-is-generated-from-database