这是我第一次使用PHP邮件收发器。所以,如果可以的话,请帮忙。当商家订购他们的产品时,我会给他们发一封邮件。
我已经下载并安装了phpmailer在我的代码,完成了所有的包括指导在回购,但当我运行代码没有找到GET SMTP类。请问我的疏忽是什么?谢谢你。
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
function mail_merchants_and_shoply($cart_code){
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
$conn = $this -> connection;
$merchant_sent = false;
$shoply_sent = false;
$query = mysqli_query($conn, "SELECT * FROM business_account ORDER BY business_id DESC")or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query)){
$id = $row['business_id'];
$stat = "NEW";
$email = $row['email_add'];
$name = $row['business_name'];
$c_query = mysqli_query($conn, "SELECT * FROM cart_db WHERE business_id = '$id' AND cart_code = '".mysqli_real_escape_string($conn, $cart_code)."'")or die(mysqli_error($conn));
$merchant_mail_body = "<h2>NEW ORDER</h2><ul>";
while($arr = mysqli_fetch_array($c_query)){
$p_id = $arr['product_id'];
$p_query = mysqli_query($conn, "SELECT * FROM products WHERE product_id = '$p_id'")or die(mysqli_error($conn));
$p_arr = mysqli_fetch_array($p_query);
$image = $p_arr['product_image1_url'];
$price = $p_arr['product_price'];
$name = $p_arr['product_name'];
$image = "<img src = 'https://www.shoply.ng/backend/$image' style = 'max-width: 200px; max-height: 200px'/>";
$merchant_mail_body.="<li>$name</li> <li>$image</li> <li> $price</li>";
}
$merchant_mail_body .="</ul>";
$mail_subject = "NEW SHOPLY ORDER";
// $mailHead = implode("rn", ["MIME-Version:1.0","Content-type:text/html; charset =utf-8"]);
$mailHead = "MIME-Version: 1.0" . "rn";
$mailHead .= "Content-type:text/html;charset=UTF-8" . "rn";
// Additional headers
$mailHead .= 'From: Shoply<orders@shoply.ng>' . "rn";
// if(mail($email, $mail_subject, $merchant_mail_body, $mailHead)){
// $merchant_sent = true;
// }
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'mail.shoply.ng'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'myusername'; //SMTP username
$mail->Password = 'mypassword'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('orders@shoply.ng', 'SHOPLY');
$mail->addAddress('el1harolds@gmail.com', ''); //Add a recipient
// $mail->addAddress('ellen@example.com'); //Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'NEW SHOPLY ORDER';
$mail->Body = $merchant_mail_body;
$mail->send();
$merchant_sent = true;
} catch (Exception $e) {
$merchant_sent = false;
}
$s_query = mysqli_query($conn, "SELECT * FROM cart_db WHERE cart_code = '".mysqli_real_escape_string($conn, $cart_code)."'")or die(mysqli_error($conn));
$o_query = mysqli_query($conn, "SELECT * FROM orders WHERE order_code = '".mysqli_real_escape_string($conn, $cart_code)."'")or die(mysqli_error($conn));
$o_arr = mysqli_fetch_array($o_query);
$location = $o_arr['delivery_location'];
$phone = $o_arr['delivery_phone'];
$user_fname = $_SESSION['user_fname'];
$shoply_mail_body = "<h2>NEW ORDER</h2> FROM: $user_fname <ul>
<li><b>Address:</b> $location</li>
<li><b>Phone Number: </b>$phone</li>";
// $f_arr = mysqli_fetch_array($s_query);
// $customer_id = $f_arr['customer_id'];
// $c_query = mysqli_query($conn, "SELECT * FROM customers WHERE customer_id= '$customer_id'")or die(mysqli_error($conn));
// $c_arr[]
while($row = mysqli_fetch_array($s_query)){
$p_id = $row['product_id'];
$p_query = mysqli_query($conn, "SELECT * FROM products WHERE product_id = '$p_id'")or die(mysqli_error($conn));
$p_arr = mysqli_fetch_array($p_query);
$image = $p_arr['product_image1_url'];
$price = $p_arr['product_price'];
$name = $p_arr['product_name'];
$image = "<img src = 'https://www.shoply.ng/backend/$image' style = 'max-width: 200px; max-height: 200px'/>";
$shoply_mail_body.="<li>$name</li> <li>$image</li> <li> $price</li>";
}
$shoply_mail_body .="</ul>";
// $email = "sales@shoply.ng";
// $mail_subject = "NEW SHOPLY ORDER";
// // $mail_head = implode("rn", ["MIME-Version:1.0","Content-type:text/html; charset =utf-8"]);
// $headers = "MIME-Version: 1.0" . "rn";
// $headers .= "Content-type:text/html;charset=UTF-8" . "rn";
// // Additional headers
// $headers .= 'From: Shoply<orders@shoply.ng>' . "rn";
// if(mail($email, $mail_subject, $shoply_mail_body, $headers)){
// $shoply_sent = true;
// }
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'mail.shoply.ng'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'myusername'; //SMTP username
$mail->Password = 'mypassword'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('orders@shoply.ng', 'SHOPLY');
$mail->addAddress('sales@shoply.ng', ''); //Add a recipient
// $mail->addAddress('ellen@example.com'); //Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'NEW ORDER';
$mail->Body = $shoply_mail_body;
$mail->send();
$shoply_sent = true;
return true;
} catch (Exception $e) {
return false;
}
}
}
您错过了名称空间导入。在顶部添加:
use PHPMailerPHPMailerSMTP;
单独学习使用composer。你的函数会在第二次调用时崩溃,因为你会加载重复的类。