问题描述
我正在这里处理文件附件.邮件功能运行正常,但文件字段为空.我尝试使用Content-Type: multipart/mixed
和其他方法,但无法实现所需的输出.我已经搜索了不同的答案并尝试过,但是仍然面临着同样的问题.似乎有重复,但是我尝试了所有方法,任何人都可以按照我的脚本指导我正确的方向,这是我在这里所缺少的.如何获得文件附件?任何人都可以向我建议正确的方向,如果我删除了文件附件代码,那么其他字段仍然可以正常工作并且收到邮件,为什么其他字段没有出现?HTML
I'm working on file attachment here. The mail function is working fine other than the file field is empty. I have tried using Content-Type: multipart/mixed
and some other methods but unable to achieve the desired output. I have searched for a different answer and tried but am still facing the same issue. There seems to be a duplicate but I had tried all methods can anyone guide me the right direction as per my script what i missing here. How can I get the file attachment? Can anyone suggest me in the right direction what I'm missing here why other fields are not coming if I remove file attachment code then other fields are working fine and mail has come.HTML
<form class="test" action="contactMail.php" method="POST">
<input type="hidden" name="formname" value="Form sent by About page">
<select class="user-select">
<option value="">Pick Job Role</option>
<option>Web Developer</option>
<option>Java Developer</option>
</select>
<input type="text" name="name" placeholder="Your Name">
<input type="tel" name="phone" placeholder="Contact Number">
<label for="file-upload" class="file-upload">Upload Image</label>
<input type="file" id="file-upload" name="upload" required>
<textarea name="message" placeholder="Your Message" rows="3">/textarea>
<button type="submit" class="from-submit form-group form-fields">Submit</button>
<div id="success_contact">
<h2>Request Sent!</h2>
</div>
</form>
`<?php
// Receiver mail id
$mail_to = 'yourmail@gmail.com';
// Mail Subject
$subject = 'Virtual Raasta';
$upload = $_FILES["upload"];
$path = 'assets/file';
$filename = 'myfile';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ( isset($_POST['name']) ) {
$name = $_POST['name'];
}
if (isset($_POST['phone'])) {
$phone = $_POST['phone'];
}
if (isset($_POST['company'])) {
$company = $_POST['company'];
}
if(isset($_POST['message'])) {
$message = $_POST['message'];
}
if(isset($_POST['industry'])) {
$industry = $_POST['industry'];
}
if(isset($_POST['job'])) {
$job = $_POST['job'];
}
if(isset($_FILES['upload'])) {
$upload = $_FILES['upload'];
}
$reqBy = $_POST['formname'];
// Message body
$msg = '<html><body><p>';
$msg .= '<b> Request Sent From : </b>' . $reqBy . '<br/>';
$msg .= '<b> Name : </b>' . $name . '<br/>';
if($_POST["phone"] != "") {
$msg .= '<b> Phone : </b>' . $phone . '<br/>';
}
if($_POST["company"] != "") {
$msg .= '<b> Company : </b>' . $company . '<br/>';
}
if($_POST["message"] != "") {
$msg .= '<b> Message : </b>' . $message . '<br/>';
}
if($_POST["industry"] != "") {
$msg .= '<b> Industry : </b>' . $industry . '<br/>';
}
if($_POST["job"] != "") {
$msg .= '<b> Job Role : </b>' . $job . '<br/>';
}
if($_FILES["upload"] != "") {
$msg .= '<b> Upload : </b>' . $upload . '<br/>';
}
$msg .= '</p>';
$msg .= '</body></html>';
var_dump($msg);
$filename = $_FILES["upload"]["name"];
//$content = file_get_contents( $_FILES['upload']['tmp_name'] );
if(!empty($_FILES['upload']['tmp_name']) && file_exists($_FILES['upload']['tmp_name'])) {
$content= addslashes(file_get_contents($_FILES['upload']['tmp_name']));
}
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$replyto = 'test';
$headers = "From: ".$subject." <".'yourmail@gmail.com'.">\r\n";
$headers .= "Reply-To: ".$replyto."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$msg = "--".$uid."\r\n";
$msg .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$msg .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$msg .= $msg."\r\n\r\n";
$msg .= "--".$uid."\r\n";
$msg .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$msg .= $content."\r\n\r\n";
$msg .= "--".$uid."--";
if( mail( $mail_to, $subject, $msg, $headers )) {
echo "Thank You!";
} else {
die("Error!");
}
}
?>`
推荐答案
我希望这只是一个错字.您使用var $msg
两次.一次用于您的html消息和消息的正文.
I hope it is just a typo. You are using the var $msg
twice. Once for Your html-message and for the body of the message.
<?php
....
// Message body
$msg = '<html><body><p>';
$msg .= '<b> Request Sent From : </b>' . $reqBy . '<br/>';
$msg .= '<b> Name : </b>' . $name . '<br/>';
if($_POST["phone"] != "") {
$msg .= '<b> Phone : </b>' . $phone . '<br/>';
}
if($_POST["company"] != "") {
$msg .= '<b> Company : </b>' . $company . '<br/>';
}
if($_POST["message"] != "") {
$msg .= '<b> Message : </b>' . $message . '<br/>';
}
if($_POST["industry"] != "") {
$msg .= '<b> Industry : </b>' . $industry . '<br/>';
}
if($_POST["job"] != "") {
$msg .= '<b> Job Role : </b>' . $job . '<br/>';
}
if($_FILES["upload"] != "") {
$msg .= '<b> Upload : </b>' . $upload . '<br/>';
}
$msg .= '</p>';
$msg .= '</body></html>';
var_dump($msg);
$filename = $_FILES["upload"]["name"];
//$content = file_get_contents( $_FILES['upload']['tmp_name'] );
if(!empty($_FILES['upload']['tmp_name']) && file_exists($_FILES['upload']['tmp_name'])) {
$content= file_get_contents($_FILES['upload']['tmp_name']); // No add_slashes()
}
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$replyto = 'test';
$headers = "From: ".$subject." <".'yourmail@gmail.com'.">\r\n";
$headers .= "Reply-To: ".$replyto."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
在此处重命名var,并同时更改Content-type和transfer-encoding:
Rename the var here and change Content-type and transfer-encoding as well:
$msgBody = "--".$uid."\r\n";
$msgBody .= "Content-type:text/html; charset=iso-8859-1\r\n";
$msgBody .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msgBody .= $msg."\r\n\r\n";
if(isset($_Files['upload'])) // Only add attachment if uploaded
$msgBody .= "--".$uid."\r\n";
$msgBody .= "Content-Type: ".mime_content_type ( $_FILES['upload']['tmp_name'] )."; name=\"".$filename."\"\r\n";
$msgBody .= "Content-Transfer-Encoding: base64\r\n";
$msgBody .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$msgBody .= $content."\r\n\r\n";
}
$msgBody .= "--".$uid."--";
if( mail( $mail_to, $subject, $msgBody, $headers )) {
echo "Thank You!";
} else {
die("Error!");
}
}
?>
这篇关于附件未使用php mail()函数发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!