PHP mail(activation link)

I have a code to send activation link to user email and click it for account activation. I having pproblem in connecting to mail server even I configured it using ini_set().Anyone please help me to reveal the problem,thanks…:slight_smile:

signup_acc.php


<?php

//Connect to mysql database
$conn = mysql_connect("localhost", "root", "") or die('Could not connect: ');
$db = mysql_select_db("movie_ticket_booking", $conn) or die('Could not select database');

//global variable
$table1 = "temp_members";
$table2 = "registered_member";

//Random confirmation code
$confirm_code = md5(uniqid(rand()));

//values sent from form
//$name = $_REQUEST['name'];
//$email = $_REQUEST['email'];
//$pass1 = $_REQUEST['pass1'];
//$pass2 = $_REQUEST['pass2'];
//$tel = $_REQUEST['tel'];
//$address = $_REQUEST['address'];


//This code runs if the form has been submitted
if (isset($_POST['submit']))
{

    //This code makes sure that did not leave any fields blank
    //if (!$_POST['name'] | !$_POST['email'] | !$_POST['pass1'] | !$_POST['pass2'] | !$_POST['tel'] | !$_POST['address'])
    // {

    //  die('You did not complete all of the required fields');
    //}


    // checks if the email address is alredy registered
    //if (!get_magic_quotes_gpc())
    //{
    //$_POST['email'] = addslashes($_POST['email']);
    //}
    $emailcheck = $_POST['email'];
    $check = mysql_query("SELECT email FROM $table2 WHERE email = '$emailcheck'") or
        die(mysql_error());
    $check2 = mysql_num_rows($check);

    //if the name exists it gives an error
    if ($check2 != 0)
    {
        die('Sorry, the email address' . $_POST['email'] . ' is already registered.');
    }

    // this makes sure both passwords entered match
    //if ($pass1 != $pass2)
    //{
    //die('Your passwords did not match.');
    //}

    // encrypt the password
    $pass1 = md5($pass1);
    //if (!get_magic_quotes_gpc())
    //{
    //pass1 = addslashes($pass1);
    //$_POST['member_id'] = addslashes($_POST['member_id']);

    //Insert record to database
    $insert = mysql_query("INSERT INTO $table1(confirm_code,name,email,password,tel,address)
		       VALUES ('" . $_POST['confirm_code'] . "','" . $_POST['name'] . "','" .
        $_POST['email'] . "',
			   '" . $_POST['pass1'] . "','" . $_POST['tel'] . "','" . $_POST['address'] .
        "')") or die(mysql_error());


    // }

    //if successfully inserted,send confirmation link to email
    if ($insert)
    {

        echo "thank you!please check your email for confirmation.";

        //send email to who?
        $to = $email;

        //email subject
        $subject = 'Your confirmation link here';

        //From
        $headers = 'From: admin@localhost.com' . "\\r\
" .
                   'Reply-To: admin@localhost.com' . "\\r\
" .'X -Mailer: PHP/' . phpversion();


        //Message content
        $message = "Your confirmation link\\r\
";
        $message .= "Click on this link to activate your sccount\\r\
";
        $message .= "http://localhost/www/confirm.php?passkey=$confirm_code";

        //send mail
        //SMTP setting
        ini_set(SMTP,'localhost');
        ini_set(smtp_port,'25');
        ini_set(sendmail_from,'admin@localhost.com');
        $sentmail = mail($to, $subject, $message, $headers);

    }

    //if not found
    else
    {
        echo "Not found your email in our database";
    }

    //if your email successfully sent
    if ($sentmail)
    {
        echo "Your confirmation link has been sent to your email address.";
    }

    else
    {
        echo "Cannot send confirmation link to your email address.";
    }

}
?>

confirm.php


<?php

//Connect to mysql database
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("movie_ticket_booking", $conn) or die(mysql_error());

//Passkey that got from link
$passkey = $_GET['passkey'];

//global variable
$table1 = "temp_members";
$table2 = "registered_member";

//Retrieve data from table where row that match this passkey
$sql1 = "SELECT * FROM $table1 WHERE confirm_code='$passkey'";
$result1 = mysql_query($sql1);

//If successfully queried
if ($result1)
{

    //Count how many has this passkey
    $count = mysql_num_rows($result1);

    //if found this passkey,retieve data from table temp_members
    if ($count == 1)
    {

        $rows = mysql_fetch_array($result1);
        $name = $rows['name'];
        $email = $rows['email'];
        $pass1 = $rows['pass1'];
        $tel = $rows['tel'];
        $address = $rows['address'];



        //Insert data that retrieve from "temp_members" into "registered_member"
        $sql2 = "INSERT INTO $table2(name,email,password,tel,address) VALUES('$name','$email','$pass1','$tel','$address')";
        $result2 = mysql_query($sql2);
    }

    else
    {
        echo "Wrong activation code";
    }

    //if successfully moved data,display message account has been activated and delete confirmation code from
    //"temp_members"

    if ($result2)
    {
        echo "Your account have been activated";

        //Delete user information form "temp_members" that has the passkey
        $sql3 = "DELETE FROM $table1 WHERE confirm_code = '$passkey'";
        $result3 = mysql_query($sql3);
    }


}

?>

I have a code to send activation link to user email and click it for account activation. I having pproblem in connecting to mail server even I configured it using ini_set().Anyone please help me to reveal the problem,thanks…:slight_smile:

signup_acc.php


<?php

//Connect to mysql database
$conn = mysql_connect("localhost", "root", "") or die('Could not connect: ');
$db = mysql_select_db("movie_ticket_booking", $conn) or die('Could not select database');

//global variable
$table1 = "temp_members";
$table2 = "registered_member";

//Random confirmation code
$confirm_code = md5(uniqid(rand()));

//values sent from form
//$name = $_REQUEST['name'];
//$email = $_REQUEST['email'];
//$pass1 = $_REQUEST['pass1'];
//$pass2 = $_REQUEST['pass2'];
//$tel = $_REQUEST['tel'];
//$address = $_REQUEST['address'];


//This code runs if the form has been submitted
if (isset($_POST['submit']))
{

    //This code makes sure that did not leave any fields blank
    //if (!$_POST['name'] | !$_POST['email'] | !$_POST['pass1'] | !$_POST['pass2'] | !$_POST['tel'] | !$_POST['address'])
    // {

    //  die('You did not complete all of the required fields');
    //}


    // checks if the email address is alredy registered
    //if (!get_magic_quotes_gpc())
    //{
    //$_POST['email'] = addslashes($_POST['email']);
    //}
    $emailcheck = $_POST['email'];
    $check = mysql_query("SELECT email FROM $table2 WHERE email = '$emailcheck'") or
        die(mysql_error());
    $check2 = mysql_num_rows($check);

    //if the name exists it gives an error
    if ($check2 != 0)
    {
        die('Sorry, the email address' . $_POST['email'] . ' is already registered.');
    }

    // this makes sure both passwords entered match
    //if ($pass1 != $pass2)
    //{
    //die('Your passwords did not match.');
    //}

    // encrypt the password
    $pass1 = md5($pass1);
    //if (!get_magic_quotes_gpc())
    //{
    //pass1 = addslashes($pass1);
    //$_POST['member_id'] = addslashes($_POST['member_id']);

    //Insert record to database
    $insert = mysql_query("INSERT INTO $table1(confirm_code,name,email,password,tel,address)
		       VALUES ('" . $_POST['confirm_code'] . "','" . $_POST['name'] . "','" .
        $_POST['email'] . "',
			   '" . $_POST['pass1'] . "','" . $_POST['tel'] . "','" . $_POST['address'] .
        "')") or die(mysql_error());


    // }

    //if successfully inserted,send confirmation link to email
    if ($insert)
    {

        echo "thank you!please check your email for confirmation.";

        //send email to who?
        $to = $email;

        //email subject
        $subject = 'Your confirmation link here';

        //From
        $headers = 'From: admin@localhost.com' . "\\r\
" .
                   'Reply-To: admin@localhost.com' . "\\r\
" .'X -Mailer: PHP/' . phpversion();


        //Message content
        $message = "Your confirmation link\\r\
";
        $message .= "Click on this link to activate your sccount\\r\
";
        $message .= "http://localhost/www/confirm.php?passkey=$confirm_code";

        //send mail
        //SMTP setting
        ini_set(SMTP,'localhost');
        ini_set(smtp_port,'25');
        ini_set(sendmail_from,'admin@localhost.com');
        $sentmail = mail($to, $subject, $message, $headers);

    }

    //if not found
    else
    {
        echo "Not found your email in our database";
    }

    //if your email successfully sent
    if ($sentmail)
    {
        echo "Your confirmation link has been sent to your email address.";
    }

    else
    {
        echo "Cannot send confirmation link to your email address.";
    }

}
?>

confirm.php


<?php

//Connect to mysql database
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("movie_ticket_booking", $conn) or die(mysql_error());

//Passkey that got from link
$passkey = $_GET['passkey'];

//global variable
$table1 = "temp_members";
$table2 = "registered_member";

//Retrieve data from table where row that match this passkey
$sql1 = "SELECT * FROM $table1 WHERE confirm_code='$passkey'";
$result1 = mysql_query($sql1);

//If successfully queried
if ($result1)
{

    //Count how many has this passkey
    $count = mysql_num_rows($result1);

    //if found this passkey,retieve data from table temp_members
    if ($count == 1)
    {

        $rows = mysql_fetch_array($result1);
        $name = $rows['name'];
        $email = $rows['email'];
        $pass1 = $rows['pass1'];
        $tel = $rows['tel'];
        $address = $rows['address'];



        //Insert data that retrieve from "temp_members" into "registered_member"
        $sql2 = "INSERT INTO $table2(name,email,password,tel,address) VALUES('$name','$email','$pass1','$tel','$address')";
        $result2 = mysql_query($sql2);
    }

    else
    {
        echo "Wrong activation code";
    }

    //if successfully moved data,display message account has been activated and delete confirmation code from
    //"temp_members"

    if ($result2)
    {
        echo "Your account have been activated";

        //Delete user information form "temp_members" that has the passkey
        $sql3 = "DELETE FROM $table1 WHERE confirm_code = '$passkey'";
        $result3 = mysql_query($sql3);
    }


}

?>

Have you configured the mail servers correctly?

Have you tried sending out the email with a SMTP that you know is working (your ISP email for example)? This may sound stupid, but are you sure there’s a SMTP server up&running on your machine locally?

If not, there are a bunch of free ones, or at least demos, which you can find by googling.

the correct syntax is:


ini_set("SMTP","localhost");
ini_set("smtp_port",25);
ini_set("sendmail_from","admin@localhost.com");

Thanks for your help.I’ve modified it but problem remain.Now I using a tool "Local SMTP server Pro"which made SMTP connection possible but the following shown:

Warning: mail() [function.mail]: SMTP server response: 503 in D:\elearning\Project\Tool Kit\xampp\htdocs\www\signup_acc.php on line 97

Can I configure Outlook Express as mail server of my localhost?I have tried some steps maybe bug come from there…

Is the configuration correct?What should I put in account name and password?Is it my ISP account?Thanks…

Thanks for your help.I’ve modified it but problem remain.Now I using a tool "Local SMTP server Pro"which made SMTP connection possible but the following shown:

Warning: mail() [function.mail]: SMTP server response: 503 in D:\elearning\Project\Tool Kit\xampp\htdocs\www\signup_acc.php on line 97

Can I configure Outlook Express as mail server of my localhost?I have tried some steps maybe bug come from there…

Incoming mail(POP3):localhost
Outgoing mail(SMTP):localhost

Is the configuration correct?What should I put in account name and password fields?Is it my ISP account?Should I check “My Server Requires Authentication” and “Log on using Secure Password Authentication”??
Thanks…

Outlook Express as mail server? No
503 This mail server requires authentication… if your ISP requires authentication, yes.
For sending email, I recommend swiftmailer

That error means that you are trying to connect to an SMTP server that requires auth (username and password) without providing them, and your access is denied.
If you have a SMTP server locally, check it’s settings, there should be no password configured, since there is no way of sending an username or password for the SMTP server with ini_set(). However this is dangerous, since you’re basically becoming an open relay SMTP which can be used to spam people and will eventually get you blacklisted.

I have a different sugestion, use PHPMailer, it’s really easy to set up and it allows you to connect to password protected SMTP servers, specify a back up in case the main is down, etc.

http://phpmailer.sourceforge.net/

I am using PHPMailer for sending mail and configure SMTP authentication but email not sent yet and shown:

Mailer Error: Language string failed to load: data_not_accepted

I have put phpmailer.lang-en.php under htdocs folder already,so what mighht cause this error?Thanks…

I am using PHPMailer for sending mail and configure SMTP authentication but email not sent yet and shown:

Mailer Error: Language string failed to load: data_not_accepted

I have put phpmailer.lang-en.php under htdocs folder already,so what mighht cause this error?Thanks…

Well, that’s two errors over there.

For start, you didn’t place the language file in your PHP include path, so the error message defaults back to English. That’s nothing major, so we can just skip this for now.

The second error to my knowledge usually has something to do with attachments, so that’s a bit odd.

Could you post your new code over here?

This is my new code with SMTP setting:
signup_acc.php


<?php

//Connect to mysql database
$conn = mysql_connect("localhost", "root", "") or die('Could not connect: ');
$db = mysql_select_db("movie_ticket_booking", $conn) or die('Could not select database');

//global variable
$table1 = "temp_members";
$table2 = "registered_member";

//Random confirmation code
$confirm_code = md5(uniqid(rand()));

//This code runs if the form has been submitted
if (isset($_POST['submit']))
{

    // checks if the email address is alredy registered
    //if (!get_magic_quotes_gpc())
    //{
    //$_POST['email'] = addslashes($_POST['email']);
    //}
    $emailcheck = $_POST['email'];
    $check = mysql_query("SELECT email FROM $table2 WHERE email = '$emailcheck'") or
        die(mysql_error());
    $check2 = mysql_num_rows($check);

    //if the name exists it gives an error
    if ($check2 != 0)
    {
        die('Sorry, the email address' . $_POST['email'] . ' is already registered.');
    }

     // encrypt the password
    $pass1 = md5($pass1);
    //if (!get_magic_quotes_gpc())
    //{
    //pass1 = addslashes($pass1);
    //$_POST['member_id'] = addslashes($_POST['member_id']);

    //Insert record to database
    $insert = mysql_query("INSERT INTO $table1(confirm_code,name,email,password,tel,address)
		                  VALUES ('$confirm_code','" . $_POST['name'] . "','" . $_POST['email'] .
                          "', '" . $_POST['pass1'] . "','" . $_POST['tel'] . "','" . $_POST['address'] ."')")
						   or die(mysql_error());


    // }

    //if successfully inserted,send confirmation link to email
    if ($insert)
    {
        echo "thank for your registration!";

        require ("class.phpmailer.php");

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = "localhost";
        $mail->SMTPAuth = true;// turn on SMTP authentication
        $mail->Username = "root";// SMTP username
        $mail->Password = "";// SMTP password
        $mail->SetLanguage("en","phpmailer/language");

        //compose mail
        $mail->From = "admin@localhost.com";
        $mail->FromName = "Cinema Admin";
        $mail->AddAddress($email);
        $mail->AddReplyTo("admin@localhost.com", "Cinema Admin");
        $mail->Subject = 'Your confirmation link here';
        $mail->Body = "Your confirmation link\\r\
";
        $mail->Body .= "Click on this link to activate your sccount\\r\
";
        $mail->Body .= "http://localhost/www/ confirm.php?passkey=$confirm_code";


        //send mail
        if (!$mail->Send())
        {
            echo "Message was not sent <p>";
            echo "Mailer Error: " . $mail->ErrorInfo;
            exit;
        }

        echo "Message has been sent";
    }

    //if not found
    else
    {
        echo "Not found your email in our database";
    }

    //if your email successfully sent
    //if ($sentmail)
    //{
    //echo "Your confirmation link has been sent to your email address.";
    //}

    //else
    //{
    // echo "Cannot send confirmation link to your email address.";
    //}

}
?>

sendmail portion in php.ini


[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
sendmail_from = admin@localhost.com

I have put classphpmailer.php,class.smtp.php and phpmailer.lang-en.php under htdocs directory and my machine is listening to port 25.And how to ensure that language file placed in php include path?thanks…

$mail->AddAddress($email);

I don’t see you setting $email anywhere. I think in your code it should be $_POST[‘email’] and also filtered for malicious input.

Also, somewhere at the top do ini_set(“include_path”, “path/to/language/file”); to make sure it’s in the include path.

Problem still there and somemore process become very slow when click Submit.Is my sendmail code correct?


//if successfully inserted,send confirmation link to email
    if ($insert)
    {

		echo "thank for your registration!";

        require ("class.phpmailer.php");

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = "localhost";
        $mail->SMTPAuth = true;// turn on SMTP authentication
        $mail->Username = "root";// SMTP username
        $mail->Password = "";// SMTP password
        $mail->SetLanguage("en","phpmailer/language");

        //compose mail
        $mail->From = "admin@localhost.com";
        $mail->FromName = "Cinema Admin";
        $mail->AddAddress($_POST['email']);
        $mail->AddReplyTo("admin@localhost.com", "Cinema Admin");
        $mail->Subject = 'Your confirmation link here';
        $mail->Body = "Your confirmation link\\r\
";
        $mail->Body .= "Click on this link to activate your sccount\\r\
";
        $mail->Body .= "http://localhost/www/confirm.php?passkey=$confirm_code";


        //send mail
        if (!$mail->Send())
        {
            echo "Message was not sent <p>";
            echo "Mailer Error: " . $mail->ErrorInfo;
            exit;
        }

        echo "Message has been sent";
    }


Should i insert ini_set(“include_path”, “path/to/language/file”); in very beginning of the php clause or within sendmail code?thanks…

The syntax looks correct, so the problem must be the SMTP server you use locally.
Try replacing it with your provider’s SMTP server (the one Outlook uses to send out your mails). Also have a valid From address, since anything not containing that is generally discarded as spam.

Also to check your SMTP is working properly setup another account on your Outlook or other mail client to send mail through that SMTP server to an address you haven’t bombed with this script yet.
If no mail shows up you better start checking your SMTP configuration and in the mean time develop using a different one.

I can do telnet localhost 25,what wrong with my local SMTP server?
I have configured an account localhost under Outlook Express,can I use admin@localhost.com as From Address with POP3 Incoming Server and SMTP Outgoing Server?What should be account name and password?Is it that one from my ISP?

local user account, password is the user acount, password you’ve created in your local mail server.

anything@localhost.com is NOT a valid From address unless localhost.com happens to point to your computer. Use admin@localhost or configure another host localhost.loc in your hosts file.
However, exactly where you find that hosts file on Windows escapes me since I’ve been off it for a number of years now.
PS: Telneting to it is not enough. Make sure you are able to send out a message through it before considering it set up properly.

So now I changed From Adress to admin@localhost and having same username and password in Outlook account and script(sendmail form & class.phpmailer.php).But still can’t send out mail from localhost account in Outlook.

That Local SMTP I used need to configure account under Outlook rather than its own interface,should I looking for another which better?

What is the host file you mean?Is it class.phpmailer.php?