我想在我的网站上有一个联系表格,可以选择让访客发送附件及其信息。我试过但不能成功。我遇到的问题是文件被发送到电子邮件,但表格数据和电子邮件主题不与附件一起发送。
HTML:
<form name="form1" enctype="multipart/form-data" method="post" action="do_send.php">
<label>
<input type="text" name="name" id="name" />
</label>
<label>
<input type="text" name="age" id="age" />
</label>
<label>
<input type="email" name="email" id="email" />
</label>
<label>
<input type="file" name="my_file" />
</label>
<label>
<input type="submit" name="button" value="Submit" />
</label>
</form>
do_send.php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
if($_POST && isset($_FILES['my_file']))
{
//get file details we need
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
# Mail headers should work with most clients (including thunderbird)
$headers = "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "From:".$email."\r\n";
$headers .= "Subject:".$subject."\r\n";
$headers .= "Reply-To: ".$email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n";
$headers .= $MESSAGE_BODY."\r\n\r\n";
$headers .= "--".md5('boundary2')."--\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: ".$file_type."; ";
$headers .= "name=\"".$file_name."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file_name."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $encoded_content."\r\n";
$headers .= "--".md5('boundary1')."--";
if ($_POST["email"]!='') {
$ToEmail = 'info@mywebiste.com';
$EmailSubject = 'Website contact form';
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br><br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Age: ".$_POST["age"]."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $headers) or die ("Failure");
echo "<script> alert('Messgae successfully sent!');
window.location='index.html'</script>";
return true;
} else{
echo "<script> alert('Temporary problem, try again!');
window.location='index.html'</script>";
}
}
?>
请指出我在做错的地方。我在学习阶段请帮我解决这个问题。请指导我在do_send.php中的修改位置,以便表格数据和电子邮件主题也随附件一起发送。
答案 0 :(得分:0)
<?php
$nom = $_POST['lastName'];
$prenom = $_POST['firstName'];
$from_email = $_POST['email']; //sender email
if($_POST && isset($_FILES['my_file']))
{
$recipient_email = 'youremail@exemple.com'; //recipient email
$subject = 'Inscription Etudiant'; //subject of email
$message = 'This is body of the message'; //message body
$message = 'Nom: '.$nom."\n";
$message .= 'Prenom: '.$prenom."\n";
$message .= 'Email: '.$from_email;
//get file details we need
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("YADIStudio");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$user_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.php';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to youremail@exemple.com');
window.location = 'index.php';
</script>
<?php
}
}
?>
<--! YADI Studio Agency Copyright 2015 -->