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: <chuck@deneb-consulting.com>" . "\r\n";
    $headers .="Cc: <tinwakr@bell.net>" . "\r\n";
    $headers .= "Reply-to: <chuck@deneb-consulting.com>" . "\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("chuck@deneb-consulting.com", $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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.