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

时间:2016-05-11 01:18:44

标签: php mysql phpmailer

我创建了一个用户输入代码的表单,如果代码和电子邮件存在于数据库中,则代码会检索电子邮件并生成令牌并更新到数据库,并且必须将其发送到检索到的电子邮件。我使用php邮件发送邮件,有人可以帮我弄清楚下面代码有什么问题,网址查询是否正确?

<?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';

}

}

?>

1 个答案:

答案 0 :(得分:2)

首先,变量不能用单引号解析

    -- Supported protocols
    data ProtoA = ProtoA
    data ProtoB = ProtoB

    -- Protocol sum type for storing messages in a channel.
    data P = PA ProtoA | PB ProtoB

    -- messages stored in a channel that can support either message.
    type PChan = TChan P

    instance Binary ProtoA where
      put ProtoA = return ()
      get = return ProtoA

    instance Binary ProtoB where
      put ProtoB = return ()
      get = return ProtoB

    instance Binary P where
      -- on put, have the constructor available to drive behavior
      put (PA ProtoA) = return ()
      put (PB ProtoB) = return ()
      -- on get, nothing to differentiate behavior
      -- don't want alternation
      get = undefined

    -- Yuck, wrapped newtypes instances...
    newtype PA' = PA' P
    newtype PB' = PB' P

    instance Binary PA' where
      put (PA' (PA ProtoA)) = return ()
      put (PA' (PB ProtoB)) = fail "shouldn't happen"
      get = return (PA' (PA ProtoA))

    instance Binary PB' where
      put (PB' (PA ProtoA)) = fail "shouldn't happen"
      put (PB' (PB ProtoB)) = return ()
      get = return (PB' (PB ProtoB))

用双引号将其包装

$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

<强>参考:

这假设您的条件语句和POST数组是犹太教。

另外,根据abcde,此$post['idcode']需要显示为$_POST['idcode'],错误报告可以帮助您。这是一个超级全球http://php.net/manual/en/language.variables.superglobals.php并错过了下划线并将POST置于大写字母中。

如果您不确定:

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

$idcode=$_POST['idcode'];

旁注:只应在暂存时进行显示错误,而不是生产。

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

<强>脚注:

不确定您要使用<?php error_reporting(E_ALL); ini_set('display_errors', 1); // Then the rest of your code 的内容以及如何填充该内容。只有你知道。根据{{​​1}}

现在,如果您的查询失败,那么这是一个不同的故事,您需要找出原因,并且超出了问题的范围。