PHP字符串连接与变量

时间:2014-05-31 06:09:01

标签: php string email variables concatenation

这是我第一次这样做,但我真的不得不这样做,所以我一直在使用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);
?>

1 个答案:

答案 0 :(得分:0)

首先不需要进行任何转换。所以你可以摆脱$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,以验证它是否是有效的电子邮件地址。

相关问题