Jump to content

Sending html email with mail()


tinwakr

Recommended Posts

Hi Everyone,

 

I am trying to send email with the mail() function as per instructions at: http://php.net/manual/en/function.mail.php without success. The mail sends but when it arrives it doesn't render properly, I see all of the html tags with the data that's sent. My code is below:

 

<?php

function processFormData($array, $form)
{
    $errors = NULL;
    $message = NULL;
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers = "From: <[email protected]>" . "\r\n";
    $headers .="Cc: <[email protected]>" . "\r\n";
    $headers .= "Reply-to: <[email protected]>" . "\r\n";
    $headers .= "X-Mailer: PHP/" .phpversion() . "\r\n";

    foreach($array as $key => $value)
    {
        //if any of the form values are empty catch them and add to error array
        if(empty($value) && $key != 'submit')
        {
            //build error list
            $errors .= '"' . str_replace('_', ' ', $key) . '" is a required field!<br />';
        }
        else if(!empty($value) && $key != 'submit')
        {
            //capture values and insert them into a session variable
            $_SESSION[$key] = $value;
        }
    }
    
    if(empty($errors))
    {
        $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\r\n";
        $message .= '<html><head><title>' . $form .'</title></head>'
                . "\r\n" . '<body><table border="1">' . "\r\n";
        foreach($array as $key => $value)
        {
            //regular expression check for phone number
            if(str_replace('_', ' ', $key) == 'Phone Number' && !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $value))
            {
                return 'Please enter a valid phone number!';
            }
            //regular expression check for email
            if(str_replace('_', ' ', $key) == 'Email Address' && !preg_match("/^[A-Z0-9._%-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i", $value))
            {
                return 'Please enter a valid email address!';
            }
            if($key != 'submit')
            {
                $message .= '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>' . "\r\n";
            }
        }
        $message .= '</table></body></html>' . "\r\n";
        mail("[email protected]", $form, str_replace("_", " ", $message), $headers);
        return 'Thank you, someone will contact you shortly.';
    }
    else
    {
        return $errors;
    }
}
?>

 

Thanks in advance for all replies,

Chuck

Link to comment
https://forums.phpfreaks.com/topic/256473-sending-html-email-with-mail/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.