简体   繁体   中英

Trying to send username to email using PHP

I have a database which contains user emails and automatically generated usernames, and I have a form in which the user enters their email to retrieve their username, here is my code

<html>
<body>
  <form method="post" action="retrievedetails.php">
    <p><label for="email">Email:</label>
    <input name="email" type="text"/>
    </p>
    <input type="submit" name="submit" value="submit"/>
  </form>

<?php
session_start();
$connect=mysql_connect("localhost","dfarrelly","1234") or die("Could not connect to database");
mysql_select_db("game", $connect) or die("Couldn't find db");

  if(isset($_POST['submit'])) {
      $email=$_POST['email'];
      $email_check=mysql_query("SELECT user_name FROM details WHERE user_email='$email'");
      $name=mysql_num_rows($email_check);
      $subject="Login Info";
      $message="Your username is .$name";
      $from="From: test@gmail.com";
      mail($email, $subject, $message, $from);
      echo "your username has been emailed to you";
  }
?>
</body>
</html>

I am not getting any errors, I am just not receiving the email, can anyone help me with this?

A assume you didn't specify a SMTP-Server yet, which is the reason you can't send emails. Companies like Google have these SMTP-Servers, which allow you as a e-mail-service customer to receive and send emails via their SMTP-Server. Without the access to a SMTP-Server you won't be able to send emails.

The solution is to specify the SMTP details in your php.ini, which you can find via the windows search or spotlight (if you work on a mac).

I recommend you using PHPMailer, which makes it a lot easier using the SMTP details from fe your personal Google mail.

Just check the accepted answer for a example: Sending email with PHP from an SMTP server

You should NEVER use the value of a form blindly in your SQL statement.

$email=$_POST['email'];
$email_check=mysql_query("SELECT user_name FROM details WHERE user_email='$email'");

You are asking for someone to inject some really nasty SQL as an email address right there.

http://www.w3schools.com/sql/sql_injection.asp

Apart from that, I think you should check that your server can send emails. mail() will just forward your message onto whatever your server uses to send emails. Most likely that is not set up so you cannot send emails until that is fixed.

Going by what the PHP documentation says for the mail function and @BeAnonymous, you need to define an SMTP server.

I use PEAR mail for sending email via PHP.

Also, what @Carl Casbolt said, you're interacting directly with the database with your form, which is asking for an injection attack. Look into using PDO for database interactions:

IF you are working on local server "localhost", you should configure your smtp server first, else this doens will work, if you are working on web server check your spam box, mail function make that. To solve the spam problem use phpMailer instead of mail function of php.

you should configure your php.ini file: you should search for [mail function] and add information about your mail server. For me, on unix, it works and here is the configuration :

[mail function]

; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path sendmail_path = /usr/sbin/sendmail -t -i

; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename mail.add_x_header = On

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM