Email communication is a crucial aspect of modern web applications. Whether it’s welcoming new users, notifying them of changes, or sending important updates, email serves as a primary channel. PHP, being a versatile server-side scripting language, provides built-in functionalities to handle email operations efficiently. In this article, we will explore how to use PHP to send emails, including emails with attachments, to enhance your application’s communication capabilities.
The PHP mail()
Function
PHP’s mail()
function is a straightforward way to send emails directly from your server. This function can handle plain text emails, HTML emails, and even emails with attachments. Here’s the basic syntax of the mail()
function:
mail(to, subject, message, headers, parameters)
Sending a Simple Plain Text Email
Sending a plain text email using PHP is simple. Below is a basic example of how to send a plain text email using the mail()
function:
<?php
$to = 'johndoe@email.com';
$subject = 'New Notification Received';
$message = 'Hi Admin, a new notification has been received on the website';
$from = 'info@scriptut.com';
// Sending email
if(mail($to, $subject, $message, "From:" . $from)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
In this example, we specify the recipient’s email address, subject, message, and the sender’s email address.
Sending HTML Formatted Emails
HTML emails are more visually appealing and can include rich text, images, and links. To send an HTML email, we need to set the appropriate headers to inform the email client that the email content is in HTML format. Here’s how you can send an HTML email using PHP:
<?php
$to = 'johndoe@gmail.com';
$subject = 'Welcome to Our Website';
$from = 'info@scriptut.com';
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
// Create email headers
$headers .= 'From: ' . $from . "\r\n" .
'Reply-To: ' . $from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Welcome to our website.</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
In this example, we set the Content-type
header to text/html
to ensure the email is treated as HTML by the recipient’s email client.
Sending Emails with Attachments
Sending emails with attachments in PHP involves a bit more complexity. We need to handle MIME types and encode the attachment properly. Below, we will create a function named mail_attachment()
to facilitate sending emails with attachments.
Creating the mail_attachment()
Function
The mail_attachment()
function will help us send emails with attachments. Here is the complete function:
<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path . $filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: " . $from_name . " <" . $from_mail . ">\r\n";
$header .= "Reply-To: " . $replyto . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message . "\r\n\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$header .= $content . "\r\n\r\n";
$header .= "--" . $uid . "--";
if (mail($mailto, $subject, "", $header)) {
echo "Mail sent successfully.";
} else {
echo "Failed to send mail.";
}
}
?>
Using the mail_attachment()
Function
Now, let’s see how we can use the mail_attachment()
function to send an email with an attachment:
<?php
$my_file = "documents.zip";
$my_path = "/home/shared/";
$my_name = "John Doe";
$my_mail = "johndoe@mailonator.com";
$my_replyto = "info@scriptut.com";
$my_subject = "Please find the attachment";
$my_message = "Hello James, please find the attached document.";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>
In this example, we specify the file to be attached (documents.zip
), its path, and other email details. The mail_attachment()
function handles the rest, ensuring the file is properly encoded and attached to the email.
Conclusion
Sending emails from a PHP application is a powerful feature that enhances user communication. Whether you need to send plain text emails, HTML emails, or emails with attachments, PHP’s mail()
function, along with custom handling for attachments, provides a comprehensive solution. By following the examples and guidelines provided in this article, you can implement robust email functionality in your PHP applications.