mail()
function is used to sending emails in user or client. Email messages is very common for web applications (or) web sites, for example : sending a welcome email when a user create a new account on your website, newsletters, registration purpose, feedback or comments and contact from etc. mail()
function for creating and sending email messages to one or more recipients dynamically from your web application (or) web site.mail($to, $subject, $message, $headers, $parameters)
Parameter | Description | Required / Optional |
---|---|---|
$to | The recipient's email address. | Required |
$subject | Represents subject of the mail. | Required |
$message | Message to be sent with the mail. Each line should be separated with a line feed-LF (\n ). Lines should not exceed 70 characters. |
Required |
$headers | This is typically used to add extra headers such as "From", "Cc", "Bcc". The additional headers should be separated with a carriage return plus a line feed-CRLF (\r\n ). |
Optional |
$parameters | Used to pass additional parameters. | Optional |
When you send html text message using PHP then all the content will be treated as simple text. Even if you will include HTML tags in a text message, it will be displayed as simple text and HTML tags will not be formatted according to HTML syntax. But PHP provides option to send an HTML message as actual HTML message.
<!DOCTYPE html>
<html>
<head>
<title>PHP mail() Function</title>
</head>
<body>
<?php
$to = "info@somedomain.com";
$subject = "This is sample subject";
$message = "<b>This is sample HTML message.</b>";
$header = "From:abc@somedomain.com \r\n";
$header .= "Cc:abcd@somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$main_email = mail ($to,$subject,$message,$header);
if( $main_email == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
</body>
</html>
To send message with attachment, you need to mention many header information which is used in the example given below :
<?php
$to = "info@domainname.com";
$subject = "This is sample subject";
$message = "This is a sample text message.";
// Open a file
$file = fopen("file.txt", "r" );//change your file location
if( $file == false )
{
echo "Error in opening file";
exit();
}
// Read the file into a variable
$size = filesize("file.txt");
$content = fread( $file, $size);
// encode the data for safe transit
// and insert \r\n after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));
// Get a random 32 bit number using time() as seed.
$num = md5( time() );
// Define the main headers.
$header = "From:abc@example.com\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
// Define the message section
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$num\r\n";
// Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"test.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"test.txt\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
// Send email now
$result = mail ( $to, $subject, "", $header );
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>