PHP的发送邮件如何发送带有文件附件的邮件

时间:2019-09-01 07:07:55

标签: javascript php email phpmailer

我正在处理文件附件,这里的邮件功能正常工作我正在通过邮件获取所有字段,而接受文件上传字段却没有。我尝试使用Content-Type: multipart/mixed和其他方法,但无法实现所需的输出。我有服务,找到不同的答案,并尝试过,但仍然面临相同的问题。谁能根据我的脚本向我建议如何获得文件附件。

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!");
    }
}
?>

我曾尝试过这样,这里仅文件来自其他字段而不是邮件中。我在这里想念的东西。

<?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!");
    }
}
 ?>

2 个答案:

答案 0 :(得分:0)

您可以这样发送。

        $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);

答案 1 :(得分:0)

您必须使用 $ _ FILES 标签来获取上传的文件,而不是post标签

  

$_FILES["upload"]["tmp_name"]

下面的链接可帮助您保持一致

https://www.w3schools.com/php/php_file_upload.asp

要发送附件,您可以使用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()?