PHP新手 - 脚本无法发送带有表单内容的电子邮件

时间:2014-07-16 19:51:30

标签: php forms

我是PHP的新手,我需要用来将订单发送到我的电子邮件地址的脚本无效。

我已经从头开始构建了一个热门的在线教程,但它根本不适用于我。我也尝试了谷歌搜索解决方案没有运气。有什么想法吗?

<?php

$cases = $_POST['cases'];
$other_cases = $_POST['other_cases'];
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$business_name = $_POST['business_name'];
$bill_address = $_POST['bill_address'];
$bill_city = $_POST['bill_city'];
$bill_state = $_POST['bill_state'];
$bill_zip = $_POST['bill_zip'];
$ship_address = $_POST['ship_address'];
$ship_city = $_POST['ship_city'];
$ship_state = $_POST['ship_state'];
$ship_zip = $_POST['ship_zip'];
$additional = $_POST['additional'];



$from = 'From: TC Body Bag Order Form';
$to = 'heather@featherhart.com';
$subject = 'You have a new order!';

$body = "Full Name: $full_name\n 
         Email Address: $email\n 
         Phone Number: $phone\n
         Cases: $cases\n
         Other Amount: $other_cases\n 
         Business Name: $business_name\n 
         Billing Line 1: $bill_address\n 
         Billing Line 2: $bill_city\n, $bill_state\n $bill_zip\n 
         Shipping Line 1: $ship_address\n 
         Shipping Line 2: $ship_city, $ship_state $ship_zip\n 
         Additional: $additional\n ";

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your order has been placed! You will receive an email 
                shortly with an updated total and confirmation of your order.</p>';
} else { 
    echo '<p>Oops... something went wrong!<br>
                Please try again.</p>'; 
}

?>

1 个答案:

答案 0 :(得分:0)

<?php
// The message
 $message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
 // Send
mail('mail@example.com', 'My Subject', $message);
?>

。我发现你的代码没有问题。

- 您必须尝试使用​​非常基本的代码发送邮件,例如之前的代码。如果没问题且电子邮件已成功发送,那么您必须调试并检查是否已使用

设置所有变量
print_r($_POST);

这将打印post数组并查看所有变量是否正常。

对于专业方式,您可以使用此方法检查是否设置了变量:

$cases = isset($_POST['cases']) ? $_POST['cases'] : "" 

而不是这个

$cases = $_POST['cases'];

:)享受