Ad Code

Sending email with attachment and Sending HTML email

 <?php
######## Sending email with attachment ########

function sendMail(
    string $fileAttachment,
    string $mailMessage,
    string $subject,
    array $toAddress,
    array $ccAddress,
    array $bccAddress,
    string $fromMail
){
    $to        = implode(",", $toAddress);
    $cc        = implode(",", $ccAddress);
    $bcc       = implode(",", $bccAddress);
    
    $fileAttachment = trim($fileAttachment);
    $from           = $fromMail;
    $pathInfo       = pathinfo($fileAttachment);
    $attchmentName  = "attachment_".date("YmdHms").(
        (isset($pathInfo['extension']))? ".".$pathInfo['extension'] : ""
    );
    
    $attachment    = chunk_split(base64_encode(file_get_contents($fileAttachment)));
    $boundary      = "PHP-mixed-".md5(time());
    $boundWithPre  = "--".$boundary;
    
    $headers
[] = "From: $from";
    $headers
[] = "Cc: $cc";
    $headers
[] = "Bcc: $bcc";
    
    $headers
[] = "Reply-To: $from";
    $headers
[] = "Content-Type: multipart/mixed; boundary=\"".$boundary."\"";
    
    $message
[] = $boundWithPre;
    $message
[] = "Content-Type: text/plain; charset=UTF-8";
    $message
[] = "$mailMessage";
    
    $message
[] = $boundWithPre;
    $message
[] = "Content-Type: application/octet-stream; name=\"".$attchmentName."\"";
    $message
[] = "Content-Transfer-Encoding: base64";
    $message
[] = "Content-Disposition: attachment";
    $message
[] = $attachment;
    $message
[] = $boundWithPre."--";
    
    return mail($to, $subject,
implode("\r\n", $message), implode("\r\n", $headers));
}

########## Sending email in html

function sendHtmlMail(
    string $subject,
    string $fromMail
    array $toAddress,
    array $ccAddress,
    array $bccAddress,
    string $mailMessage
){
    
    $to        = implode(",", $toAddress);
    $cc        = implode(",", $ccAddress);
    $bcc       = implode(",", $bccAddress);
    $headers[] = 'MIME-Version: 1.0';
    $headers[] = 'Content-type: text/html; charset=iso-8859-1';    
    $headers[] = 'To: '.$to;
    $headers[] = 'From: '.$fromMail;    
    $headers[] = 'Cc: '.$cc;    
    $headers[] = 'Bcc: '.$bcc;    

    return mail($to, $subject, $mailMessage, implode("\r\n", $headers));
}
?>


 

Post a Comment

0 Comments