Link to home
Start Free TrialLog in
Avatar of Michelle Jackson
Michelle Jackson

asked on

PHP Mail says it was sent but never actually sends

I am trying to build a PHP app to send query results to multiple users. When I run the program it states that the message was sent but it is not actually sent, I never receive it. I must be missing something here. Could someone please take a look and point me in the right direction?  I am using AMPPS to test this. Thanks.
<?php
//include("DBConstants.php");
$con = mysqli_connect ('xxx.xxx.xxx.xxx','loginxxx','passwordxxx');
if (!$con)
  {
  die('Could not connect:' . mysqli_error());
  }
  mysqli_select_db($con,"xxx");
  $result = mysqli_query($con,"SELECT * FROM logins WHERE logintime >=(CURRENT_DATE - INTERVAL 7 DAY)");
  $from = "xxx@xx.com";
  $host = "smtp.office365.com";
  $username = "xxxx";
  $password = "xxx";
  $port = "587";
  $to = "myemail@myemail.com";
  $subject = "Logins";
  $body = "<table border='1'>";
  $body .= "<tr>";
  $body .= "<th>LOGIN:</th>";
  $body .= "<th>TIME:</th>";
  
  $body .= "</tr>";
 while($row = mysqli_fetch_array($result)){
  $body .="<tr>";
  $body .="<td>" . $row['userid'] . "</td>";
  $body .="<td>" . $row['logintime'] . "</td>";
  
  $body .="</tr>";
  }
  $body .="</table>";
  mysqli_close($con);
  ;
  
  $headers = "Return-Path: $from\n";
  $headers .= "X-Sender: $from\n";
  $headers .= "From: $from\n";
  $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


  mail($to,$subject,$body,$headers);
  echo "Mail sent to myemail@myemail.com ";
?>

Open in new window

Avatar of David Favor
David Favor
Flag of United States of America image

With email...

Sending != Delivering

2x different beasts.

Sending is super easy.

https://www.experts-exchange.com/questions/29165628/Sending-Email-by-getting-email-address-from-database.html covers the absolute minimal checklist to begin the delivery process.

Then you may require warming up IPs + other related tasks.

If the above checklist is new to you, then subscribe to a Mail Relay service like https://MailGun.com to ensure your messages actually deliver.

Note: You can check your MTA logs to determine exact reason for failure.

Where MTA will be something like Sendmail, Postfix, EXIM, etc...
Avatar of Michelle Jackson
Michelle Jackson

ASKER

Thanks David Foster, I will take a look at it now.
You need to treat this type of authentication similar to a multi function device https://docs.microsoft.com/en-us/exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-office-3 

Based on this you need to send using tls and authenticated  https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging

Log into your admin portal and see if you can find any issues logged.
ASKER CERTIFIED SOLUTION
Avatar of Michelle Jackson
Michelle Jackson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial