我們有一個提交表格的網站,通過以下代碼(通過下面的代碼);爲什麼PHP Mailer不總是發送電子郵件?
發送一個測試的郵件,自己之前插入到MySQL只是讓我知道PHP郵包的工作,它總是在我所有的測試確實
然後從表格中的所有數據插入到MYSQL(這也總是有效)
通過PHP郵件發送另一封電子郵件與他們提交的所有信息。我希望它以純文本格式(這僅適用於大約80%的時間)。
發生了什麼事是,有時人們提交和它的一切,但有時它只做步驟1和2。所以有時候,有些事情似乎在某處斷裂。我覺得這可能是某些標點符號的問題,但不確定。就像在$複製字段中一樣,人們可以輸入任何他們想要的東西。不確定是否有某些標點可能會打破第二封電子郵件程序。
注意:每次有人提交時,都會將它們帶到request-success.php頁面。但它並不總是發送來自步驟3的電子郵件。
如果有人看到有改進的地方或者錯誤的東西,請分享。這讓我瘋狂。
<?php
session_start();
include_once("config.php");
include_once("includes/functions.php");
require 'phpmailer/PHPMailerAutoload.php';
//database configuration & connection (hiding for privacy purposes, but the database connections work fine so not relevant
if ($_POST['submit']) {
$type=$_POST['type'];
$category= substr($type, 0, strpos($type, ' -'));
$category= strtolower($category);
$category= ucfirst($category);
$need = substr($type, strpos($type, "-") + 1);
$subject="REQUEST for " . $type;
$fullname= $_SESSION['google_data']['name'];
$fromemail=$_SESSION['google_data']['email'];
$brands=$_POST['brand'];
$size=$_POST['size'];
if ($size == "") {
$size="n/a";
}
$bleed=$_POST['bleed'];
if ($bleed =="no") {
$bleedsize="n/a";
} else {
$bleedsize=$_POST['bleedsize'];
}
$filetype=$_POST['filetype'];
if ($filetype=="") {
$filetype="n/a";
}
$footerurl=$_POST['footer-url'];
if ($footerurl=="") {
$footerurl="n/a";
}
$footerphone=$_POST['footer-phone'];
if ($footerphone=="") {
$footerphone="n/a";
}
$copy=mysqli_real_escape_string($con,$_POST['copy']);
$copyforemail=$_POST['copy'];
$approved=$_POST['approved'];
$seo=$_POST['seo'];
$proofread=$_POST['proofread'];
$info=mysqli_real_escape_string($con,$_POST['info']);
$infoforemail=$_POST['info'];
$priority=$_POST['priority'];
$requestdate= date('Y-m-d');
$duedate = date('Y-m-d', strtotime(str_replace('-', '/', $_POST['duedate'])));
if ($duedate =="1969-12-31") {
$duedate="0000-00-00";
}
$timinginfo=mysqli_real_escape_string($con,$_POST['timinginfo']);
$timinginfoforemail=$_POST['timinginfo'];
$communication=mysqli_real_escape_string($con,$_POST['communication']);
$communicationforemail=$_POST['communication'];
//TEST EMAIL BEFORE INSERTING
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
/*
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
*/
$mail->setFrom($fromemail, $fullname);
$mail->addAddress('[email protected]', 'my name'); // Add recipients
$mail->addReplyTo('[email protected]', 'my name');
/*$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name*/
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Request coming for Creative Team";
$mail->Body = "Request coming";
$mail->AltBody = "Request coming";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//continue to insert
}
$sql = "INSERT INTO requests (firstname, lastname, email, picture, category, type, brand, size, bleed, bleedsize, filetype, footerurl, footerphone, copy, approved, proofread, seo, info, priority, requestdate, duedate, timinginfo, communication) VALUES ('" . $_SESSION['google_data']['given_name'] . "', '" . $_SESSION['google_data']['family_name'] . "','" . $_SESSION['google_data']['email'] . "', '" . $_SESSION['google_data']['picture'] . "', '$category', '$need', '$brands', '$size', '$bleed', '$bleedsize', '$filetype', '$footerurl', '$footerphone', '$copy', '$approved', '$proofread', '$seo', '$info', '$priority', '$requestdate', '$duedate', '$timinginfo', '$communication')";
$insertinfo = mysqli_query($con, $sql);
if (!$insertinfo) {
die("Database query failed: " . mysqli_error($con));
} else {
//Success, continue to email...
}
$plaintextversion= "
$type
BRAND: $brands
SPECS
SIZE: $size
BLEED: $bleed
BLEED SIZE: $bleedsize
FILE TYPE: $filetype
FOOTER URL: $footerurl
FOOTER PHONE: $footerphone
COPY: $copyforemail
COPY APPROVED? $approved
PROOFREAD? $proofread
ADDITIONAL INFO: $infoforemail
TIMING
PRIORITY: $priority
REQUEST DATE: $requestdate
DESIRED DUE DATE: $duedate
TIMING INFO: $timinginfoforemail
COMMUNICATION
ADDITIONAL PEOPLE TO INCLUDE: $communicationforemail";
} else {
header("Location:index.php");
}
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
/*
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
*/
$mail->setFrom($fromemail, $fullname);
$mail->addAddress('[email protected]', 'someone'); // Add recipients
$mail->addReplyTo('[email protected]', 'my name');
/*$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name*/
$mail->isHTML(false); // Set email format to plain text since that is what Salesforce needs
$mail->Subject = $subject;
$mail->Body = $plaintextversion;
$mail->AltBody = $plaintextversion;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header("Location:request-success.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex, nofollow">
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
您的查詢全面開放SQL注入攻擊,如果您的內容中有引號,可能是您的問題的原因。你已經在使用mysqli了,所以你應該準備好語句功能供你使用。 –