如何创建url链接并使用php邮件程序将其发送到电子邮件

2022-08-08 00:00:00 php mysql phpmailer
我创建了一个表单,用户在其中键入代码,如果代码和电子邮件存在于数据库中,则代码然后检索电子邮件,并生成令牌并更新到数据库,并必须发送到检索到的电子邮件。我正在使用php邮件工具发送电子邮件,有人能帮我找出下面的代码出了什么问题吗?url查询正确吗?

<?php

error_reporting(1);
session_start();


include 'includes/connect.php';
include 'includes/additional_function.php';
include('classes/phpmailer/phpmailer.php');



if($_POST["Submit"]=="Submit"){

 $idcode=$_POST['idcode'];
 $_SESSION['idcode'] = $post['idcode'];

 $sql = "SELECT * FROM people WHERE idcode = :idcode";

 $stmt = $pdo->prepare($sql);

 $stmt->bindValue(':idcode', $idcode);



 $stmt->execute();

 $result = $stmt->fetch(PDO::FETCH_ASSOC);



 if(!empty($result)){
 $email = $result['email'];
 //echo $email;


 $token = generateToken();
 //echo $token;


 $sql = "UPDATE student SET token = :token WHERE email = :email";
 $stmt = $pdo->prepare($sql);

 $stmt->execute(array(
 ':token' => $token,
 ':email' => $email
 ));


 $result1 = $stmt->fetch(PDO::FETCH_ASSOC);
 if(!empty($result)){

 {
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;




$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server

$mail->SMTPAuth = true; // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer@bradm.inmotiontesting.com
// pass: password
$mail->Username = "send_from_PHPMailer@bradm.inmotiontesting.com"; // SMTP username
$mail->Password = "password"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("bradm@inmotiontesting.com", "Brad Markle");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);



$mail->Subject = "You Registration Link!";


$mail->Body = 'http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id';
$mail->AltBody = 'Click to Register';


if(!$mail->Send())
{
 echo "Message could not be sent. <p>";
 echo "Mailer Error: " . $mail->ErrorInfo;
 exit;
}

echo "Message has been sent";
}

} 
}

else{


 echo 'You are not Registered';

}

}

?>

解决方案

首先,不会在单引号中解析变量

$mail->Body = 'http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id';

用双引号引起来

$mail->Body = "http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id";

并且不会在发送的电子邮件中填充自己。

例如:

$token = "abcde";

echo $var = 'http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id';

echo "<br>";

echo $var = "http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id";

将回应以下内容:

http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id
http://www.domain.com/register/registration.php?token=abcde&stud_id=stud_id

如您所见,$token没有填充其预期的值,但回显为$token而不是abcde

参考:

  • http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single

这是假定您的条件语句和POST数组是合乎规则的。

加上这个$post['idcode']需要按照$idcode=$_POST['idcode'];读作$_POST['idcode'],错误报告在这里会对您有所帮助。这是一个超全局http://php.net/manual/en/language.variables.superglobals.php,并且漏掉了下划线和大写字母的POST。

如果您不确定:

将error reporting添加到您的文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sideote:只能在试运行中显示错误,绝不能在生产中显示错误。

或在您的问题中发布您的HTML表单。


脚注:

不确定要使用stud_id来做什么,以及应该如何填充。只有你知道。根据$token&stud_id=stud_id';

现在,如果您的查询失败,那就是另一回事了,您需要找出原因,这超出了问题的范围。

  • http://php.net/manual/en/pdo.error-handling.php

相关文章