PHP的发送邮件如何发送带有文件附件的邮件 [英] php send mail how to send mail with file attachment
问题描述
我正在处理文件附件,这里的邮件功能正常工作,我正在通过邮件获取所有字段,而接受文件上传字段却没有.我尝试使用Content-Type: multipart/mixed
和其他方法,但无法实现所需的输出.我有服务,找到不同的答案,并尝试过,但仍然面临着同样的问题.有人可以根据我的脚本向我建议如何获取文件附件.
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邮件功能
<?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!");
}
}
?>
推荐答案
您必须使用 $ _ FILES 标签来获取上传的文件而不是post标签
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
要发送附件,您可以使用PHPMailer
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();
这篇关于PHP的发送邮件如何发送带有文件附件的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!