BBM Tutor
BBM Tutor

PHP Mail - mail()

Description & Uses of PHP mail()

The PHP mail() function is used to send emails directly from the PHP scripts.

Simple Example - PHP mail(String $to, String $subject, String $message, String $header);

PHP Input Screen

<?php
mail("info@bbminfo.com", "Test Mail", "Hai Buddy! How are you?");
?>

BBM Invoice

Simple PHP Syntax

<?php
mail(String $to, String $subject, String $message, String $header);
?>

Parameter & Return Type

Description

String $to
Specifies the To Address. (*Required)
String $subject
Specifies the To Subject. (*Required)
String $message
Specifies the Body of an eMail. (*Required)
String $header
Specifies the headers like From:, Cc:, Bcc: and Date:
Return Boolean
Returns TRUE if Success otherwise FALSE.

What is PHP mail() ?

  • It's used to send an Instant Electronic Mail through PHP Script
  • It returns TRUE if Success otherwise FALSE.
  • Header Information atleast contain "From:" part and the additional informations like Carbon Copy (Cc:), Blind Carbon Copy (BCC), Date, if neccessary.
  • It is available in Three flavors
    • Simple Plain Text eMail
    • HTML embeded eMail
    • eMail with Attachment
  • Simple Plain Text eMail:
    • No HTML Tags are rendered
    • The default Content-Type for Plain mail() is "text/plain; charset=us-ascii"
  • HTML embeded eMail:
    • HTML Tags are rendered
    • Inline Style Sheet must be used
    • No <html>, </html>, <head>, </head>, <title>, </title> <doctype> are used for best practice.
    • No .css Files are used
    • No .js Files are used.
    • The default Content-Type for HTML mail() is "text/html; charset=ISO-8859-1"
  • eMail with an Attachment:
    • Attachment can be sent either Plain or HTML eMail
    • No external Reference files can be sent.(Ex: https://www.bbminfo.com/YGen.pdf)
    • Maximum File Size of an Attachment is 5 MB
    • The default Content-Types are
      1. The default Content-Type of a text message is "multipart/mixed";
      2. The default Content-Type of an attachment file is "application/octet-stream";
BBM Invoice

Index

Particulars

PHP Email Example #1
Simple Plain Email
PHP Email Example #2
HTML Embeded Email
PHP Email Example #3
Simple Plain Email with an attachment (JPEG Image)
PHP Email Example #4
HTML Embeded Email with an attachment (JPEG Image)
PHP Email Example #5
HTML Embeded Email with a PDF attachment
PHP Email Example #6
HTML Embeded Email with a Multiple attachment
PHP Email Example #7
Simple Plain Email with Reply-To, CC, BCC Header

Example #1 - PHP mail(String $to, String $subject, String $message, String $header);

PHP Input Screen

<?php
/* Simple Plain eMail */
$from = "support@bbminfo.com";
$to = "info@pugalidam.com";
$subject = "Invitation for New YGen SEO Tool";
$message = "Dear User, We are very glad to inform and invite you, that BBM's YGen SEO Tool (bbminfo.com) is now ready to launch on 15th August, 2013.";

/* Header Information */
$header = "From: " . strip_tags($from) . "\r\n";
$header .= "Reply-To: " . strip_tags($from) . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=us-ascii\r\n";

$flg = mail($to, $subject, $message, $header);

if($flg == true) {
echo("Your message has been sent.");
} else {
echo("Failed to sent.");
}
?>

PHP Output Screen

Your message has been sent.

BBM Invoice

Example #2 - PHP mail(String $to, String $subject, String $message, String $header);

PHP Input Screen

<?php
/* Simple HTML eMail */
$from = "support@bbminfo.com";
$to = "info@pugalidam.com";
$subject = "Invitation for New YGen SEO Tool";
$message = "<div style=\"width:600px; margin:auto;\"><p><strong>Dear User</strong>,<br>We are very glad to inform and invite you, that <span style=\"color:#00aad2; font-weight: bold;\">BBM's YGen</span> SEO Tool (<a href=\"https://www.bbminfo.com\">bbminfo.com</a>) is now ready to launch on 15th August, 2013.<p><div>";

/* Header Information */
$header = "From: " . strip_tags($from) . "\r\n";
$header .= "Reply-To: " . strip_tags($from) . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$flg = mail($to, $subject, $message, $header);

if($flg == true) {
echo("Your message has been sent.");
} else {
echo("Failed to sent.");
}
?>

PHP Output Screen

Your message has been sent.

BBM Invoice

Example #3 - PHP mail(String $to, String $subject, String $message, String $header);

PHP Input Screen

<?php
/* Simple Plain eMail with Attachment (JPEG Image) */
$from = "support@bbminfo.com";
$to = "info@pugalidam.com";
$subject = "Invitation for New YGen SEO Tool";
$txt_message = "Dear User, We are very glad to inform and invite you, that BBM's YGen SEO Tool (bbminfo.com) is now ready to launch on 15th August, 2013.";

/* Attachment File Information (JPEG Image) */
$file_path = "YGen.jpg";

/* Header Information */
$header = "From: " . strip_tags($from);

/* Boundary Information */
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

/* Header Information */
$header .= "\nMIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed;\n";
$header .= " boundary=\"{$mime_boundary}\"";

/* Inner Operation for Message Attachment */
$msg_attachment = "This is a multi-part message in MIME format.\n\n";
$msg_attachment .= "--{$mime_boundary}\n";
$msg_attachment .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$msg_attachment .= "Content-Transfer-Encoding: 7bit\n\n" . $txt_message . "\n\n";
$msg_attachment .= "--{$mime_boundary}\n";

/* Inner Operation for File Attachment */
$file = fopen($file_path, "rb");
$data = fread($file, filesize($file_path));
fclose($file);
$data = chunk_split(base64_encode($data));

$file_attachment = "Content-Type: {\"application/octet-stream\"};\n";
$file_attachment .= " name=\"$file_path\"\n";
$file_attachment .= "Content-Disposition: attachment;\n";
$file_attachment .= " filename=\"$file_path\"\n";
$file_attachment .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$file_attachment .= "--{$mime_boundary}\n";

$message = $msg_attachment . $file_attachment;

/* Triggering Mail Function (Sending Mail with an Image) */
$flg = mail($to, $subject, $message, $header);

if($flg == true) {
echo("Your message has been sent.");
} else {
echo("Failed to sent.");
}
?>

PHP Output Screen

Your message has been sent.

BBM Invoice

Example #4 - PHP mail(String $to, String $subject, String $message, String $header);

PHP Input Screen

<?php
/* Simple HTML eMail with Attachment (JPEG Image) */
$from = "support@bbminfo.com";
$to = "info@pugalidam.com";
$subject = "Invitation for New YGen SEO Tool";
$html_message = "<div style=\"width:600px; margin:auto;\"><p><strong>Dear User</strong>,<br>We are very glad to inform and invite you, that <span style=\"color:#00aad2; font-weight: bold;\">BBM's YGen</span> SEO Tool (<a href=\"https://www.bbminfo.com\">bbminfo.com</a>) is now ready to launch on 15th August, 2013.<p><div>";

/* Attachment File Information (JPEG Image) */
$file_path = "YGen.jpg";

/* Header Information */
$header = "From: " . strip_tags($from);

/* Boundary Information */
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

/* Header Information */
$header .= "\nMIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed;\n";
$header .= " boundary=\"{$mime_boundary}\"";

/* Inner Operation for Message Attachment */
$msg_attachment = "This is a multi-part message in MIME format.\n\n";
$msg_attachment .= "--{$mime_boundary}\n";
$msg_attachment .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$msg_attachment .= "Content-Transfer-Encoding: 7bit\n\n" . $html_message . "\n\n";
$msg_attachment .= "--{$mime_boundary}\n";

/* Inner Operation for File Attachment */
$file = fopen($file_path, "rb");
$data = fread($file, filesize($file_path));
fclose($file);
$data = chunk_split(base64_encode($data));

$file_attachment = "Content-Type: {\"application/octet-stream\"};\n";
$file_attachment .= " name=\"$file_path\"\n";
$file_attachment .= "Content-Disposition: attachment;\n";
$file_attachment .= " filename=\"$file_path\"\n";
$file_attachment .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$file_attachment .= "--{$mime_boundary}\n";

$message = $msg_attachment . $file_attachment;

/* Triggering Mail Function (Sending Mail with an Image) */
$flg = mail($to, $subject, $message, $header);

if($flg == true) {
echo("Your message has been sent.");
} else {
echo("Failed to sent.");
}
?>

PHP Output Screen

Your message has been sent.

BBM Invoice

Example #5 - PHP mail(String $to, String $subject, String $message, String $header);

PHP Input Screen

<?php
/* Simple HTML eMail with PDF Attachment */
$from = "support@bbminfo.com";
$to = "info@pugalidam.com";
$subject = "Invitation for New YGen SEO Tool";
$html_message = "<div style=\"width:600px; margin:auto;\"><p><strong>Dear User</strong>,<br>We are very glad to inform and invite you, that <span style=\"color:#00aad2; font-weight: bold;\">BBM's YGen</span> SEO Tool (<a href=\"https://www.bbminfo.com\">bbminfo.com</a>) is now ready to launch on 15th August, 2013.<p><div>";

/* Attachment File Information (PDF) */
$file_path = "YGen.pdf";

/* Header Information */
$header = "From: " . strip_tags($from);

/* Boundary Information */
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

/* Header Information */
$header .= "\nMIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed;\n";
$header .= " boundary=\"{$mime_boundary}\"";

/* Inner Operation for Message Attachment */
$msg_attachment = "This is a multi-part message in MIME format.\n\n";
$msg_attachment .= "--{$mime_boundary}\n";
$msg_attachment .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$msg_attachment .= "Content-Transfer-Encoding: 7bit\n\n" . $html_message . "\n\n";
$msg_attachment .= "--{$mime_boundary}\n";

/* Inner Operation for File Attachment */
$file = fopen($file_path, "rb");
$data = fread($file, filesize($file_path));
fclose($file);
$data = chunk_split(base64_encode($data));

$file_attachment = "Content-Type: {\"application/octet-stream\"};\n";
$file_attachment .= " name=\"$file_path\"\n";
$file_attachment .= "Content-Disposition: attachment;\n";
$file_attachment .= " filename=\"$file_path\"\n";
$file_attachment .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$file_attachment .= "--{$mime_boundary}\n";

$message = $msg_attachment . $file_attachment;

/* Triggering Mail Function (Sending Mail with an Image) */
$flg = mail($to, $subject, $message, $header);

if($flg == true) {
echo("Your message has been sent.");
} else {
echo("Failed to sent.");
}
?>

PHP Output Screen

Your message has been sent.

BBM Invoice

Example #6 - PHP mail(String $to, String $subject, String $message, String $header);

PHP Input Screen

<?php
/* Simple HTML eMail with Multiple Filee Attachment */
$from = "support@bbminfo.com";
$to = "info@pugalidam.com";
$subject = "Invitation for New YGen SEO Tool";
$html_message = "<div style=\"width:600px; margin:auto;\"><p><strong>Dear User</strong>,<br>We are very glad to inform and invite you, that <span style=\"color:#00aad2; font-weight: bold;\">BBM's YGen</span> SEO Tool (<a href=\"https://www.bbminfo.com\">bbminfo.com</a>) is now ready to launch on 15th August, 2013.<p><div>";

/* Attachment File Information (Multiple Attachments) */
$file_path = array("YGen.txt", "YGen.jpg", "YGen.pdf");

/* Header Information */
$header = "From: " . strip_tags($from);

/* Boundary Information */
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

/* Header Information */
$header .= "\nMIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed;\n";
$header .= " boundary=\"{$mime_boundary}\"";

/* Inner Operation for Message Attachment */
$msg_attachment = "This is a multi-part message in MIME format.\n\n";
$msg_attachment .= "--{$mime_boundary}\n";
$msg_attachment .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$msg_attachment .= "Content-Transfer-Encoding: 7bit\n\n" . $html_message . "\n\n";
$msg_attachment .= "--{$mime_boundary}\n";

/* Inner Operation for File Attachment */
$file_attachment = "";

for($x = 0; $x < count($file_path); $x++) {
$file = fopen($file_path[$x], "rb");
$data = fread($file, filesize($file_path[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));

$file_attachment .= "Content-Type: {\"application/octet-stream\"};\n";
$file_attachment .= " name=\"$file_path[$x]\"\n";
$file_attachment .= "Content-Disposition: attachment;\n";
$file_attachment .= " filename=\"$file_path[$x]\"\n";
$file_attachment .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$file_attachment .= "--{$mime_boundary}\n";
}

$message = $msg_attachment . $file_attachment;

/* Triggering Mail Function (Sending Mail with an Image) */
$flg = mail($to, $subject, $message, $header);

if($flg == true) {
echo("Your message has been sent.");
} else {
echo("Failed to sent.");
}
?>

PHP Output Screen

Your message has been sent.

BBM Invoice

Example #7 - PHP mail(String $to, String $subject, String $message, String $header);

PHP Input Screen

<?php
/* Simple Plain eMail */
$from = "support@bbminfo.com";
$to = "info@pugalidam.com";
$email_cc = "info@bbminfo.com";
$email_bcc = "bbm4you@yahoo.com";
$subject = "Invitation for New YGen SEO Tool";
$message = "Dear User, We are very glad to inform and invite you, that BBM's YGen SEO Tool (bbminfo.com) is now ready to launch on 15th August, 2013.";

/* Header Information */
$header = "From: " . strip_tags($from) . "\r\n";
$header .= "Cc: " . strip_tags($email_cc) . "\r\n";
$header .= "Bcc: " . strip_tags($email_bcc) . "\r\n";
$header .= "Reply-To: " . strip_tags($from) . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=us-ascii\r\n";

$flg = mail($to, $subject, $message, $header);

if($flg == true) {
echo("Your message has been sent.");
} else {
echo("Failed to sent.");
}
?>

PHP Output Screen

Your message has been sent.

BBM Invoice
 
Disclaimer: Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.