我正在向用户发送确认邮件,以确认他的帐户。确认邮件使用HTML格式,并有一个href元素,该元素将用户指向一个PHP文件,我在该文件中执行确认过程。这个href链接还需要有一个PHP随机字符串附加,相同的随机字符串保存在数据库中,也被发送给用户,这样一旦用户点击它,交叉检查就可以在PHP中完成。
<td align="center" style="margin:0;text-align:center">
<a href="http://aliencreative.wehubs.com/new_alien/email.php?code=<?php echo $randomString; ?>"
style="font-size:21px;line-height:22px;text-decoration:none;color:#ffffff;font-weight:bold;
border-radius:2px;background-color:#0096d3;padding:14px 40px;display:block;
letter-spacing:1.2px" target="_blank">Confirm Alien account now!</a></td>PHP代码包括上述HTML,如下所示。
<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;
echo $random;
$to = 'sample@gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
$htmlContent = file_get_contents("email_template.php");
// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Alien creative control<alien@alien.com>' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Some problem occurred, please try again.';
endif;
?> 但是,PHP变量在链接中不可用。
发布于 2016-10-20 17:23:57
请试一下这个。删除file_get_contents并使用具有输出缓冲区功能的include。我想这会将你的变量插入到你的模板中。请检查以下更新后的代码。不管怎么说,你的方法不是安全的。请创建更复杂的随机码
<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;
echo $random;
$to = 'sample@gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
ob_start();
include "email_template.php";
$htmlContent=ob_get_contents();
ob_end_clean();
// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Alien creative control<alien@alien.com>' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Some problem occurred, please try again.';
endif;
?> https://stackoverflow.com/questions/40148870
复制相似问题