繁体   English   中英

如何使用php mailer创建URL链接并将其发送到电子邮件

[英]How To create a url link and send it to email using php mailer

我创建了一个表单,用户可以在其中输入代码,如果代码和电子邮件存在于数据库中,则代码将检索电子邮件,并且将生成令牌并将令牌更新到数据库,并且必须将令牌发送到所检索的电子邮件。 我正在使用php mailer发送电子邮件,有人可以帮助我确定下面的代码有什么问题,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

参考:

这是假设您的条件语句和POST数组是洁净的。

另外,根据$idcode=$_POST['idcode'];$post['idcode']需要读为$_POST['idcode'] $idcode=$_POST['idcode']; 错误报告将在这里为您提供帮助。 那是一个超全局的http://php.net/manual/en/language.variables.superglobals.php ,但是缺少下划线并以大写字母表示POST。

如果不确定:

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

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

// Then the rest of your code

旁注:显示错误仅应在登台中进行,而不应在生产中进行。

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


脚注:

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

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

暂无
暂无

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

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