Simple email function to send html content

Use the the below send_mail() function to send the text or html mail to specific recipients

<?php
function send_mail($to, $from, $subject, $body, $cc='', $bcc=''){
    // To send HTML mail, you can set the Content-type header.
    $headers = "From: $from\r\n";
    $headers .= "Content-type: text/html\r\n";
    //add additional headers like, reply-to, etc if you need
    if(!empty($cc)){
        $headers .= "Cc: $cc\r\n";
    }
    if(!empty($bcc)){
    $headers .= "Bcc: $bcc\r\n";
    }
    // and now native mail function
    mail($to,$subject, $body, $headers);
   //you can use the return type for success message or error message
}
?>

You can use the above function by calling the function with parameters as below

<?php
    send_mail('recipient@domain.com', 'frommail@fromdomainname.com', 'Hello subject', 'Content');
?>

Leave a Reply