ianhaney Posted February 18, 2020 Share Posted February 18, 2020 I am trying to get phpmailer working but not having much luck with it. I am not receiving the email Below is the code I have <?php //index.php $error = ''; $quantity1 = ''; $totalprice1 = ''; $subtotal = ''; $postage = ''; $grandtotal = ''; $name = ''; $addressline1 = ''; $postcode = ''; $email = ''; function clean_text($string) { $string = trim($string); $string = stripslashes($string); $string = htmlspecialchars($string); return $string; } if(isset($_POST["submit"])) { if(empty($_POST["quantity1"])) { $error .= '<p><label class="text-danger">Please enter the quantity required</label></p>'; } if(empty($_POST["totalprice1"])) { $error .= '<p><label class="text-danger">Please enter the total price required</label></p>'; } if(empty($_POST["subtotal"])) { $error .= '<p><label class="text-danger">Please enter the subtotal</label></p>'; } if(empty($_POST["postage"])) { $error .= '<p><label class="text-danger">Please enter the postage</label></p>'; } if(empty($_POST["grandtotal"])) { $error .= '<p><label class="text-danger">Please enter the grand total</label></p>'; } if(empty($_POST["name"])) { $error .= '<p><label class="text-danger">Please Enter your name</label></p>'; } else { $name = clean_text($_POST["name"]); if(!preg_match("/^[a-zA-Z ]*$/",$name)) { $error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>'; } } if(empty($_POST["addressline1"])) { $error .= '<p><label class="text-danger">Please enter your address line 1</label></p>'; } if(empty($_POST["postcode"])) { $error .= '<p><label class="text-danger">Please enter your postcode</label></p>'; } if(empty($_POST["email"])) { $error .= '<p><label class="text-danger">Please Enter your Email</label></p>'; } else { $email = clean_text($_POST["email"]); if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $error .= '<p><label class="text-danger">Invalid email format</label></p>'; } } if($error == '') { require 'phpmailer/class.phpmailer.php'; $mail = new PHPMailer(true); $mail->SMTPDebug = 1; // to see exactly what's the issue $mail->IsSMTP(); //Sets Mailer to send message using SMTP $mail->Host = 'hostname'; //Sets the SMTP hosts $mail->Port = '587'; //Sets the default SMTP server port $mail->SMTPAuth = true; //Sets SMTP authentication. Utilizes the Username and Password variables $mail->Username = 'emailaddress'; //Sets SMTP username $mail->Password = 'emailaddresspassword'; //Sets SMTP password $mail->SMTPSecure = ''; //Sets connection prefix. Options are "", "ssl" or "tls" $mail->From = $_POST["email"]; //Sets the From email address for the message $mail->FromName = $_POST["name"]; //Sets the From name of the message $mail->AddAddress('emailaddress', 'Name');//Adds a "To" address $mail->AddCC($_POST["email"], $_POST["name"]); //Adds a "Cc" address $mail->WordWrap = 50; //Sets word wrapping on the body of the message to a given number of characters $mail->IsHTML(true); //Sets message type to HTML $mail->Subject = "Online Badge Order"; //Sets the Subject of the message $mail->Body = "The order information is below" . "<br><br>" . "Club One: " . $_POST["club1"] . "<br>" . "Country One: " . $_POST["country1"] . "<br>" . "Ref No: " . $_POST["refno1"] . "<br>" . "Quantity: " . "<br>" . $_POST["quantity1"] . "<br>" . "Total Price Line: " . "\243" . $_POST["totalprice1"] . "<br><br>" . "Club Two: " . $_POST["club2"] . "<br>" . "Country Two: " . $_POST["country2"] . "<br>" . "Ref No: " . $_POST["refno2"] . "<br>" . "Quantity: " . $_POST["quantity2"] . "<br>" . "Total Price Line: " . "\243".$_POST["totalprice2"] . "<br><br>" . "Club Three: " . $_POST["club3"] . "<br>" . "Country Three: " . $_POST["country3"] . "<br>" . "Ref No: " . $_POST["refno3"] . "<br>" . "Quantity: " . $_POST["quantity3"] . "<br>" . "Total Price Line: " . "\243".$_POST["totalprice3"] . "<br><br>" . "Club Four: " . $_POST["club4"] . "<br>" . "Country Four: " . $_POST["country4"] . "<br>" . "Ref No: " . $_POST["refno4"] . "<br>" . "Quantity: " . $_POST["quantity4"] . "<br>" . "Total Price Line: " . "\243".$_POST["totalprice4"] . "<br><br>" . "Club Five: " . $_POST["club5"] . "<br>" . "Country Five: " . $_POST["country5"] . "<br>" . "Ref No: " . $_POST["refno5"] . "<br>" . "Quantity: " . $_POST["quantity5"] . "<br>" . "Total Price Line: " . "\243".$_POST["totalprice5"] . "<br><br>" . "Club Six: " . $_POST["club6"] . "<br>" . "Country Six: " . $_POST["country6"] . "<br>" . "Ref No: " . $_POST["refno6"] . "<br>" . "Quantity: " . $_POST["quantity6"] . "<br>" . "Total Price Line: " . "\243".$_POST["totalprice6"] . "<br><br>" . "Club Seven: " . $_POST["club7"] . "<br>" . "Country Seven: " . $_POST["country7"] . "<br>" . "Ref No: " . $_POST["refno7"] . "<br>" . "Quantity: " . $_POST["quantity7"] . "<br>" . "Total Price Line: " . "\243".$_POST["totalprice7"] . "<br><br>" . "Club Eight: " . $_POST["club8"] . "<br>" . "Country Eight: " . $_POST["country8"] . "<br>" . "Ref No: " . $_POST["refno8"] . "<br>" . "Quantity: " . $_POST["quantity8"] . "<br>" . "Total Price Line: " . "\243".$_POST["totalprice8"] . "<br><br>" . "Club Nine: " . $_POST["club9"] . "<br>" . "Country Nine: " . $_POST["country9"] . "<br>" . "Ref No: " . $_POST["refno9"] . "<br>" . "Quantity: " . $_POST["quantity9"] . "<br>" . "Total Price Line: " . "\243".$_POST["totalprice9"] . "<br><br>" . "Club Ten: " . $_POST["club10"] . "<br>" . "Country Ten: " . $_POST["country10"] . "<br>" . "Ref No: " . $_POST["refno10"] . "<br>" . "Quantity: " . $_POST["quantity10"] . "<br>" . "Total Price Line: " . "\243".$_POST["totalprice10"]; //An HTML or plain text message body if($mail->Send()) //Send an Email. Return true on success or false on error { header('Location:thank-you.html'); } else { $error = '<label class="text-danger">There is an Error</label>'; } $quantity1 = ''; $totalprice1 = ''; $subtotal = ''; $postage = ''; $grandtotal = ''; $name = ''; $addressline1 = ''; $postcode = ''; $email = ''; } } ?> Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted February 18, 2020 Share Posted February 18, 2020 (edited) ian, I worked with a veteran on this very issue for 3 days a long time ago. we found out that the package I downloaded from github was coded incorrectly. he had to use another method to put the body of the HTML message together. can you possibly get anything from this script of mine? https://drive.google.com/file/d/1Cj8RIDPOUe8hWUBUQjtjGUjKgQG2u3t_/view?usp=sharing the new code from him, that he wrote to solve the problem of me not getting the message and/or the message coming incorrectly was this portion: $temp_HTML = "<!DOCTYPE html> <html> <head> <title>Website Form Submission</title> </head> <body> <p>$email_body</p> </body> </html>"; he also had to add a few other lines of REQUIRE() function code. Edited February 18, 2020 by ajetrumpet Quote Link to comment Share on other sites More sharing options...
ianhaney Posted February 18, 2020 Author Share Posted February 18, 2020 I removed the line below and redirected to the thank you page but the email is still not coming through $mail->IsSMTP(); Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted February 18, 2020 Share Posted February 18, 2020 (edited) did you check the spam folder? additionally, perhaps you need more lines of REQUIRE() code, like in my script? don't quote me, ian, I'm no PHP expert. Edited February 18, 2020 by ajetrumpet Quote Link to comment Share on other sites More sharing options...
ianhaney Posted February 18, 2020 Author Share Posted February 18, 2020 I have just managed to get it working, got a little delay of about 10 seconds or so but main thing I managed to get it working, shall I post the code just in case anyone else needs it Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted February 18, 2020 Share Posted February 18, 2020 that's always a good idea, ian. good for you. Quote Link to comment Share on other sites More sharing options...
ianhaney Posted February 18, 2020 Author Share Posted February 18, 2020 No probs, below is the coding I have <?php // PHPMailer classes into the global namespace use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // Base files require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; // create object of PHPMailer class with boolean parameter which sets/unsets exception. $mail = new PHPMailer(true); try { //$mail->isSMTP(); // using SMTP protocol $mail->Host = 'hostname'; // SMTP host as gmail $mail->SMTPAuth = true; // enable smtp authentication $mail->Username = 'emailaddress'; // sender gmail host $mail->Password = 'emailaddresspassword'; // sender gmail host password $mail->SMTPSecure = 'ssl'; // for encrypted connection $mail->Port = 587; // port for SMTP $mail->setFrom('emailaddress', "Sender"); // sender's email and name $mail->addAddress('emailaddress', "Receiver"); // receiver's email and name $mail->Subject = 'New Online Order'; $mail->Body = "A new order has been received. The order information is below" . "\r\n\r\n" . "Club One: " . $_POST["club1"] . "\r\n" . "Country One: " . $_POST["country1"] . "\r\n" . "Ref No: " . $_POST["refno1"] . "\r\n" . "Quantity: " . $_POST["quantity1"] . "\r\n" . "Total Price Line: " . "\243" . $_POST["totalprice1"] . "\r\n\r\n" . "Club Two: " . $_POST["club2"] . "\r\n" . "Country Two: " . $_POST["country2"] . "\r\n" . "Ref No: " . $_POST["refno2"] . "\r\n" . "Quantity: " . $_POST["quantity2"] . "\r\n" . "Total Price Line: " . "\243".$_POST["totalprice2"] . "\r\n\r\n" . "Club Three: " . $_POST["club3"] . "\r\n" . "Country Three: " . $_POST["country3"] . "\r\n" . "Ref No: " . $_POST["refno3"] . "\r\n" . "Quantity: " . $_POST["quantity3"] . "\r\n" . "Total Price Line: " . "\243".$_POST["totalprice3"] . "\r\n\r\n" . "Club Four: " . $_POST["club4"] . "\r\n" . "Country Four: " . $_POST["country4"] . "\r\n" . "Ref No: " . $_POST["refno4"] . "\r\n" . "Quantity: " . $_POST["quantity4"] . "\r\n" . "Total Price Line: " . "\243".$_POST["totalprice4"] . "\r\n\r\n" . "Club Five: " . $_POST["club5"] . "\r\n" . "Country Five: " . $_POST["country5"] . "\r\n" . "Ref No: " . $_POST["refno5"] . "\r\n" . "Quantity: " . $_POST["quantity5"] . "\r\n" . "Total Price Line: " . "\243".$_POST["totalprice5"] . "\r\n\r\n" . "Club Six: " . $_POST["club6"] . "\r\n" . "Country Six: " . $_POST["country6"] . "\r\n" . "Ref No: " . $_POST["refno6"] . "\r\n" . "Quantity: " . $_POST["quantity6"] . "\r\n" . "Total Price Line: " . "\243".$_POST["totalprice6"] . "\r\n\r\n" . "Club Seven: " . $_POST["club7"] . "\r\n" . "Country Seven: " . $_POST["country7"] . "\r\n" . "Ref No: " . $_POST["refno7"] . "\r\n" . "Quantity: " . $_POST["quantity7"] . "\r\n" . "Total Price Line: " . "\243".$_POST["totalprice7"] . "\r\n\r\n" . "Club Eight: " . $_POST["club8"] . "\r\n" . "Country Eight: " . $_POST["country8"] . "\r\n" . "Ref No: " . $_POST["refno8"] . "\r\n" . "Quantity: " . $_POST["quantity8"] . "\r\n" . "Total Price Line: " . "\243".$_POST["totalprice8"] . "\r\n\r\n" . "Club Nine: " . $_POST["club9"] . "\r\n" . "Country Nine: " . $_POST["country9"] . "\r\n" . "Ref No: " . $_POST["refno9"] . "\r\n" . "Quantity: " . $_POST["quantity9"] . "\r\n" . "Total Price Line: " . "\243".$_POST["totalprice9"] . "\r\n\r\n" . "Club Ten: " . $_POST["club10"] . "\r\n" . "Country Ten: " . $_POST["country10"] . "\r\n" . "Ref No: " . $_POST["refno10"] . "\r\n" . "Quantity: " . $_POST["quantity10"] . "\r\n" . "Total Price Line: " . "\243".$_POST["totalprice10"] . "\r\n\r\n" . "Name: " . $_POST["name"] . "\r\n" . "Address Line 1: " . $_POST["addressline1"] . "\r\n" . "Address Line 2: " . $_POST["addressline2"] . "\r\n" . "Postcode: " . $_POST["postcode"] . "\r\n" . "Email: " . $_POST["email"] . "\r\n\r\n" . "Catalogue Subscription: " . $_POST["catalogue"]; $mail->send(); header('Location:thank-you.html'); } catch (Exception $e) { // handle error. echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.