问题
I'm working on file attachment here mail function is working fine I'm getting all fields through mail accept file upload field is not coming. I have tried using Content-Type: multipart/mixed
and some other methods but unable to achieve the desired output. I have servey and find the different answer and tried but still facing the same issue. Can anyone suggest to me according to my script how should I get the file attachment.
HTML
<input id="file-upload" name="upload" type="file" required>
PHP mail function
<?php
// Receiver mail id
$mail_to = 'yourmail@gmail.com';
// Mail Subject
$subject = 'title';
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($_POST['upload'])) {
$upload = $_POST['upload'];
}
// Message body
$msg = '<html><body><p>';
$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($_POST["upload"] != "") {
$msg .= '<b> Upload : </b>' . $upload . '<br/>';
}
$msg .= '</p>';
$msg .= '</body></html>';
// Mail headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: yourmail@gmail.com' . "\r\n";
if( mail( $mail_to, $subject, $msg, $headers )) {
echo "Thank You!";
} else {
die("Error!");
}
}
?>
I had tried like this here only file coming other fields not coming in the mail. what I'm missing here.
<?php
// Receiver mail id
$mail_to = 'yourmail@gmail.com';
// Mail Subject
$subject = 'project';
$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($_POST['upload'])) {
$upload = $_POST['upload'];
}
// Message body
$msg = '<html><body><p>';
$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($_POST["upload"] != "") {
$msg .= '<b> Upload : </b>' . $upload . '<br/>';
}
$msg .= '</p>';
$msg .= '</body></html>';
$file = $path.$filename;
$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$uploadname = basename($file);
$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!");
}
}
?>
回答1:
You can send like this.
$file_type = 'pdf';
$file_name = 'invoice.pdf';
$encoded_content = chunk_split(base64_encode($document));
$boundary = md5("random"); // define boundary with a md5 hashed value
//header
$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
$headers .= "From: Fr. Muller\r\n"; // Sender Email
$headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type
$headers .= "boundary = $boundary\r\n"; //Defining the Boundary
//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";
//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; // Attaching the encoded file with email
mail($to_email, $subject, $body, $headers);
回答2:
you have to use the $_FILES tag to get the uploaded file instead of post tag
$_FILES["upload"]["tmp_name"]
below link help you on the same
https://www.w3schools.com/php/php_file_upload.asp
for sending the attachment you can use the PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject = $subject;
$email->Body = $msg;
$email->AddAddress( $mail_to );
$file_to_attach = $_FILES["upload"]["tmp_name"]
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();
Send attachments with PHP Mail()?
来源:https://stackoverflow.com/questions/57743898/php-send-mail-how-to-send-mail-with-file-attachment