Daniel Faulkner
Daniel Faulkner

Reputation: 59

PHP Mailer crashes website

When an email is sent from the site the whole site crashes! Is there any way with the code below to make it run as a cron job maybe? The problem is I'm not to familiar with cron jobs and am finding it hard to find a working solution online.

Heres the code responsible for sending mail:

<?php

define('DB_NAME', 'daniel_login');
define('DB_USER', 'daniel_login');
define('DB_PASSWORD', '#####');
define('DB_HOST', 'localhost');

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn) {
     die('Could not connect: ' . mysqli_connect_error());
     }

$recovery = $_POST['recovery'];     

$sql = "SELECT * FROM members WHERE username = '$recovery'"; 
$result=mysqli_query($conn, $sql);

require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{

   $mailto = $recovery;
   $mailSub = "" . $row['firstname'] . "'s Password Reminder";
   $mailMsg = "### CONTENT ###";
   $mail->IsSMTP();
   $mail ->SMTPDebug = 0;
   $mail ->SMTPAuth = true;
   $mail ->SMTPSecure = 'ssl';
   $mail ->Host = "dev.#####.co.uk";
   $mail ->Port = 465; // or 587
   $mail ->IsHTML(true);
   $mail ->Username = "forgot@dev.#####.co.uk";
   $mail ->Password = "#####";
   $mail ->SetFrom("forgot@dev.#####.co.uk");
   $mail ->Subject = $mailSub;
   $mail ->Body = $mailMsg;
   $mail ->AddAddress($mailto);

   if(!$mail->Send())
   {
       include 'failed.php';
   }
   else
   {
       include 'success.php';
   }
}
mysqli_close($conn);

Upvotes: 2

Views: 958

Answers (1)

Mr Hery
Mr Hery

Reputation: 870

I know it's been a year now, I hope can help other people.

I have tested on several servers with different ports. And some of those servers facing the website breakdown problem.

In my case, the server not set up for port 465 even it's said from CPanel every time we create new email. But the problem is, PHPMailer cannot reach xx.xx.xx.xx:465 because its a close port.

In my case, I solved it by changing to port 587. But before that, make sure your SMTP service are up and ready by try to send a mail on port 25 (default SMTP port).

Either way, you may scan all the servers ports (SMTP related port) and see which is open and available.

The website only break at the current hosting account only and not affecting other hosting account. So if you running the website under the "Reseller Hosting", the WHM will break too because your billing is in the same account. What I ve done, create another hosting to host mail service, so if the mail hosting breakdown, it will not affect the reseller / customer account.

Upvotes: 1

Related Questions