How to send attechment in mail through php

To send an attachment in an email using PHP, you can use the mail() function or a PHP email library such as PHPMailer. Here's an example of how to do it using the mail() function:

<?php

// Set the email headers
$headers = 'From: [email protected]'. "\r\n".
           'Reply-To: [email protected]'. "\r\n".
           'MIME-Version: 1.0'. "\r\n".
           'Content-Type: multipart/mixed; boundary="===============1234567890=="';

// Set the email body
$body = 'This is the email body';

// Set the attachment
$attachment = 'path/to/attachment.txt';

// Open the attachment file
$fp = fopen($attachment, 'rb');

// Read the attachment file
$data = fread($fp, filesize($attachment));

// Close the attachment file
fclose($fp);

// Encode the attachment
$data = chunk_split(base64_encode($data));

// Set the attachment headers
$headers.= "\r\n".
           'Content-Type: application/octet-stream; name="'. basename($attachment). '"'. "\r\n".
           'Content-Disposition: attachment; filename="'. basename($attachment). '"'. "\r\n".
           'Content-Transfer-Encoding: base64'. "\r\n".
           'Content-Length: '. strlen($data). "\r\n";

// Set the email message
$message = "This is the email message\r\n\r\n".
           $body. "\r\n\r\n".
           '--===============1234567890==--'. "\r\n".
           'Content-Type: application/octet-stream; name="'. basename($attachment). '"'. "\r\n".
           'Content-Disposition: attachment; filename="'. basename($attachment). '"'. "\r\n".
           'Content-Transfer-Encoding: base64'. "\r\n".
           $data. "\r\n".
           '--===============1234567890==--';

// Send the email
mail('[email protected]', 'Subject', $message, $headers);

?>

This code sets the email headers, body, and attachment, and then sends the email using the mail() function.

Alternatively, you can use a PHP email library such as PHPMailer, which provides a more convenient and flexible way to send emails with attachments. Here's an example of how to do it using PHPMailer:

<?php

require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

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

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your-password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

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

$mail->Subject = 'Subject';
$mail->Body = 'This is the email body';

$mail->addAttachment('path/to/attachment.txt', 'text/plain', 'attachment', 'attachment.txt');

$mail->send();

?>

This code sets up a PHPMailer object, sets the email headers and body, and adds an attachment using the addAttachment() method. Finally, it sends the email using the send() method.

Note that in both examples, you'll need to replace the placeholders ([email protected], your-password, [email protected], etc.) with your actual email credentials and recipient information.