Attach pdf in php mail body

To attach a PDF file to a PHP mail body, you can use the mail() function and specify the attachment using the --attachment option. Here's an example:

<?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="This is the boundary string"';

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

// Set the attachment
$attachment = 'path/to/your/pdf/file.pdf';

// 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 file
$data = chunk_split(base64_encode($data));

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

// Send the email
mail('[email protected]', 'Subject', $body, $headers, $data);
?>

In this example, we set the email headers to include the MIME-Version and Content-Type headers, which specify that the email body is a multipart message with a boundary string. We then set the email body and attachment file path.

We open the attachment file, read its contents, and encode it using base64. We then set the attachment headers, which include the Content-Type, Content-Disposition, and Content-Transfer-Encoding headers.

Finally, we send the email using the mail() function, passing in the recipient email address, subject, email body, headers, and attachment data.

Note that this is just one way to attach a PDF file to a PHP mail body. There are other libraries and methods you can use, such as PHPMailer or SwiftMailer, which provide more advanced features and flexibility.