How to add attachment in php mail

To add an attachment to a PHP mail, you can use the mail() function and specify the attachment file using the @ symbol. Here's an example:

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

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

// 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=="';

// Create the email body
$body = "This is a test email with an attachment.\r\n\r\n";
$body.= "--===============1234567890==\r\n";
$body.= "Content-Type: application/pdf; name=\"$attachment\"\r\n";
$body.= "Content-Disposition: attachment; filename=\"$attachment\"\r\n";
$body.= "Content-Transfer-Encoding: base64\r\n";
$body.= "\r\n";
$body.= chunk_split(base64_encode(file_get_contents($attachment)), 76, "\r\n ");
$body.= "--===============1234567890==--";

// Send the email
mail($to, $subject, $body, $headers);
?>

In this example, we're using the mail() function to send an email to the recipient. We're specifying the attachment file using the @ symbol, and setting the email headers to include the MIME type and boundary.

We're also creating the email body by concatenating several strings. The first part of the body is the plain text message. The second part is the attachment, which is encoded in base64 and split into chunks of 76 characters each.

Finally, we're sending the email using the mail() function.

Note that this is just one way to add an attachment to a PHP mail. There are many other libraries and methods you can use, such as using a PHPMailer class or using a third-party library like SwiftMailer.

Also, make sure to check the email server settings and configuration to ensure that the attachment is being sent correctly.

Here is another example using PHPMailer library:

<?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 = '[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 = 'Test email with attachment';
$mail->Body = 'This is a test email with an attachment.';

$mail->addAttachment('path/to/attachment/file.pdf', 'application/pdf', 'base64', 'attachment; filename="file.pdf"');

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

In this example, we're using the PHPMailer library to send an email with an attachment. We're specifying the attachment file using the addAttachment() method, and setting the email headers and body using the setFrom(), addAddress(), Subject, and Body methods.