0

Possible Duplicate:
Send email using GMail SMTP server from PHP page

Im following a tutorial on how to register a user through an email authorization. Im having some trouble on SMTP. Im not sure why, but the script will not send the email. I dont know if its an issue with the SMTP or what. The tutorial is from 2009 so it may not be exactly compatible with the newest build of php. Does anyone see anything that is causing the issue?

 <?php
 echo "<h1>Register</h1>";

 $submit = filter_input(INPUT_POST, 'submit');
 //form data
 $fullname = strip_tags (filter_input(INPUT_POST, 'fullname'));
 $username = strtolower(strip_tags (filter_input(INPUT_POST, 'username')));
 $password = strip_tags(filter_input(INPUT_POST, 'password'));
  $repeatpassword = strip_tags(filter_input(INPUT_POST, 'repeatpassword'));
 $date = date("Y-m-d");
 $email = strtolower(strip_tags (filter_input(INPUT_POST, 'email')));

 if ($submit)
 {
 //open database
 $connect=mysql_connect("localhost","root","myrealpasswordwouldgohere");
 mysql_select_db("phplogin");

 $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'" );
 $count = mysql_num_rows($namecheck);

 if($count!=0)
 {
 die("Username already taken, please choose another");
 }

 //check for existence
if($fullname&&$username&&$password&&$repeatpassword)
{
    if ($password==$repeatpassword)
    {
    //check char length of username and fullname
        if (strlen($username)>25||strlen($fullname)>25)
        {
        echo "Length of username or full name is too long!";
        }
        else
        {
        //check password length 
            if (strlen ($password)>25 || strlen ($password)<6)
            {
            echo "Password must be between 6 and 25 characters";
            }
            else
            {
             $password = md5($password);
             //register user    

            //generate random number
            $random = rand(23456789,98765432);


            $queryreg = mysql_query("INSERT INTO users VALUES ('','$fullname','$username','$password','$email','$date','$random','0')");
            die ("You have been registered! Check your email to activate your account to activate your account.");

            $lastid = mysql_insert_id();

            //send activation email
            $to = $email;
            $subject = "Activate your Account";
            $headers = "From: ryansinclair14@gmail.com";
            $server = "smtp.gmail.com";

            ini_set("SMTP","smtp.gmail.com");

            $body = "
            Hello $fullname,\n\n
            You need to activate you account with the link below:
            http://localhost/academy/loginsession/activate.php?id=$lastid&code=$random \n\n

            Thanks!
            ";

            //function to send email
            mail($to, $subject, $body, $headers);
            die("You have been registered! Check your email to activate your account.");


            }



        }





    }
    else echo "Your passwords do not match";


}
else echo "Please fill in <b>all</b> fields!";


 }


 ?>
 <p>
 <html>

      <form action='register.php' method='POST'>
<table>
    <tr>
        <td>
        Your full name:
        </td>
        <td>
        <input type='text' name='fullname' value='<?php echo $fullname;?>'>
        </td>

    </tr>

    <tr>
        <td>
        choose a username:
        </td>
        <td>
        <input type='text' name='username' value='<?php echo $username;?>'>
        </td>

    </tr>


    <tr>
        <td>
        Choose a password:
        </td> 
        <td>
        <input type='password' name='password'>
        </td>

    </tr>

    <tr>
        <td>
        Repeat your password:
        </td> 
        <td>
        <input type='password' name='repeatpassword'>
        </td>
    </tr>
    <tr>
        <td>
        Email:
        </td> 
        <td>
        <input type='text' name='email'>
        </td>


    </tr>

     <table>
     <p>
     <input type='submit' name='submit' value='Register'>

 </form>
Community
  • 1
  • 1
Ryan Sinclair
  • 195
  • 1
  • 4
  • 11
  • http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page – Prix Apr 16 '12 at 04:33

4 Answers4

1

You can use phpmailer for smtp mails

http://code.google.com/a/apache-extras.org/p/phpmailer/wiki/UsefulTutorial

Pradeep Sanjaya
  • 1,816
  • 1
  • 15
  • 23
1
<?php

    require_once "Mail.php";

    $from = "ryansinclair14@gmail.com";
    $to = "ryansinclair14@gmail.com";
    $subject = "Test";
    $body = "Hello World";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "ryansinclair14";
    $password = "PASSWORD";

    $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>");
     }

?>

Try this. Might need to include the full path for Mail.php wherever you installed it.

Jack
  • 5,680
  • 10
  • 49
  • 74
  • whats the full path mean exactly? its installed in the normal location – Ryan Sinclair Apr 16 '12 at 05:05
  • http://pear.php.net/manual/en/installation.php – Jack Apr 16 '12 at 05:08
  • i think i have it installed already – Ryan Sinclair Apr 16 '12 at 05:09
  • You think you have it installed? Well do you? Is there a `pear` folder inside your php folder? – Jack Apr 16 '12 at 05:54
  • yeah i got the folder and included mail.php...still not working – Ryan Sinclair Apr 16 '12 at 13:35
  • You sure you have all these components installed: http://www.markstechstuff.com/2009/04/installing-pear-mail-for-php-on-ubuntu.html and included the path to your pear folder in your php.ini like `include_path = ".;C:\Wamp\bin\php\php5.3.8\pear"` – Jack Apr 16 '12 at 19:43
  • okay so i put the following code in the phpini ============= ; PHP's default setting for include_path is ".;/path/to/php/pear" ; http://php.net/include-path include_path = ".;C:\xampp\php\PEAR\Mail" ======================= – Ryan Sinclair Apr 16 '12 at 20:22
  • and im using the pear mail format...all my syntax is correct (i think) still not working... – Ryan Sinclair Apr 16 '12 at 20:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10137/discussion-between-ryan-sinclair-and-jack) – Ryan Sinclair Apr 16 '12 at 20:33
0

You dint configure smtp properly.you dint provide password and user name.

You can go through the following link.

http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm

Pushparaj
  • 538
  • 1
  • 4
  • 16
0

I'm sure that if you are using gmail servers you will need to authenticate properly.

Google popped up something that looks like it might be useful How to Send Email from a PHP Script Using SMTP Authentication

Andy
  • 2,095
  • 1
  • 29
  • 59