0

Hi I've had this in place for sometime and have been doing things the hard way. It simply goes to the database and I check it frequently, but as you can assume this is a terribly tedious way of doing things. Here is my current code.

<?php
$name = "";
$email = "";
$msg_to_user = "";
if ($_POST['name'] != "") {

    include_once "newsletter/connect_to_mysql.php";

    // Be sure to filter this data to deter SQL injection, filter before querying database
    $name = $_POST['name'];
    $email = $_POST['email'];

    $sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
    $numRows = mysql_num_rows($sql);

    if (!$email) {

        $msg_to_user = '<br /><br /><span style="font color="FF0000">Please type an email address ' . $name . '.</span>';

    } else if ($numRows > 0) {

        $msg_to_user = '<br /><br /><font color="FF0000">' . $email . ' is already in the system.</font>';

    } else {

        $sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime) 
                                                    VALUES('$name','$email',now() )")  or die (mysql_error());

        $msg_to_user = '<br /><br /><div style="color=#F47926;width:180px;">Thanks ' . $name . ',  expect an email shortly!</div>';
        $name = "";
        $email = "";
    }
}
?>

I'm trying to add a simple way to do send and email with the email and name to my own email address. This way I'd not have to do any frequent checks of the database itself, but rather view them as they appear in my email. I hope this makes sense.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Is this PHP? You should tell us instead of making us guess. – John Saunders Apr 25 '13 at 01:53
  • so sorry about that John I tagged it as PHP. The first line also says php. I thought that would be enough...yes it is php with a mysql database connection. – user2251674 Apr 25 '13 at 01:58
  • It was not tagged PHP. – John Saunders Apr 25 '13 at 01:59
  • I'm positive it is John, but if that's enough to not help out I'm sorry. Maybe someone else could point me in the right direction. – user2251674 Apr 25 '13 at 02:00
  • I tagged it PHP. Please always tag PHP questions as PHP. Tags on SO aren't just used to tell a reader what language it is. They're also used to categorize questions in various ways. One way is that it's possible to set "PHP" as a favorite tag, so that PHP questions show up highlighted. Another way is to set "PHP" as an ignored tag, so that PHP questions are never seen. – John Saunders Apr 25 '13 at 02:05

2 Answers2

0

It would be wise to do some additional checks to validate the data being passed through $_POST.

Something like this should get you started.

$email = filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL);

Here is how you would send an email message.

$message = 'Name: ' . $_POST['name'] . ', Email: ' . $_POST['email']; 
mail('your@email.com', 'A New User Has Signed Up',  $message);

http://php.net/manual/en/function.mail.php

http://php.net/manual/en/function.filter-var.php

maxiscool
  • 537
  • 3
  • 9
  • Hi Max, thats the exact code I had before. I beleive I may be having the issue with where to place it. I get a white screen with no content when I try to add it in. I put it right before the last curly bracket. – user2251674 Apr 25 '13 at 02:23
  • Scratch taht. I believe I was missing a semicolon...I just got it working. Thanks a ton for the help. I'm going to look into the first issue of filtering the variable. Thanks. – user2251674 Apr 25 '13 at 02:24
-1

i dont really know what needs to be done, but if you are askng for improvements or suggestions...

instead of this:

$msg_to_user = '<br /><br /><span style="font color="FF0000">Please type an email address ' . $name . '.</span>';

you can do this:

$msg_to_user = "<br /><br /><span style='color:FF0000'>Please type an email address {$name}.</span>";

note: use double quotes.

Viscocent
  • 2,024
  • 2
  • 19
  • 26
  • 1
    Using single quotes is better than double quotes – Michael Sivolobov Apr 25 '13 at 02:23
  • Double quotes always make PHP to use interpolation with whole string even if it has no variables in it. Concatenation is faster than interpolation. Take an experiment. Or see answer here: http://stackoverflow.com/questions/13620/speed-difference-in-using-inline-strings-vs-concatenation-in-php5 – Michael Sivolobov Apr 25 '13 at 02:29