PHP Mail Function to Send Email & Encoded attachments

Email is a cost effective solution to send & receive massages faster & secure. In web application development sending mail is a very common activity. Whether a on-line job consultancy or an employee management system every where you required email facility. Email is a derived technology of postal service. To send an mail we required To, From at minimum. To means who is going to receive the mail & from is the sender.

Among many web development programming languages PHP is popular. It required open source environment to execute. The cause the server cost is less in PHP, compare to other web development technologies. In PHP to send a mail there is a mail() function. Look at the syntax below.

mail(to,subject,message,headers,parameters);

PHP mail() function accepts 5 parameters. First parameter is to whom you want to send the mail. This value is an email id. Next parameter is subject, here you can mention the Title of your message. This param accepts String value. Third parameter is the real message. These 3 parameters are mandatory. Rest 2 headers & parameters are optional. Headers specify the additional headers like Content-type, From, CC & BCC. Last param specify additional parameters to send mail function.

Normally two types of mails we send Text & HTML based. To send text mail when declaring message for mail() function you need to add pure texts like $message = “This mail body contains simple text message.”;. But in case you want to send a mail with HTML format in this case if you have a small message you can do in-line HTML while declaring $message. If your message is bigger it was advisable to define a string variable & concatenate HTML entries with message characters. Additionally in header declare content-type to text/html. Look at the example below.

Example to send HTML format Mail using PHP

<html>
<head>
<title>Send HTML format mail using PHP</title>
</head>
<body>
<?php
$to = "admin@jharaphula.com";
$subject = "Demo PHP mail Function";
$message = "<b>I am a regular reader of jharaphula.</b>";
$message .= "<h2>This is the Header.</h2>";
$header = "From:customer@yourdomain.com \r\n";
$header = "Cc:panda.biswabhusan@gmail.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$sendMail = mail ($to, $subject, $message, $header);
if($sendMail == true)
{
echo "Your email sent successfully.";
}
else
{
echo "Email could not be sent.";
}
?>
</body>
</html>

Example to send text Mail using PHP

<html>
<head>
<title>Send text mail using PHP</title>
</head>
<body>
<?php
$to = "admin@jharaphula.com";
$subject = "Demo PHP mail Function";
$message = "I am a regular reader of jharaphula. This is a text msg.";
$header = "From:customer@yourdomain.com \r\n";
$sendMail = mail ($to, $subject, $message, $header);
if($sendMail == true)
{
echo "Your email sent successfully.";
}
else
{
echo "Email could not be sent.";
}
?>
</body>
</html>

During we send a mail we not only send the content but also some time we required to send attachments. Your attachment can be a file or an image too. So let us know how to send attachment using php mail() function.

To send an attachment first thing is to define is the content-type to multipart/mixed. Using this mail function will know that the email you are going to send is with an attachment. Then the next thing is when send a file over network to make the file transfer secure you can use php base64_encode() function. Look at the below example.

Sending attachment using PHP mail function

<html>
<head>
<title>Send attachment based email using PHP</title>
</head>
<body>
<?php
$to = "admin@jharaphula.com";
$subject = "This is a demo mail";
$message = "This message contents file attachment.";
# Using PHP file funtion Open the file.
$file = fopen( "/tmp/demo.txt", "r" );
if($file == false)
{
echo "Error in file. Unable to Open.";
exit();
}
# Read the file and store the content into a variable
$fsize = filesize("/tmp/demo.txt");
$fcontent = fread($file, $fsize);

# encode the data for secure transaction and insert \r\n after every 76 chars.
$encoded_Con = chunk_split(base64_encode($fcontent));

# Generate a random 32 bit number using time() function and md5 algorithm.
$number = md5(time());

$header = "From:customer@yourdomain.com\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$number\r\n";
$header .= "--$number\r\n";

# Define the header information for message section
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$number\r\n";

# Define the header information for attachment section
$header .= "Content-Type:  multipart/mixed; ";
$header .= "name=\"demo.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"demo.txt\"\r\n\n";
$header .= "$encoded_Con\r\n";
$header .= "--$number--";

$sendMail = mail ($to, $subject, "", $header);
if($sendMail == true)
{
echo "Your email sent successfully.";
}
else
{
echo "Email could not be sent.";
}
?>
</body>
</html>