How to attach file in php mail function

To attach a file to a PHP mail function, you can use the addAttachment() method of the mail() function. Here is an example:

<?php

$to = '[email protected]';
$subject = 'Test email with attachment';
$message = 'This is a test email with an attachment.';

// Set the file path and name
$file_path = 'path/to/file.txt';
$file_name = 'file.txt';

// Set the email headers
$headers = array(
    'From' => '[email protected]',
    'Subject' => $subject,
    'Content-Type' => 'text/plain; charset=UTF-8'
);

// Set the email body
$body = $message;

// Attach the file
$attachment = chunk_split(base64_encode(file_get_contents($file_path)), 76);
$headers.= "\nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"_boundary\"\n";
$headers.= "\n--_boundary\nContent-Type: text/plain; charset=UTF-8\n\n". $body;
$headers.= "\n--_boundary\nContent-Type: application/octet-stream; name=\"$file_name\"\n".
             "Content-Disposition: attachment; filename=\"$file_name\"\n\n". $attachment.
             "\n--_boundary--";

// Send the email
mail($to, $subject, '', $headers);

?>

In this example, we first set the email headers, body, and attachment file path and name. We then use the chunk_split() and base64_encode() functions to encode the file contents and split it into chunks of 76 characters each. We then set the email headers to include the attachment information, and finally send the email using the mail() function.

Note that this is just one way to attach a file to a PHP mail function, and there are many other ways to do it. Also, this example assumes that the file is a text file, if you want to attach a binary file, you need to use a different approach.

Also, you can use the mime_content_type() function to set the correct MIME type for the attachment, for example:

$headers.= "\n--_boundary\nContent-Type: ". mime_content_type($file_path). "; name=\"$file_name\"\n".
             "Content-Disposition: attachment; filename=\"$file_name\"\n\n". $attachment.
             "\n--_boundary--";

This will set the correct MIME type for the attachment based on the file extension.

You can also use the PHPMailer library which is a popular and easy to use library for sending emails in PHP, it has built-in support for attaching files.

<?php

require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';

$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');

$mail->Subject = 'Test email with attachment';
$mail->Body = 'This is a test email with an attachment.';

$mail->addAttachment('path/to/file.txt', 'file.txt');

if (!$mail->send()) {
    echo 'Error: '. $mail->ErrorInfo;
} else {
    echo 'Email sent successfully';
}

?>

This is just a basic example, you can customize the settings and options to fit your needs.