Mailing from php script
Posted: Wed Mar 09, 2011 8:30 am
Hi all,
I am working on a script that searches for a patients email address and emails a new password to them when they have forgotten their current one.
I am not getting any errors and I also get to display that the email has been sent to that person but when checking if the email has been received, it has not...
I am with virgin media and the port im using is port 25 which I have set up using the ini_set() function within the script.
Any help is appreciated..
I am working on a script that searches for a patients email address and emails a new password to them when they have forgotten their current one.
I am not getting any errors and I also get to display that the email has been sent to that person but when checking if the email has been received, it has not...
I am with virgin media and the port im using is port 25 which I have set up using the ini_set() function within the script.
Code: Select all
<?php
require_once("connect.php");
$rsent = "";
$error = "";
//This code runs if the form has been submitted
if (isset($_POST['submit']))
{
// check for valid email address
$email = $_POST['remail'];
$pattern = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
if (!preg_match($pattern, trim($email))) {
$error[] = 'Please enter a valid email address';
}
// checks if the username is in use
$check = mysql_query("SELECT email FROM patient WHERE email = '$email'")or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 == 0) {
$error[] = 'Sorry, we cannot find your account details please try another email address.';
}
// if no errors then carry on
if (!$error) {
$query = mysql_query("SELECT username FROM patient WHERE email = '$email' ")or die (mysql_error());
$r = mysql_fetch_object($query);
//create a new random password
$password = substr(md5(uniqid(rand(),1)),3,10);
$pass = md5($password); //encrypted version for database entry
//send email
ini_set ( "SMTP", "smtp.virgin.net" );
ini_set("smtp_port", "465");
$to = "$email";
$subject = "Account Details Recovery";
$body = "Hi $r->username, \n\n you or someone else have requested your account details. \n\n Here is your account information please keep this as you may need this at a later stage. \n\nYour username is $r->username \n\n your password is $password \n\n Your password has been reset please login and change your password to something more rememberable.\n\n Regards Site Admin";
$additionalheaders = "From: <draven61@hotmail.com>\r\n";
$additionalheaders .= "Reply-To: noprely@domain.com";
mail($to, $subject, $body, $additionalheaders);
//update database
$sql = mysql_query("UPDATE patient SET password='$pass' WHERE email = '$email'")or die (mysql_error());
$rsent = true;
}// close errors
}// close if form sent
//show any errors
if (!empty($error))
{
$i = 0;
while ($i < count($error)){
echo "<div class=\"msg-error\">".$error[$i]."</div>";
$i ++;}
}// close if empty errors
if ($rsent == true){
echo "<p>You have been sent an email with your account details to $email</p>\n";
} else {
echo "<p>Please enter your e-mail address. You will receive a new password via e-mail.</p>\n";
}
?>
<form action="forgot_password.php" method="post">
<p>Email Address: <input type="text" name="remail" size="50" maxlength="255">
<input type="submit" name="submit" value="Get New Password"></p>
</form>Any help is appreciated..