PHP mail

Basic form
  • mail($to, $subject, $message, $headers);



 //change this to your email.
    $to = "";
    $from = "";
    $subject = "";

Multiple recipients

$to  = 'recipient1@example.com' . ', '; // note the comma
$to .= 'recipient2@example.com';

HTML message

    $message = <<<EOF
<html>
  <body bgcolor="#DCEEFC">
    <center>
        <b>This is bold.</b> <br>
        <font color="red">This is red</font> <br>
        <a href="http://www.blueswami.com/">Blueswami.com</a>
    </center>
    </body>
     </html>
EOF;   //end of message

For HTML to work these headers must be sent

$headers  = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";

Headers

    $headers  = "From: $fromrn";
    $headers .= "Content-type: text/htmlrn";

//options to send to cc+bcc    
    $headers .= 'Cc: copy@@example.com' . "rn";
    $headers .= 'Bcc: blindcopy@example.com' . "rn";

Mail with attachment


<?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()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">rn";
    $header .= "Reply-To: ".$replyto."rn";
    $header .= "MIME-Version: 1.0rn";
    $header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
    $header .= "This is a multi-part message in MIME format.rn";
    $header .= "--".$uid."rn";
    $header .= "Content-type:text/plain; charset=iso-8859-1rn";
    $header .= "Content-Transfer-Encoding: 7bitrnrn";
    $header .= $message."rnrn";
    $header .= "--".$uid."rn";
    $header .= "Content-Type: application/octet-stream; name="".$filename.""rn"; // use diff. tyoes here
    $header .= "Content-Transfer-Encoding: base64rn";
    $header .= "Content-Disposition: attachment; filename="".$filename.""rnrn";
    $header .= $content."rnrn";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo = "mail send ... OK"; // or use booleans here
    } else {
        echo = "mail send ... ERROR!";
    }
}
// how to use
$my_file = "somefile.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>