Php 将上载的文件作为附件发送到电子邮件
我是网页设计的新手。我需要用HTML创建一个表单,这样它会要求用户输入几个字段并上传他/她的简历。当他提交表格时,他提交的内容应与他的简历一起通过电子邮件发送给我,作为电子邮件的附件。 我使用PHP发送电子邮件。一切正常,除了文件没有与发送的电子邮件连接 我张贴的HTML和PHP代码,请帮助我 HTML代码:FileName:Careers.HTML PHP代码:FileName:Careers.PHPPhp 将上载的文件作为附件发送到电子邮件,php,html,forms,email,attachment,Php,Html,Forms,Email,Attachment,我是网页设计的新手。我需要用HTML创建一个表单,这样它会要求用户输入几个字段并上传他/她的简历。当他提交表格时,他提交的内容应与他的简历一起通过电子邮件发送给我,作为电子邮件的附件。 我使用PHP发送电子邮件。一切正常,除了文件没有与发送的电子邮件连接 我张贴的HTML和PHP代码,请帮助我 HTML代码:FileName:Careers.HTML PHP代码:FileName:Careers.PHP 你没有作为附件传递任何内容 在这里,我粘贴一个片段,希望这将帮助你 <?php
你没有作为附件传递任何内容 在这里,我粘贴一个片段,希望这将帮助你
<?php
$fileatt = "mypdffile.pdf"; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "mypdffile.pdf"; // Filename that will be used for the file as the attachment
$email_from = "sales@mysite.com"; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_message = "Thanks for visiting mysite.com! Here is your free file.
";
$email_message .= "Thanks for visiting.
"; // Message that the email has in it
$email_to = $_POST['email']; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "You file has been sent
to the email address you specified.
Make sure to check your junk mail!
Click here to return to mysite.com.";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
你没有作为附件传递任何内容 在这里,我粘贴一个片段,希望这将帮助你
<?php
$fileatt = "mypdffile.pdf"; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "mypdffile.pdf"; // Filename that will be used for the file as the attachment
$email_from = "sales@mysite.com"; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_message = "Thanks for visiting mysite.com! Here is your free file.
";
$email_message .= "Thanks for visiting.
"; // Message that the email has in it
$email_to = $_POST['email']; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "You file has been sent
to the email address you specified.
Make sure to check your junk mail!
Click here to return to mysite.com.";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
如上所述,PHP部件中没有处理任何附件。
这可能有助于您逐个完成这些步骤。如上所述,您的PHP部件中没有处理任何附件。
这可能有助于您逐个完成这些步骤。此功能将上传文件作为附件发送。有关html代码和分步说明,请参阅教程
此功能将上传文件作为附件发送。有关html代码和分步说明,请参阅教程
感谢分享这样一段好代码: 只要稍加修改,这就是真正的工作代码。 创建HTML文件: 上传文件并作为电子邮件附件发送 姓名: 电邮: 选择要上载的文件: 同一个文件夹生成一个名为mail_sender.php的php文件
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$name_of_uploaded_file =basename($_FILES['uploaded_file']['name']);
$formData = $_POST;
getFile( $name_of_uploaded_file, $formData );
function getFile( $filename , $formData ) {
$allowedExts = array("csv","pdf","jpg","png","JPG","PNG","jpeg","JPEG");
$temp = explode(".", $_FILES["uploaded_file"]["name"]);
$extension = end($temp);
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv','application/jpg','image/jpg', 'image/jpeg', 'image/png','image/gif');
if (in_array($_FILES['uploaded_file']['type'],$mimes )
&& ($_FILES["uploaded_file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["uploaded_file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["uploaded_file"]["error"] . "<br>";
}
else
{
sendMailAsAttachment($_FILES["uploaded_file"]["tmp_name"],$_FILES["uploaded_file"]["name"],$formData);
}
}
else
{
echo "Invalid file " . $extension . " {} " . $_FILES['uploaded_file']['type'];
//echo in_array($_FILES['uploaded_file']['type'],$mimes );
}
}
//This function accepts post data on form submissions and prepare the email message from the form data.
function prepareEmail( $formData ) {
// email fields: to, from, subject, and so on
$to = "solimankhulna@gmail.com";
$from = "solimankhulna@solimankhulna.com";
$subject ="";
$message = "Uploaded File\n";
$message .= "Name :". $formData['name']."\n";
$message .= "Email Address :". $formData['email']."\n";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
$emailData = array (
'to' => $to,
'from' => $from,
'subject' => $subject,
'headers' => $headers,
'message' => $message
);
return $emailData;
}
function prepareAttachment( $filename ,$fileorgname) {
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$attachContent = '';
$file = fopen($filename,"rb");
$data = fread($file,filesize($filename));
fclose($file);
$cvData = chunk_split(base64_encode($data));
$attachContent .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$fileorgname\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$fileorgname\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $cvData . "\n\n";
$attachContent .= "--{$mime_boundary}\n";
return $attachContent;
}
function sendMailAsAttachment( $filename, $fileorgname, $formData ) {
$emailData = prepareEmail( $formData );
$attachContent = prepareAttachment( $filename,$fileorgname );
$message = $emailData['message'].$attachContent;
$ok = @mail($emailData['to'], $emailData['subject'], $message, $emailData['headers']);
if ($ok) {
echo "<p>mail sent to " . $emailData['to'] . "!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
}
?>
感谢分享这样一段好代码: 只要稍加修改,这就是真正的工作代码。 创建HTML文件: 上传文件并作为电子邮件附件发送 姓名: 电邮: 选择要上载的文件: 同一个文件夹生成一个名为mail_sender.php的php文件
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$name_of_uploaded_file =basename($_FILES['uploaded_file']['name']);
$formData = $_POST;
getFile( $name_of_uploaded_file, $formData );
function getFile( $filename , $formData ) {
$allowedExts = array("csv","pdf","jpg","png","JPG","PNG","jpeg","JPEG");
$temp = explode(".", $_FILES["uploaded_file"]["name"]);
$extension = end($temp);
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv','application/jpg','image/jpg', 'image/jpeg', 'image/png','image/gif');
if (in_array($_FILES['uploaded_file']['type'],$mimes )
&& ($_FILES["uploaded_file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["uploaded_file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["uploaded_file"]["error"] . "<br>";
}
else
{
sendMailAsAttachment($_FILES["uploaded_file"]["tmp_name"],$_FILES["uploaded_file"]["name"],$formData);
}
}
else
{
echo "Invalid file " . $extension . " {} " . $_FILES['uploaded_file']['type'];
//echo in_array($_FILES['uploaded_file']['type'],$mimes );
}
}
//This function accepts post data on form submissions and prepare the email message from the form data.
function prepareEmail( $formData ) {
// email fields: to, from, subject, and so on
$to = "solimankhulna@gmail.com";
$from = "solimankhulna@solimankhulna.com";
$subject ="";
$message = "Uploaded File\n";
$message .= "Name :". $formData['name']."\n";
$message .= "Email Address :". $formData['email']."\n";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
$emailData = array (
'to' => $to,
'from' => $from,
'subject' => $subject,
'headers' => $headers,
'message' => $message
);
return $emailData;
}
function prepareAttachment( $filename ,$fileorgname) {
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$attachContent = '';
$file = fopen($filename,"rb");
$data = fread($file,filesize($filename));
fclose($file);
$cvData = chunk_split(base64_encode($data));
$attachContent .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$fileorgname\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$fileorgname\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $cvData . "\n\n";
$attachContent .= "--{$mime_boundary}\n";
return $attachContent;
}
function sendMailAsAttachment( $filename, $fileorgname, $formData ) {
$emailData = prepareEmail( $formData );
$attachContent = prepareAttachment( $filename,$fileorgname );
$message = $emailData['message'].$attachContent;
$ok = @mail($emailData['to'], $emailData['subject'], $message, $emailData['headers']);
if ($ok) {
echo "<p>mail sent to " . $emailData['to'] . "!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
}
?>
我在你的代码中没有看到任何可以处理文件上传的东西。有什么好处?你有没有阅读过关于如何实现这一点的教程?试试这个链接,这或多或少都是同一个问题,我在你的代码中没有看到任何可以处理文件上传的内容。有什么好处?你有没有读过关于如何让它工作的教程?试试这个链接,或多或少都是相同的问题
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$name_of_uploaded_file =basename($_FILES['uploaded_file']['name']);
$formData = $_POST;
getFile( $name_of_uploaded_file, $formData );
function getFile( $filename , $formData ) {
$allowedExts = array("csv","pdf","jpg","png","JPG","PNG","jpeg","JPEG");
$temp = explode(".", $_FILES["uploaded_file"]["name"]);
$extension = end($temp);
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv','application/jpg','image/jpg', 'image/jpeg', 'image/png','image/gif');
if (in_array($_FILES['uploaded_file']['type'],$mimes )
&& ($_FILES["uploaded_file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["uploaded_file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["uploaded_file"]["error"] . "<br>";
}
else
{
sendMailAsAttachment($_FILES["uploaded_file"]["tmp_name"],$_FILES["uploaded_file"]["name"],$formData);
}
}
else
{
echo "Invalid file " . $extension . " {} " . $_FILES['uploaded_file']['type'];
//echo in_array($_FILES['uploaded_file']['type'],$mimes );
}
}
//This function accepts post data on form submissions and prepare the email message from the form data.
function prepareEmail( $formData ) {
// email fields: to, from, subject, and so on
$to = "solimankhulna@gmail.com";
$from = "solimankhulna@solimankhulna.com";
$subject ="";
$message = "Uploaded File\n";
$message .= "Name :". $formData['name']."\n";
$message .= "Email Address :". $formData['email']."\n";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
$emailData = array (
'to' => $to,
'from' => $from,
'subject' => $subject,
'headers' => $headers,
'message' => $message
);
return $emailData;
}
function prepareAttachment( $filename ,$fileorgname) {
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$attachContent = '';
$file = fopen($filename,"rb");
$data = fread($file,filesize($filename));
fclose($file);
$cvData = chunk_split(base64_encode($data));
$attachContent .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$fileorgname\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$fileorgname\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $cvData . "\n\n";
$attachContent .= "--{$mime_boundary}\n";
return $attachContent;
}
function sendMailAsAttachment( $filename, $fileorgname, $formData ) {
$emailData = prepareEmail( $formData );
$attachContent = prepareAttachment( $filename,$fileorgname );
$message = $emailData['message'].$attachContent;
$ok = @mail($emailData['to'], $emailData['subject'], $message, $emailData['headers']);
if ($ok) {
echo "<p>mail sent to " . $emailData['to'] . "!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
}
?>