【问题标题】:php send mail how to send mail with file attachment [duplicate]php发送邮件如何发送带有文件附件的邮件[重复]
【发布时间】:2020-01-04 17:18:08
【问题描述】:

我正在处理文件附件,邮件功能工作正常我正在通过邮件获取所有字段接受文件上传字段未到来。我曾尝试使用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!");
    }
}
 ?>

【问题讨论】:

    标签: javascript php email phpmailer


    【解决方案1】:

    你可以这样发送。

            $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】:

      你必须使用 $_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()?

      【讨论】:

      • 你正在使用 PHPMailer 我正在开发 php 发送邮件功能。你能根据我的剧本提出建议吗?
      猜你喜欢
      • 2023-03-21
      • 2017-07-31
      • 2016-05-03
      • 2012-04-21
      • 2017-09-06
      • 2012-09-09
      • 2012-06-15
      相关资源
      最近更新 更多