ctsstc / Lite-Stack-PHP

A collection of lite PHP frameworks and libraries
0 stars 0 forks source link

Add Email Basic Functionality #3

Open ctsstc opened 9 years ago

ctsstc commented 9 years ago

Throw into the helper functions

ctsstc commented 9 years ago
/**
 * @param $to
 * @param $fromEmail
 * @param $subject
 * @param $message
 * @param $fromName
 */
function sendEmail($to, $fromEmail, $subject, $message, $fromName)
{
    $fromEmail = str_replace(array("\r", "\n"), '', $fromEmail); // to prevent email injection

    $headers = "From: $fromName <$fromEmail>\n".
        //"X-Sender: <mail@".$domain.">\n".
        "X-Mailer: PHP\n". //mailer
        "X-Priority: 3\n". //1 UrgentMessage, 3 Normal
        "Content-Type: text/html; charset=utf-8\n".
        "Return-Path: <$fromEmail>\n";
    $fromEmail = "-f ".$fromEmail;
    mail($to, $subject, $message, $headers, $fromEmail);
}

/**
 * @param $to
 * @param $fromEmail
 * @param $subject
 * @param $message
 * @param $filePath
 * @param $fromName
 */
function sendEmailAttachment($to, $fromEmail, $subject, $message, $filePath,  $fromName)
{
    $uid = md5(uniqid(time()));

    $fileName = basename($filePath);
    //$fileSize = filesize($filePath);
    $fileContent = chunk_split(base64_encode(file_get_contents($filePath)));

    $fromEmail = str_replace(array("\r", "\n"), '', $fromEmail); // to prevent email injection

    $headers = "From: $fromName <$fromEmail>\n".
        //"X-Sender: <mail@".$domain.">\n".
        "X-Mailer: PHP\n". //mailer
        "X-Priority: 3\n". //1 UrgentMessage, 3 Normal
        "Return-Path: <$fromEmail>\n"
        ."MIME-Version: 1.0\r\n"
        ."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
        ."This is a multi-part message in MIME format.\r\n"
        ."--".$uid."\r\n"
        ."Content-type:text/html; charset=utf-8\r\n"
        ."Content-Transfer-Encoding: 7bit\r\n\r\n"
        .$message."\r\n\r\n"
        ."--".$uid."\r\n"
        ."Content-Type: application/octet-stream; name=\"".$fileName."\"\r\n"
        ."Content-Transfer-Encoding: base64\r\n"
        ."Content-Disposition: attachment; filename=\"".$fileName."\"\r\n\r\n"
        .$fileContent."\r\n\r\n"
        ."--".$uid."--";

    $fromEmail = "-f ".$fromEmail;
    mail($to, $subject, "", $headers, $fromEmail);
}