Attach cc in mail using php mailer
To attach a file to an email using PHPMailer, you can use the addAttachment()
method. Here's an example:
<?php
require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';
require_once 'PHPMailer/Exception.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]', 'From Name');
$mail->addAddress('[email protected]', 'To Name');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email';
// Attach a file
$mail->addAttachment('path/to/file.pdf', 'application/pdf', 'file.pdf');
if (!$mail->send()) {
echo 'Error: '. $mail->ErrorInfo;
} else {
echo 'Email sent!';
}
?>
In this example, we're attaching a file named file.pdf
located at path/to/file.pdf
to the email. The third argument to addAttachment()
is the file name, which will be used in the email.
You can also specify additional options for the attachment, such as:
encoding
: The encoding to use for the attachment (e.g.base64
,7bit
, etc.)type
: The MIME type of the attachmentdisposition
: The disposition of the attachment (e.g.attachment
,inline
, etc.)
For example:
$mail->addAttachment('path/to/file.pdf', 'application/pdf', 'file.pdf', 'base64', 'attachment');
Make sure to replace path/to/file.pdf
with the actual path to the file you want to attach.
Also, you can use addStringAttachment()
method to attach a string as an attachment, it's useful when you want to send a file that is generated dynamically.
$mail->addStringAttachment($file_contents, 'file.txt', 'text/plain', 'file.txt');
You can also use addEmbeddedImage()
method to attach an image as an embedded image, it's useful when you want to send an email with an image that should be displayed inline.
$mail->addEmbeddedImage('path/to/image.jpg', 'image.jpg', 'image/jpeg');
Make sure to check the PHPMailer documentation for more information on the available methods and options.