Phpmailer attachment

PHPMailer is a popular PHP class used to send emails. Here's an example of how to use PHPMailer to send an email with an attachment:

Step 1: Install PHPMailer

You can install PHPMailer using Composer:

composer require phpmailer/phpmailer

Step 2: Create a PHPMailer instance

Create a new instance of the PHPMailer class:

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

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

Step 3: Set up the email

Set the sender's email address, recipient's email address, and the subject of the email:

$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test Email with Attachment';

Step 4: Add the attachment

Use the addAttachment() method to add the attachment to the email:

$mail->addAttachment('path/to/attachment.pdf', 'application/pdf', 'attachment', 'Test Attachment');

The parameters are:

Step 5: Set the email body

You can set the email body using the Body property:

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

Step 6: Send the email

Use the send() method to send the email:

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

Full example code

Here's the full example code:

<?php

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

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

$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test Email with Attachment';

$mail->addAttachment('path/to/attachment.pdf', 'application/pdf', 'attachment', 'Test Attachment');

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

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

?>

Make sure to replace path/to/attachment.pdf with the actual path to the attachment file you want to send.