PHP uses the mail() function to send email. This function requires three authoritative arguments that define the recipient's email address, the subject of the message and the message itself and there are two other optional parameters.

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

Here is a description of each of the parameters.

Parameter Description
toRequired. Specifies the receiver / receivers of the email
subjectRequired. Specifies the subject of the email. This parameter cannot contain any newline characters
messageRequired. Defines the message to be sent. Each line should be separated with a LF ( ). Lines should not exceed 70 characters
headersOptional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF ( )
parametersOptional. Specifies an additional parameter to the send mail program

Sending HTML email

If you send a text message using PHP all content will be considered as simple text. Or you will add HTML tags to the text message, it will be displayed as simple text and the HTML tags will not be formatted according to the HTML syntax. But PHP offers the option to send the HTML message as the HTML message itself.


         $to = "";
         $subject = "This is subject";
         $message = "This is HTML message.";
         $message .= "

This is headline.

; $header = " \r\n"; $header .= " \r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $retval = mail ($to,$subject,$message,$header); if( $retval == true ) { echo "Message sent successfully..."; }else { echo "Message could not be sent..."; } ?>

Sending attachments with email

Sending an email with attachments content requires setting the type of content type to multipart/mixed. Thereafter the text and attachment sections can be specified within the parameters.


ini_set('display_errors', 1);

if (isset($_FILES) && (bool) $_FILES) {

    $allowedExtensions = array("pdf", "doc", "docx", "gif", "jpeg", "jpg", "png", "txt");

    $files = array();
    foreach ($_FILES as $name => $file) {
        $file_name = $file['name'];
        $temp_name = $file['tmp_name'];
        $file_type = $file['type'];
        $path_parts = pathinfo($file_name);
        $ext = $path_parts['extension'];
        if (!in_array($ext, $allowedExtensions)) {
            die("File $file_name has the extensions $ext which is not allowed");
        array_push($files, $file);

    $to = $_POST['email'];
    $from = "";  //your website email type here
    $subject = "Email attachment tutorial";
    $message = $_POST['msg'];
    $headers = "From: $from";

    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
    $message .= "--{$mime_boundary}\n";

    // preparing attachments
    for ($x = 0; $x < count($files); $x++) {
        $file = fopen($files[$x]['tmp_name'], "rb");
        $data = fread($file, filesize($files[$x]['tmp_name']));
        $data = chunk_split(base64_encode($data));
        $name = $files[$x]['name'];
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
                "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    // send

    $ok = mail($to, $subject, $message, $headers);
    if ($ok) {
        echo "

mail sent to $to!

; } else { echo "

mail could not be sent!

; } } ?> <html> <head> <link href="style.css" rel="stylesheet" type="text/css"/> </head> <body> <form method="post" action="" enctype="multipart/form-data" class="attachment-form"> <h3>Email with attachment</h3> <p>We will not store your email to our database you can test to type your email id at here</p> <input type="email" name="email" placeholder="email"/><br> <textarea name="msg" placeholder="Message"></textarea><br> attach file<br> <input type="file" name="attach1"/><br> <input type="submit" value="Send"/> </form> </body> </html>