发送生成的二维码到收件人的电子邮件

时间:2018-06-27 04:32:22

标签: php phpmailer qr-code

是否可以通过以下方式让QR码显示在电子邮件中:

This is the result of running at localhost

它不必像页面一样是图形的,它可以是这样的:

it can also be as simple as this

我只是希望QR码显示在收件人的邮件中。

以下是php代码:

index.php

<!DOCTYPE html>
<html><head>

    <meta charset="UTF-8">
    <title>Generate QR Code</title>
    <!-- bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    <!-- bootstrap -->
 <style>
    body, html {
        height: 100%;
        width: 100%;
        background-image: url("images/bg.jpg");
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
    }
</style>
</head>
<body>
<div class="container" id="panel">
    <br><br><br>
    <div class="row">
        <div class="col-md-6 offset-md-3" style="background-color: white; padding: 20px; box-shadow: 10px 10px 5px #888;">
            <div class="panel-heading">
                <h1>Generate QR-code in PHP</h1>
            </div>
            <hr>
            <form action="show.php" method="post">
                Name :<input type="text" autocomplete="off" class="form-control" name="PartName" style="border-radius: 0px; " placeholder="Type your Name" value="">
                <br>
                Age :<input type="text" autocomplete="off" class="form-control" name="PartAge" style="border-radius: 0px; " placeholder="Type your Age" value="">
                <br>
                Email :<input type="text" autocomplete="off" class="form-control" name="PartEmail" style="border-radius: 0px; " placeholder="Type your Email" value="">
                <br>
                IC :<input type="text" autocomplete="off" class="form-control" name="PartIC" style="border-radius: 0px; " placeholder="Type your IC" value="">
                <br>
                Gender :<br>
                <input type="radio" name="gender" value="male"> Male<br>
                <input type="radio" name="gender" value="female"> Female<br>
                <br>
                Phone No :<input type="text" autocomplete="off" class="form-control" name="PartPhone" style="border-radius: 0px; " placeholder="Type your Phone Number" value="">
                <br>
                Address :<input type="text" autocomplete="off" class="form-control" name="PartAddress" style="border-radius: 0px; " placeholder="Type your Address" value="">
                <br>
                 Size :<select name="size">
                        <option value="XS">XS</option>
                        <option value="S">S</option>
                        <option value="M">M</option>
                        <option value="L">L</option>
                        <option value="XL">XL</option>
                        </select>
                <!-- button with bootstrap applied-->
                <input type="submit" class="btn btn-md btn-danger btn-block" value="Generate" name="register">
                <a href="decode.php">Decode QR</a>
            </form>
        </div>
    </div>
</div>

generate.php

e<?php
require "vendor/autoload.php";
use Endroid\QrCode\QrCode;
$qrcode = new QrCode($_GET['text']);
header("Content-Type: image/png");
echo $qrcode->writeString();
die();

show.php

enter code here<?php


$name=$_POST['PartName'];
$age=$_POST['PartAge'];
$address=$_POST['PartAddress'];
$phoneNo=$_POST['PartPhone'];
$gender=$_POST['gender'];
$ic=$_POST['PartIC'];
$email=$_POST['PartEmail'];
$size=$_POST['size'];
$fulldetail="Name: ".$name."\r\n Age:".$age."\r\n Address:".$address."\r\n Phone No:".$phoneNo."\r\n Gender:".$gender."\r\n IC:".$ic."\r\n Email:".$email."\r\n Size:".$size;
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Generate QR Code</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<style>
    body, html {
        height: 100%;
        width: 100%;
        background-image: url("images/bg.jpg");
        background-position: center;
        background-repeat: repeat;
        background-size: cover;
    }
</style>
</head>
<body>
<div class="container" id="panel">
    <br><br><br>
    <div class="row">
        <div class="col-md-6 offset-md-3" style="background-color: white; padding: 20px; box-shadow: 10px 10px 5px #888;">
            <div class="panel-heading">
                <h1>Generate QR-code in PHP</h1>
            </div>
            <hr>
             <div id ="qrbox" style="text-align: center;">               
                 <h3>Thanks for your register Mr <?php echo $name?> below is your qr code</h3>
                 <img src="generate.php?text=<?php echo $fulldetail?>" alt="">
                 <h5>Details:</h5>
                 <p>Name : <?php echo $name ?></p>
                 <p>Age : <?php echo $age?></p>
                 <p>Address/State : <?php echo $address?></p>
                 <p>Gender : <?php echo $gender?></p>
                 <p>IC : <?php echo $ic?></p>
                 <p>Phone Number : <?php echo $phoneNo?></p>
                 <p>T-shirt Size : <?php echo $size?></p>
                 <p>The details are sent to your Email</p>

        </div>
            <br>
            <a href="index.php">Generate One More</a>
             <a href="decode.php">Decode qr</a>
        </div>
    </div>
</div>
</body>
</html>
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//include PHPmailer/Autoload.php
require 'vendor/autoload.php';
//create an instance of PHPMailer
$mail = new PHPMailer(true);
try{
$mail->isSMTP();
//set host
$mail->Host = "smtp.gmail.com";
//set authentication to true
$mail->SMTPAuth = true;
//set login details for Gmail account;
$mail->Username = "rexgohkokhow@gmail.com";
$mail->Password ="PASS";
//set port
$mail->Port = 587;
//set who is sending an email
$mail->setFrom('rexgohkokhow@gmail.com','Rex');
//set who are we sending mail
$mail->addAddress($email,$name);
//set html for body
$mail->isHTML(true);
//set subject or title
$mail->Subject = "Thanks for your register";
$mail->AddEmbeddedImage('generate.php?text='.$fulldetail , 'qr');
//set body
$mail->Body = 
"
<h1>Thank You for Register</h1>
            <hr>
             <div id ='qrbox' style='text-align: center;'>               
                 <h3>Thanks for register below is your qr code</h3>".
                 "<img href='generate.php?text=$fulldetail' />".
                    "<h4>Details:</h4>
                 <p>Name : $name</p><p>Age : $age</p><p>Address/State : $address</p><p>Gender : $gender</p><p>IC : $ic</p><p>Phone Number : $phoneNo</p><p>T-shirt Size : $size</p>
                <h6>Please Remember to show this upon that day</h6>
        </div>";
//send email
$mail->send();
}catch(Exception $e){}

PS:我是PHP的新手,我对这段代码零经验,因为根据youtube上的教程,上面的大多数编码都是遵循的。很抱歉,没有清楚地解释它。

1 个答案:

答案 0 :(得分:1)

这行不通:

$mail->addEmbeddedImage($qrcode->writeString(), 'qr', PHPMailer::ENCODING_BASE64, 'image/png');

因为它不会调用您的脚本。结合两个脚本,您可以执行以下操作:

href

然后(因为它是嵌入式图像),而不是这样做(<img href='generate.php?text=$fulldetail' /> 也用于链接而不是图像):

<img src="cid:qr">

请参考模板HTML中的嵌入式图像:

List<WebElement> elemlist= driver.findElements(By.xpath("//div[@class='smpdf_379Xjwyb9lWVFS smpdf_2-apaRtNmCROQL']//li/ul/li"));
        int count = elemlist.size();
        System.out.println(count);

请注意,多次调用QR代码生成脚本都没有关系。