Add attachment in mail php
To add an attachment to an email using PHP, you can use the mail()
function or a PHP mail library such as PHPMailer. Here's an example of how to do it using the mail()
function:
<?php
$to = '[email protected]';
$subject = 'Test email with attachment';
$message = 'This is a test email with an attachment.';
// Path to the file you want to attach
$attachment = 'path/to/your/file.txt';
// Open the file to read its contents
$fp = fopen($attachment, 'rb');
$attachment_data = fread($fp, filesize($attachment));
fclose($fp);
// Set the 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 message body
$message = "This is a test email with an attachment.\r\n\r\n".
'--This is the boundary string'. "\r\n".
'Content-Type: text/plain'. "\r\n".
'Content-Transfer-Encoding: 7bit'. "\r\n\r\n".
$message. "\r\n\r\n".
'--This is the boundary string'. "\r\n".
'Content-Type: application/octet-stream; name="'. basename($attachment). '"'. "\r\n".
'Content-Transfer-Encoding: base64'. "\r\n".
'Content-Disposition: attachment; filename="'. basename($attachment). '"'. "\r\n\r\n".
chunk_split(base64_encode($attachment_data)). "\r\n\r\n".
'--This is the boundary string--';
// Send the email
mail($to, $subject, $message, $headers);
?>
In this example, we're using the mail()
function to send an email with an attachment. We're setting the Content-Type
header to multipart/mixed
and the boundary
string to This is the boundary string
. We're then setting the message body to include the attachment.
The attachment is added to the message body using the Content-Type
header set to application/octet-stream
and the Content-Disposition
header set to attachment
. The attachment data is encoded using base64 and chunked using the chunk_split()
function.
Please note that the mail()
function is not recommended for sending emails with attachments, as it's not reliable and can cause issues with some email clients. It's recommended to use a PHP mail library such as PHPMailer instead.
Here's an example of how to send an email with an attachment using PHPMailer:
<?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.';
// Path to the file you want to attach
$attachment = 'path/to/your/file.txt';
$mail->addAttachment($attachment, basename($attachment));
if (!$mail->send()) {
echo 'Error sending email: '. $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 setting the Host
, SMTPAuth
, Username
, Password
, SMTPSecure
, and Port
properties to configure the SMTP server. We're then setting the setFrom()
, addAddress()
, Subject
, and Body
properties to configure the email. Finally, we're adding the attachment using the addAttachment()
method and sending the email using the send()
method.