php中的email()函数给了我一个0作为电子邮件正文


The email() function in php is giving me a 0 as the email body

我有一个简单的帐户创建页面,它接受用户输入并通过电子邮件发送到我的电子邮件帐户,但当我检查电子邮件时,它显示一个零作为正文。

    <form action="" method="post">
    <p style="font-family:latine;">Username: <input type="text" name="username" id="username"></p>
    <br><br>
    <p style="font-family:latine;">Password: <input type="password" name="password" id="password"></p>
<br>
<input type="submit" value="Create account">
</form>
<?php
   $user = $_POST["username"];
   $password = $_POST["password"];
   $info = $user + $password;
   mail("myemail@gmail.com", "User request", $info);
?>

看到别人决定发布答案:

我将解释

CCD_ 1,CCD_。

要么用引号括起来,然后去掉+符号:

$info = "$user $password";

或者使用PHP的.(句点)串联语法:

$info = $user . " " . $password;

错误报告添加到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code

旁注:错误报告只能在临时阶段进行,而不能在生产阶段进行。


您可能还想将可执行代码封装在一个条件语句中,命名提交按钮并检查是否所有字段都为空。

看到您在一个文件中使用全部代码。

<form action="" method="post">
    <p style="font-family:latine;">Username: 
    <input type="text" name="username" id="username"></p>
    <br><br>
    <p style="font-family:latine;">Password: 
    <input type="password" name="password" id="password"></p>
<br>
<input type="submit" name="submit" value="Create account">
</form>
<?php
if(isset($_POST['submit']) 
&& !empty($_POST["username"]) 
&& !empty($_POST["password"])){
   $user = $_POST["username"];
   $password = $_POST["password"];
   $info = "$user $password";
   mail("myemail@gmail.com", "User request", $info);
   }
?>
  • 否则,在使用错误报告时,您将在初始页面加载时收到未定义索引的通知

另一件事。您需要在邮件头中使用额外的参数"发件人:"。由于缺少"发件人:",您的邮件可能会被垃圾邮件或完全忽略。

  • http://php.net/manual/en/function.mail.php

从该页面提取的示例:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "'r'n" .
    'Reply-To: webmaster@example.com' . "'r'n" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

additional_parameters参数可用于将附加参数传递给配置为在使用sendmail_path发送邮件时使用的程序。

<?php
mail('nobody@example.com', 'the subject', 'the message', null,
   '-fwebmaster@example.com');
?>

替换:

$info = $user + $password;

通过$info = $user . $password;

请参见字符串concate:http://php.net/manual/en/language.operators.string.php

相关文章: