Mail() not sending message

am trying to create a forgot password system and I want to send link to user email address but am not getting any message in the receiving email address box. The mail() IF statement echo success and no message comes in…

below is the code am trying…Pls kindly help

<?php if(isset($_POST['changePass'])) { $emailAdd = $_POST['email']; function getRandomString($length) { $validCharacters = "ABCDEFnmbvmn456789"; $validCharNumber = strlen($validCharacters); $result = ""; for ($i = 0; $i < $length; $i++) { $index = mt_rand(0, $validCharNumber - 1); $result .= $validCharacters[$index]; } return $result;} $token = getRandomString(10); include db.php'; $tokn = 'INSERT INTO tokens SET token = :token, email = :email' ; $tok = $pdo->prepare($tokn); $tok->bindParam(':token', $token); $tok->bindValue(':email', $emailAdd); $tok->execute(); ////////////////////// $subject = "Forgot Password on example.com"; $uri = 'http://'. $_SERVER['HTTP_HOST']; $message = ' Forgot Password For example.com

Click on the given link to reset your password Reset Password

'; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= 'From: noreply@example.com>' . "\r\n"; $headers .= 'Cc: Admin@example.com' . "\r\n"; $headers .= "Reply To: replyto@example.com" . "\r\n"; if(mail($emailAdd, $subject, $message, $headers)){ echo "We have sent the password reset link to your email address ".$emailAdd.""; } } //changePass

mail function doesn’t work on Windows and if you happen to get really lucky on configuring it, it’ll most likely break soon. mail’s alternative is PHPMailer. I’ve had this issue before. I had a successful configured sendmail, but for some reason, it broke the following week so I turned to using PHPMailer. It’s not really that hard to use PHPMailer.

Steps on how to get PHPMailer to work.

  1. Go to https://github.com/PHPMailer/PHPMailer and download the library.
  2. Extract the library in a folder you want. Something like /vendor/PHPMailer/.
  3. In your db.php or your current snippet, do something like require('/vendor/PHPMailer/PHPMailerAutoload.php');
  4. You can use the snippet below.

$mail = new PHPMailer;

//Set PHPMailer to use SMTP.
$mail->isSMTP();

//Set SMTP host name
$mail->Host = "SMTP_SERVER"; // The SMTP server like smtp.live.com or smtp.gmail.com

//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;

//Provide username and password
$mail->Username = "SMTP_EMAIL"; // Email
$mail->Password = "SMTP_PASSWORD"; // Email password

//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";

//Set TCP port to connect to
$mail->Port = "587";

$mail->From = "noreply@example.com";
$mail->FromName = "Noreply";

$mail->addAddress($emailAdd);

$mail->isHTML(true);

$uri = 'http://'. $_SERVER['HTTP_HOST'];
$mail->Subject = "Forgot Password on example.com";
$mail->Body = "<html>
<head>
<title>Forgot Password For example.com</title>
</head>
<body>
<p>Click on the given link to reset your password <a href=\"".$uri."/resetexample.php?token=".$token."\">Reset 

Password</a></p>

</body>
</html>";

if(!$mail->send()) {

    echo "Mailer Error: " . $mail->ErrorInfo;

} else {

    echo "Message has been sent successfully";

}

It should work. Let me know if it doesn’t.

1 Like

@spaceshiptrooper

Am trying to use this on a live website and is not working…I contact my hosting for SMTP setting, I got a reply for SMTP server setting…that the STMP server is localhost…and I can use any email address and password on my domain for the email and email password…and the port is 25…I try 25 and the port you gave, still not working…

require('../PHPMailer/PHPMailerAutoload.php');

$mail = new PHPMailer;

//Set PHPMailer to use SMTP.
$mail->isSMTP();

//Set SMTP host name
$mail->Host = "localhost"; // The SMTP server like smtp.live.com or smtp.gmail.com

//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;

//Provide username and password
$mail->Username = "support@example.com"; // Email
$mail->Password = "34nerHH"; // Email password

//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";

//Set TCP port to connect to
$mail->Port = "587";

and if I use the PHP mail() it will send and sometimes is will take up to 10 to 20mins before any message comes in or sometimes no message comes in at all…Can I make it send the message to email address the moment the message was sent?

Kindly help me out…I really want to get this to work…

Thanks for your time.

Well, you have to actually use the smtp server configuration. It can’t just be localhost because localhost is only a local call. I recommend using gmail or hotmail. Anything that you use in your PHP will never be displayed or given to any users whether they want to or not.

Well it certainly COULD be a localhost, if the hosting company is running an SMTP agent on the box.

@aosworks so when you set the port to 25, have you specified the rest of the settings (From, FromName, isHTML, etc)? Have you tried not using SMTPSecure?

Try setting $mail->SMTPDebug = 2; and seeing what it tells you.

As far as “make it send the message to email address the moment the message was sent”… well, no. Your mail server will have a process queue, which waits until whatever time it’s configured to wait, then it’s got to be sent across the web to the appropriate MX agent on the other side, which has to decode it and deliver it to the correct mailbox in it’s own system.

It shouldnt generally take 10 to 20 minutes, more like 1-5, but there will always be a delay that is out of your control to influence.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.