Làm thế nào để PHP. Gửi tệp đính kèm với email có ví dụ

Bài viết này dành cho bạn nếu bạn đang tìm kiếm câu trả lời cho câu hỏi Làm thế nào để PHP. Gửi tệp đính kèm cùng với email có ví dụ . Tôi sẽ cung cấp cho bạn một ví dụ 1 để giúp bạn giải quyết vấn đề này.

Làm thế nào để PHP. Gửi tệp đính kèm với email có ví dụ - PHP. Gửi tệp đính kèm bằng email
if($_POST['button'] && isset($_FILES['attachment']))
{
 
    $from_email         = '[email protected]'; //from mail, sender email address
    $recipient_email    = '[email protected]'; //recipient email address
     
    //Load POST data from HTML form
    $sender_name    = $_POST["sender_name"] //sender name
    $reply_to_email = $_POST["sender_email"] //sender email, it will be used in "reply-to" header
    $subject        = $_POST["subject"] //subject for the email
    $message        = $_POST["message"] //body of the email
     
 
    /*Always remember to validate the form fields like this
    if(strlen($sender_name)<1)
    {
        die('Name is too short or empty!');
    }
    */
     
    //Get uploaded file data using $_FILES array
    $tmp_name    = $_FILES['my_file']['tmp_name']; // get the temporary file name of the file on the server
    $name        = $_FILES['my_file']['name'];  // get the name of the file
    $size        = $_FILES['my_file']['size'];  // get size of the file for size validation
    $type        = $_FILES['my_file']['type'];  // get type of the file
    $error       = $_FILES['my_file']['error']; // get the error (if any)
 
    //validate form field for attaching the file
    if($file_error > 0)
    {
        die('Upload error or No files uploaded');
    }
 
    //read from the uploaded file & base64_encode content
    $handle = fopen($tmp_name, "r");  // set the file handle only for reading the file
    $content = fread($handle, $size); // reading the file
    fclose($handle);                  // close upon completion
 
    $encoded_content = chunk_split(base64_encode($content));
 
    $boundary = md5("random"); // define boundary with a md5 hashed value
 
    //header
    $headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
    $headers .= "From:".$from_email."\r\n"; // Sender Email
    $headers .= "Reply-To: ".$reply_to_email."\r\n"; // Email address to reach back
    $headers .= "Content-Type: multipart/mixed;"; // 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";
    $body .= chunk_split(base64_encode($message));
         
    //attachment
    $body .= "--$boundary\r\n";
    $body .="Content-Type: $type; name=".$name."\r\n";
    $body .="Content-Disposition: attachment; filename=".$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
     
    $sentMailResult = mail($recipient_email, $subject, $body, $headers);
 
    if($sentMailResult )
    {
       echo "File Sent Successfully.";
       unlink($name); // delete the file after attachment sent.
    }
    else
    {
       die("Sorry but the email could not be sent.
                    Please go back and try again!");
    }
}

Nếu bạn chưa tìm thấy câu trả lời cho câu hỏi của mình. Vui lòng xem thêm các mục bên dưới xoay quanh How to PHP. Gửi tệp đính kèm với email có ví dụ. Hoặc để lại câu hỏi cho chúng tôi

Làm cách nào để gửi email có tệp đính kèm?

Chuyển tiếp email dưới dạng tệp đính kèm .
Trên máy tính của bạn, hãy truy cập Gmail
Chọn các email mà bạn muốn
Nhấp vào Thêm. Chuyển tiếp dưới dạng tệp đính kèm
Trong trường “Tới”, thêm người nhận. Bạn cũng có thể thêm người nhận trong trường “Cc” và “Bcc”
Thêm một môn học
Viết tin nhắn của bạn
Ở dưới cùng, hãy nhấp vào Gửi

Làm cách nào để gửi tệp Excel dưới dạng tệp đính kèm trong email bằng PHP?

xls" Bố cục nội dung. tệp đính kèm $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent".

Tệp đính kèm PHP là gì?

Trang đính kèm ( attachment. php ) là một trang bài đăng có loại bài đăng đính kèm, được tạo thông qua việc tạo tệp đính kèm. php . Cũng giống như một trang bài đăng dành riêng cho bài viết của bạn, trang đính kèm cung cấp một trang dành riêng trong các tệp đính kèm trong chủ đề của bạn.