Attach docx in mail using php mailer
To attach a .docx
file to an email using PHPMailer, you can follow these steps:
- First, make sure you have PHPMailer installed and configured in your PHP project. You can download it from here.
- Create a new instance of the PHPMailer class and set the sender's email address, recipient's email address, and the subject of the email.
- Use the
addAttachment()
method to attach the.docx
file to the email. You can specify the file path and name as arguments to this method.
Here's an example code snippet that demonstrates how to attach a .docx
file to an email 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 = 'your_email_username';
$mail->Password = 'your_email_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('your_email_address', 'Your Name');
$mail->addAddress('recipient_email_address', 'Recipient Name');
$mail->Subject = 'Test Email with Attachment';
$mail->Body = 'This is a test email with an attachment.';
// Attach the.docx file
$mail->addAttachment('path/to/your/file.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'base64', 'file.docx');
if (!$mail->send()) {
echo 'Error sending email: '. $mail->ErrorInfo;
} else {
echo 'Email sent successfully!';
}
?>
In this example, we're using the addAttachment()
method to attach a .docx
file named file.docx
located at path/to/your/file.docx
. We're also specifying the MIME type of the file as application/vnd.openxmlformats-officedocument.wordprocessingml.document
and the encoding as base64
.
Make sure to replace the placeholders (your_email_username
, your_email_password
, your_email_address
, recipient_email_address
, and path/to/your/file.docx
) with your actual email credentials and file path.
Also, note that you may need to adjust the SMTP settings and authentication details depending on your email provider and configuration.