繁体   English   中英

PHP字符串并置变量

[英]PHP String Concatenation With Variables

这是我第一次这样做,但是我确实必须这样做,所以我一直在使用php并拥有此脚本,该脚本在发送邮件时给我带来麻烦。 它只是一个ajax POST请求,向php脚本发送代码和电子邮件。 我面临的问题是获取$ message字符串以将第一封电子邮件与变量连接在一起。 如果我只是将$ message设置为字符串文字并发送电子邮件,则一切正常。 但是,如此处所示,当尝试将一些变量连接到其中时,不会发送电子邮件。 另外,将新的电子邮件和代码写入Winners.txt文件时,所有功能均正常运行。 我将字符串串联错了吗? 我尝试了几种不同的方法。 谢谢!

<?php
$myFile = "winners.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$email = $_POST['email'];
$code = $_POST['code'];
$code = (string)$code . '';
$message = "Winner information follows:\r\nEmail: ";
$message .= strval($email);
$message .= "\r\nConfirmation Code: ";
$message .= strval($code);
fwrite($fh, $email);
fwrite($fh, $code);
mail("loganhsnow@gmail.com", "Winning Notice ST", $message);
mail($email, "Winning Notice CT", ', Congrats, you won a free amazon gift card at logansnow.tk. If the following confirmation code matches the one in our records you will receive your reward. The code follows: ');
fclose($fh);
?>

首先不需要进行任何转换。 这样就可以摆脱$code = (string)$code . ''; $code = (string)$code . ''; 以及所有的strval()调用。 您的$ message可以这样设置:

$message = "Winner information follows:\\r\\nEmail: ".$email."\\r\\nConfirmation Code: ".$code;

另外,您似乎没有在第二封电子邮件中包含该代码。 因此,整个更改看起来像这样:

<?php
$myFile = "winners.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$email = $_POST['email'];
$code = $_POST['code'];
$message = "Winner information follows:\r\nEmail: ".$email."\r\nConfirmation Code: ".$code;
fwrite($fh, $email);
fwrite($fh, $code);
mail("loganhsnow@gmail.com", "Winning Notice ST", $message);
mail($email, "Winning Notice CT", 'Congrats, you won a free amazon gift card at logansnow.tk. If the following confirmation code matches the one in our records you will receive your reward. The code follows: '.$code);
fclose($fh);
?>

要注意的另一件事是,您可能希望通过某些卫生检查来放入$ email,以确认它是有效的电子邮件地址。

暂无
暂无

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

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