我一直在尝试一些解决方案,但我无法弄清问题是什么。我正在使用PHPmailer发送SMTP邮件,在该邮件中,我使用的是用户的值,有些是在后端计算的。请有人帮忙解决这个问题。
在我将PHP文件中的文本到图像代码放在工作之前。
代码经过实时/在线服务器测试。我检查了垃圾文件夹,但我还没收到任何东西。
<?php
echo '
<html>
<head>
<title>Thank you for Registering</title>
<style type="text/css">
body {
padding-top: 50px;
background-image:url(images/bkgrd01.jpg);
background-repeat:no-repeat;
}
.thankyoucontent {
padding: 10px;
width: 500px;
margin-right: auto;
margin-left: auto;
text-align: center;
border: 1px solid #999;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
-khtml-border-radius: 10px;
border-radius: 10pt;
background-color:#FFF;
}
.thankyoucontent p {
font-family: Helvetica, Arial, sans-serif;
font-size: 12pt;
text-align:left;
}
@font-face {
font-family: bcodefont;
src: url(fonts/IDAutomationHC39M.ttf);
}
.thankyoucontent p.barcode {
font-family: bcodefont;
font-size: 18pt;
font-style: normal;
text-align:center;
}
</style>
</head>
<body>
<div class="thankyoucontent">' ?>
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
/* ------------- GENERATING THE BARCODE -------------- */
$result = mysql_query("SELECT * FROM wp_tps_forms");
$rows = mysql_num_rows($result);
global $bcode_user;
$bcode_user = 600201700000 + $rows + 1;
$flagemail = 0;
while($row=mysql_fetch_array($result)){
$Email=$row['email'];
if(strcmp($Email, $_POST['Email']) == 0) {
exit("<p>This email is taken. Please use another to register.</p>");
}
}
/* ------------- SAVING DATA INTO DB -------------- */
$sql="INSERT INTO wp_tps_forms (name, email, contact, barcode)
VALUES ('$_POST[Fname]','$_POST[Email]','$_POST[Tel]','$bcode_user')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error()); exit('Error: ' . mysql_error());
}
/* --------------------> CONVERT TEXT TO IMAGE >------------------------ */
function ImageTTFCenter($image, $text, $font, $size, $angle = 45)
{
$xi = imagesx($image);
$yi = imagesy($image);
$box = imagettfbbox($size, $angle, $font, $text);
$xr = abs(max($box[2], $box[4]));
$yr = abs(max($box[5], $box[7]));
$x = intval(($xi - $xr) / 2) - 20;
$y = intval(($yi + $yr) / 2);
return array($x, $y);
}
$im = @imagecreatefromjpeg('images/testimg(w).jpg');
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// The text to draw
$fbid ="$bcode_user";
$text ="$bcode_user";
$uploads_dir = 'images/';
// Replace path by your own font path
$font = './fonts/IDAutomationHC39M.ttf';
//image file name
$name = $uploads_dir.$fbid.".png";
list($x, $y) = ImageTTFCenter($im, $text, $font,16);
// Add the text
imagettftext($im, 16, 0, $x, $y, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im,$name,9);
imagedestroy($im);
/* ---------------------------<<------------------------------- */
/* ---------------------- SEND EMAIL ----------------------- */
//Img path to be attached
$imgpath = 'images/' . $bcode_user . '.png';
/**
date_default_timezone_set('Etc/UTC');
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = username@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('dummy@gmail.com', 'T2I - First Last');
//Set an alternative reply-to address
$mail->addReplyTo('dummy@gmail.com', 'Reply Email');
//Set who the message is to be sent to
$mail->addAddress($_POST['Email'], 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer - Txt2Img Test Email';
$mail->AddEmbeddedImage($imgpath, 'bcodeimg');
$mail->AddEmbeddedImage('./images/testimg.jpg', 'bcodeimg', 'testimg.jpg');
$mail->AltBody = 'This is a plain-text message body';
$mail->Body = '<p>This is a test picture: <img src="cid:bcodeimg" /></p><p style="font-family: Helvetica, Arial, sans-serif; font-size: 12pt; text-align:left;">Test 3 - (In Email Body)Thank you for registering. You will receive an email (Inbox/Junk folder) with a copy of your unique barcode. Alternatively, you can take a screenshot of the barcode and carry it with you to enter at the gates.</p>';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo '<p style="font-family: bcodefont; font-size: 18pt; font-style: normal; text-align:center;">' . $bcode_user . '</p><p style="font-family: Helvetica, Arial, sans-serif; font-size: 12pt; text-align:left;">(WebPage Confirmation)Thank you for registering. You will receive an email(Inbox/Junk folder) with a copy of your unique barcode. Alternatively, you can take a screenshot of the barcode and carry it with you to enter at the gates.</p>';
}
?>
<?php
echo '</div></body></html>';
?>