使用HTML格式化PHP电子邮件

时间:2014-07-29 14:31:31

标签: php html email

我跟着this tutorial at CSS-Tricks,但显然搞砸了。

我想知道我的问题是由于我的表单和PHP在单独的文件中引起的吗?无论如何......目前使用的PHP还没有给我发送电子邮件。

Here is the site currently live,即使发送电子邮件也不会发送代码

以下是发送电子邮件的代码,但格式不正确:

<?php
/* Set e-mail recipient */

$to = 'myemail@example';
$from='teamemail@example.com';
$subject='Demo Submission Form';
$headers .='From: ' . strip_tags($_POST['email']) . '/r/n';
$headers .='Reply-To: ' . strip_tags($_POST['email']) . '/r/n';
$headers .='MIME-Version: 1.0/r/n';
$headers .='Content-type: text/html; charset=ISO-8859-1/r/n';


/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], 'Please, enter your name');
$email = check_input($_POST['email'], 'Please enter your email address');
$number = check_input($_POST['phoneNumber']);
$address = check_input($_POST['address']);
$city = check_input($_POST['city']);
$state = check_input($_POST['state']);
$zip = check_input($_POST['zip']);
$contact= $_POST['radio'];
$request= $_POST['checkbox'];

if (isset($_POST['checkbox'])) {
$checkbox = $_POST['checkbox'];
// $service is an array of selected values
$checkbox_string = '';
for($i=0;$i<count($checkbox);$i++)
{
    if($i!=0)
    {
        $checkbox_string = $checkbox_string . ', ';
    }
    $checkbox_string = $checkbox_string . $checkbox[$i];
}$checkbox = join(',  ', $_REQUEST['checkbox']);
}

/* If e-mail is not valid show error message */
if (!preg_match('/([\w\-]+\@[\w\-]+\.[\w\-]+)/', $email))
{
show_error('E-mail address not valid');
}
/* Let's prepare the message for the e-mail */
$message = "

Name: $name
E-mail: $email
Client Type: $contact

Phone Number: $number

Address: $address
City: $city
State: $state
Zip Code: $zip

Request: $checkbox

";

/* Send the message using mail() function */

mail($to, $subject, $message, $headers );

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

我在去的时候学习这个,所以请随意解释,就像我是一个5岁的白痴一样!

ETA:不起作用的代码:

<?php
/* Set e-mail recipient */

$to = 'myemail@example.com';
$from='testemail@example.com';
$subject='Demo Submission Form';
$headers .='From: ' . strip_tags($_POST['email']) . '/r/n';
$headers .='Reply-To: ' . strip_tags($_POST['email']) . '/r/n';
$headers .='MIME-Version: 1.0/r/n';
$headers .='Content-type: text/html; charset=ISO-8859-1/r/n';


/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], 'Please, enter your name');
$email = check_input($_POST['email'], 'Please enter your email address');
$number = check_input($_POST['phoneNumber']);
$address = check_input($_POST['address']);
$city = check_input($_POST['city']);
$state = check_input($_POST['state']);
$zip = check_input($_POST['zip']);
$contact= $_POST['radio'];
$request= $_POST['checkbox'];

if (isset($_POST['checkbox'])) {
$checkbox = $_POST['checkbox'];
// $service is an array of selected values
$checkbox_string = '';
for($i=0;$i<count($checkbox);$i++)
{
    if($i!=0)
    {
        $checkbox_string = $checkbox_string . ', ';
    }
    $checkbox_string = $checkbox_string . $checkbox[$i];
}$checkbox = join(',  ', $_REQUEST['checkbox']);
}

/* If e-mail is not valid show error message */
if (!preg_match('/([\w\-]+\@[\w\-]+\.[\w\-]+)/', $email))
{
show_error('E-mail address not valid');
}
/* Let's prepare the message for the e-mail */
$message = '<html><body>';
$message .= '<img src="IMAGE"     alt="Ferris Demo Submission Request" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . ($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . ($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Client Type:</strong> </td><td>" . ($_POST['contact']) . "</td></tr>";
$message .= "<tr><td><strong>Phone Number:</strong> </td><td>" . ($_POST['phoneNumber']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" . $_POST['address'] . "</td></tr>";
$message .= "<tr><td><strong>City:</strong> </td><td>" . $_POST['city'] . "</td></tr>";
$message .= "<tr><td><strong>State:</strong> </td><td>" . $_POST['state'] . "</td></tr>";
$message .= "<tr><td><strong>Zip Code:</strong> </td><td>" . $_POST['zip'] . "</td></tr>";
$message .= "<tr><td><strong>Request:</strong> </td><td>" . $_POST['checkbox'] . "</td>         </tr>";
$message .= "</table>";
$message .= "</body></html>"
;

/* Send the message using mail() function */

mail($to, $subject, $message, $headers );

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

0 个答案:

没有答案