【问题标题】:How To create a url link and send it to email using php mailer如何创建一个 url 链接并使用 php mailer 将其发送到电子邮件
【发布时间】:2016-05-11 01:18:43
【问题描述】:

我创建了一个表单,用户在其中键入代码,如果代码和电子邮件存在于数据库中,则代码然后检索电子邮件并生成令牌并将其更新到数据库,并且必须将其发送到检索到的电子邮件.我正在使用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';

}

}

?>

【问题讨论】:

  • 那么本应与此一起发布的表格在哪里?
  • 您的代码基于一个过时的示例,并且使用的是旧版本的 PHPMailer。 Get the latest.

标签: php mysql phpmailer


【解决方案1】:

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

$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 数组是洁净的。

另外,这个$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

旁注:只应在暂存阶段显示错误,而不是在生产阶段。

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


脚注:

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

现在,如果您的查询失败,那就另当别论了,您需要找出原因,并且超出了问题的范围。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2012-11-25
    • 2022-11-03
    • 2021-12-11
    • 2018-06-17
    相关资源
    最近更新 更多