dou4064 2016-10-28 17:13 采纳率: 100%
浏览 33
已采纳

调试:用PHP发送电子邮件[关闭]

I've been writing some code to send emails from a site. The base code worked perfectly, I then added some small bits for checking the inputted data is right and it's now just displaying that the page isn't working. This is the first time I've used PHP so am quite new to it all, although I can't see any errors with the code?

enter image description here

<?php 
if(isset($_POST['submitButton'])){

    function errorMessage($error) {
        echo 'Apologies but the request has not been successful<br/>';
        echo 'Please see below, amend and then resubmit<br/><ul>';
        echo $error . '</ul>';
        die();
    }
    $error_message = ""; // set the error message as empty

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; // string to look for
    $string_exp = "/^[A-Za-z .'-]+$/"; // string to look for
    $contact_exp = '/^[0-9]/';


    $subject = " Request"; // subject of their email to me
    $subject2 = "Copy of your Request"; // subject of the email back to them
    $to = "myemail"; // this is my Email address
    $from = $_POST['emailAddressField']; // this is the requesters Email address

    if (!preg_match($email_exp,$from)) {    
        $error_message . = '<li>The Email Address you entered does not appear to be valid.</li>';
    }

    $first_name = $_POST['firstNameField']; // this is their first name

    if (!preg_match($string_exp,$first_name)) {
        $error_message . = '<li>The first name you entered does not appear to be valid.</li>';
    }

    $last_name = $_POST['surnameField']; // this is their surname

    if (!preg_match($string_exp,$last_name)) {
        $error_message . = '<li>The surname you entered does not appear to be valid.</li>';
    }

    $contact_number = $_POST['contactNumberField']; // this is their contact number

    if (!preg_match($contact_exp,$contact_number)){
        $error_message . = '<li>The contact number you entered does not appear to be valid.</li>';
    }

    $details_field = $_POST['detailsField']; // this is the details

    if (strlen($details_field) < 10) {
        $error_message . = '<li>Please give more details, at least 10 characters.</li>';
    }

    $message = $first_name . " " . $last_name . " (" . $contact_number . ") wrote the following:" . "

" . $details_field; // message of email to me
    $message2 = "Here is a copy of your request " . $first_name . " (" . $contact_number . ").

" . $details_field . "

Many thanks"; // message of email to requester

    $headers = "From:" . $from; // message of email header to me
    $headers2 = "From:" . $to; // message of email header to requester

    // if there have been errors, then display error message and end
    if(strlen($error_message) > 0) {
        errorMessage($error_message);
    }

    // send the emails
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Request sent. Thank you " . $first_name . ", I will contact you shortly.";

}
?>

Works perfectly:

<?php 
if(isset($_POST['submitButton'])){

    $subject = "Request"; // subject of their email to me
    $subject2 = "Copy of your Request"; // subject of the email back to them
    $to = "myemail"; // this is my Email address
    $from = $_POST['emailAddressField']; // this is the requesters Email address
    $first_name = $_POST['firstNameField']; // this is their first name
    $last_name = $_POST['surnameField']; // this is their surname
    $contact_number = $_POST['contactNumberField']; // this is their contact number
    $details_field = $_POST['detailsField']; // this is the details
    $message = $first_name . " " . $last_name . " (" . $contact_number . ") wrote the following:" . "

" . $details_field; // message of email to me
    $message2 = "Here is a copy of your request " . $first_name . " (" . $contact_number . ").

" . $details_field . "

Many thanks"; // message of email to requester
    $headers = "From:" . $from; // message of email header to me
    $headers2 = "From:" . $to; // message of email header to requester

    // send the emails
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Request sent. Thank you " . $first_name . ", I will contact you shortly.";

}
?>

Can anyone help me with finding this annoying error?

  • 写回答

2条回答 默认 最新

  • dougai3418 2016-10-28 17:23
    关注

    Everywhere you have . =, needs to be changed to .=

    i.e.

    $error_message . = '<li>The Email Address you entered does not appear to be valid.</li>';
    

    should be

    $error_message .= '<li>The Email Address you entered does not appear to be valid.</li>';
    

    In order to be able to debug php, you need somewhere you can run it locally and view the error messages. When I run the file, I get: Parse error: syntax error, unexpected '=' in temp.php on line 23 which tells you exactly where to find the syntax problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量