Sending emails with attachments is a common task while developing PHP applications, achieved using PHP's built-in mail()
function. The mail()
function allows developers to send emails with text or HTML content and attach one or more files.
Here's a sample PHP code that demonstrates how to send an email with an attachment in PHP:
Example:
<?php
// recipient email address
$to = "[email protected]";
// subject of the email
$subject = "Email with Attachment";
// message body
$message = "This is a sample email with attachment.";
// from
$from = "[email protected]";
// boundary
$boundary = uniqid();
// header information
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\".$boundary.\"\r\n";
// attachment
$attachment = chunk_split(base64_encode(file_get_contents('file.pdf')));
// message with attachment
$message = "--".$boundary."\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode($message));
$message .= "--".$boundary."\r\n";
$message .= "Content-Type: application/octet-stream; name=\"file.pdf\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"file.pdf\"\r\n\r\n";
$message .= $attachment."\r\n";
$message .= "--".$boundary."--";
// send email
if (mail($to, $subject, $message, $headers)) {
echo "Email with attachment sent successfully.";
} else {
echo "Failed to send email with attachment.";
}
?>
The recipient's email address, subject, message body, and header information are defined in the code above. The boundary variable separates the message and attachment in the email.
The attachment is read from a file using file_get_contents()
and encoded using base64_encode()
. The chunk_split()
function splits the attachment into smaller chunks for sending.
Finally, the mail()
function sends the email with an attachment. If the email has been sent successfully, the function will return true
; Otherwise, it will return false
.
Send HTML Form Data Over Email Using PHP
To get the HTML form content and send it in an email, you need to modify the code as follows:
- Add an HTML form to your webpage to collect the desired information from users.
- When submitting the HTML form, use PHP code to retrieve the form data and store it in variables.
- In the above PHP code, replace the hardcoded values of the variables with the input names of the form data.
Here's an example HTML code for a form that allows you to submit an email with the recipient's email address, subject, message, sender's email address, and an attachment:
Example:
<form action="send-email.php" method="post" enctype="multipart/form-data">
<label for="to">To:</label>
<input type="email" id="to" name="to"><br>
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject"><br>
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea><br>
<label for="from">From:</label>
<input type="email" id="from" name="from"><br>
<label for="attachment">Attachment:</label>
<input type="file" id="attachment" name="attachment"><br>
<input type="submit" value="Send">
</form>
In the code above, the form's action is set to "send-email.php", where the form data will be sent for processing. The enctype
attribute is set to multipart/form-data
to indicate that the form will contain both text and binary data (i.e., the file attachment). The input fields allow you to enter the recipient email, subject, message, sender email and select a file for attachment. The submit button is labeled "Send".
Below is a code of the "send-email.php" page, which receives data and sends emails using the above HTML form:
Example:
<?php
// recipient email address
$to = $_POST['to'];
// subject of the email
$subject = $_POST['subject'];
// message body
$message = $_POST['message'];
// from
$from = $_POST['from'];
// boundary
$boundary = uniqid();
// header information
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\".$boundary.\"\r\n";
// attachment
$file = $_FILES["attachment"]["tmp_name"];
$filename = $_FILES["attachment"]["name"];
$attachment = chunk_split(base64_encode(file_get_contents($file)));
// message with attachment
$message = "--".$boundary."\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode($message));
$message .= "--".$boundary."\r\n";
$message .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$message .= $attachment."\r\n";
$message .= "--".$boundary."--";
// send email
if (mail($to, $subject, $message, $headers)) {
echo "Email with attachment sent successfully.";
} else {
echo "Failed to send email with attachment.";
}
?>
In conclusion, sending an email with attachments in PHP is a straightforward process that can be achieved using the mail()
function and format the email correctly.