简体   繁体   English

尝试发送电子邮件时PHP错误

[英]Error in php while trying to send email

I am learning php and have a book which has lots of examples and exercises in. The one I'm doing is about sending email. 我正在学习php,有一本书,其中包含许多示例和练习。我正在做的一本关于发送电子邮件。 I copied the code to the letter, but I am getting an error: 我将代码复制到了信件上,但出现错误:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\website2\www\sendscript.php on line 6 

Why is this happening, any idea? 为什么会发生这种情况?

Here is my PHP script: 这是我的PHP脚本:

<?php
    if(($_POST['sender_name']=="")||($_POST['sender_email']=="")||($_POST['message']=="")){
        header("Location: sendemail.html");
        exit;
    }

    $email = "Sender's name:\t$_POST['sender_name']\n";
    $email .= "Sender's email:\t$_POST['sender_email']\n";
    $email .= "Message:\t$_POST['message']\n";
    $to = "stafford.king16@pgs.org.uk";
    $subject = "did script work?";
    $mailheaders = "From: Stafford King";
    $mailheaders .= "Reply to: $_POST['sender_email']\n";
    mail($to, $subject, $email, $mailheaders);
?>
<html>
    <head></head>
    <body>
        <h1>Email sent!</h1>
    </body>
</html>

And here is my email input form: 这是我的电子邮件输入表单:

<html>
  <head></head>
  <body>
    <form method = "post" action = "sendscript.php">
        <p><strong>Your name:</strong><br />
        <input type = "text" name = "sender_name" size = "30"</p>
        <p><strong>Your email address:</strong><br />
        <input type = "text" name = "sender_email" size = "30"</p>
        <p><strong>Message:</strong><br />
    <textarea name = "message" cols = "30" rows = "5" wrap = "virtual"></textarea></p>
        <p><input type = "submit" name = "submit" value = "send email"></p>
    </form>
  </body>
</html>

Thanks guys :) 多谢你们 :)

You need to wrap your array variables in curly brackets {} or concatenate them within the string: 您需要将数组变量包装在大括号{}或将其串联在字符串中:

OPTION 1 选项1

$email = "Sender's name:\t{$_POST['sender_name']}\n";

OPTION 2 选项2

$email = "Sender's name:\t" . $_POST['sender_name'] . "\n";

NOTE: 注意:

Use of an IDE such as Dreamweaver (license fee), NetBeans or Eclipse (both open source & free) would have caught this immediately. 使用Dreamweaver(许可费),NetBeans或Eclipse(开源和免费)之类的IDE可能会立即引起注意。 Try one of these, rather than Notepad or Notepad++. 请尝试其中之一,而不是Notepad或Notepad ++。

$email = "Sender's name:\t$_POST['sender_name']\n";

can't be resolved very well. 无法很好地解决。 Use 使用

$email = "Sender's name:\t{$_POST['sender_name']}\n";

instead. 代替。 Alternatively, you could concatenate or use sprintf : 另外,您可以连接或使用sprintf

$email = "Sender's name:\t" . $_POST['sender_name'] . "\n";
$email = sprintf("Sender's name:\t%s\n", $_POST['sender_name']);

You need to split up your concatenation: 您需要拆分串联:

$email = "Sender's name:\t" . $_POST['sender_name'] . "\n";
$email .= "Sender's email:\t" . $_POST['sender_email'] . "\n";
$email .= "Message:\t" . $_POST['message'] . "\n";

PHP can't parse it as it is now. PHP无法像现在一样解析它。

Wrapping the variables in curly braces or taking them out of the double quotes will help with the body of the email but not solve your parsing error. 将变量用大括号括起来或将其排除在双引号之外将有助于电子邮件正文,但不能解决解析错误。 You mail headers seem to be messed up. 您的邮件标题似乎一团糟。 You need a "\\n" at the end of the each line. 每行的末尾都需要一个“ \\ n”。 Change the first $mailheader assignment to: $mailheaders = "From: Stafford King\\n"; 将第一个$ mailheader分配更改为: $mailheaders = "From: Stafford King\\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM