php - Sending an email verification after registering


I want, that users who register on my site, have to activate their account first, before using it. The problem is, that I don't get any email to my email test account.

Before I start posting a code, could the problem be, that I'm working currently on a local machine with xampp?

Otherwise, here is the code snippet

$random = substr(number_format(time() * rand(),0,'',''),0,10);
    $insertMailVerify = $this->db->prepare("INSERT INTO mailverify (mailAddress, token, datetime) VALUES (:mailAddress, :token, :date)");

    $to = $emailAddress;
    $subject = "Activating your Account";
    $body = "Hi, in order to activate your account please visit http://localhost/FinalYear/activation.php?email=".$emailAddress." and fill in the verification code $random";
    if(mail($to, $subject, $body))
        echo ("<p>Message success</p>");
    else {
        echo ("<p>Message fail</p>");

Just in case you wonder where i take $emailAddress from: This is just a code snippet, i already let the software echo the email address, and it's correct. It even goes in the "Message success" if case, but I still can't get any email. What could be the problem?




After submit the form you can use a code or link and send it to the user email id

$message = "Your Activation Code is ".$code."";
$subject="Activation Code For";
$from = 'your email';
$body='Your Activation Code is '.$code.' Please Click On This link <a href="verification.php">Verify.php?id='.$db_id.'&code='.$code.'</a>to activate  your account.';
$headers = "From:".$from;

echo "An Activation Code Is Sent To You Check You Emails";

Code from for complete tutorial visit




in local host i think the best way is usingphpmailer andgmail account

here the tutorial :




It seems your mail server SMTP is not configured correctly....

check the port and IP of SMTP server address.




Try to change youPHP.ini file like this and tell me if it works.

[mail function]
; For Win32 only.

smtp_port = 465 //if 465 is not working then try 25

If this is not working then there are a lot of tutorials of how to achieve what you are trying to do. Here is one link: Send email from localhost




Had the same problem, but on linux.

1) Assuming your mail() function is working properly: the problem could be, that email is coming into spam-box (because these localhost mail systems are often marked as spambots, so email services are protecting emails from unverified host).

2) If mail() is not working, its not configured properly, if you follow some tutorial and configure it, which needs like 5 minutes, you will realise why not to use it:)

The best way for you is to use some free or paid smtp service. Very easy, quick and your emails wont be marked as spam. And install PEAR library to send email. Here is an example.

$from    = '[email protected]';
$to      = '[email protected]';
$subject = 'subject';
$body    = 'Hello!';

$host = '';
$port = '25';
$username = 'yourlogin';
$password = 'yourpass';

$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if(PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");

To your verification code: instead of telling user to write down the verification code, better generate him the link, that he clicks his account gets activated. Something like

http://localhost/FinalYear/[email protected]&token=79054025255fb1a26e4bc422aef54eb4

on registration page: generate the activation token likemd5($email . '_sometext');

on activation pageif(md5($_GET['email'] . '_sometext') == $_GET['token']) { activate.. }

please note, that it is just an example, there are 10 ways how it could be done :)

People are also looking for solutions to the problem: php - Zend Framework 2: Disable module on production environment


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.