-2

I'm using a code I found in developphp.com for a newsletter system. But it doesn't show how to send an email to the subscriber for confirmation and an option to opt out. So, How could I send an email to the new subscriber?

<?php
$name = "";
$email = "";
$msg_to_user = "";
    if ($_POST['name'] != "") {
    include_once "scripts/connect.php";
$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 /><h4><font color="FF0000">' . $name . ', Please type in your email.</font></h4>';
    } else if ($numRows > 0) {
$msg_to_user = '<br /><br /><h4><font color="FF0000">' . $email . ' is already in the system.</font></h4>';
    } else {
$sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime)
    VALUES('$name','$email',now() )")  or die (mysql_error());
$msg_to_user = '
    <br /><br /><h4><font color="33cc44">' . $name . ', You have been subscribed.</font></h4>
    ';
$name = "";
$email = "";
}
}
?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Victor Vizcaino
  • 11
  • 1
  • 2
  • 10
  • You should stop querying this way `mysql_query` is almost deprecated, also you need to validate `$_POST['name']` and $_POST['email'] before you include them in the `SQL` statement, the current code is so open for SQL Injection.. it is highly recommended to use Prepared Statements with `PDO` or `MySQLi` – Mi-Creativity Nov 17 '15 at 00:27
  • PDO: http://php.net/manual/en/pdo.prepared-statements.php , MySQLi: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php http://www.w3schools.com/php/php_mysql_prepared_statements.asp – Mi-Creativity Nov 17 '15 at 00:31
  • Not a duplicate, you twats! If you don't know the answer, don't downvote me. I'm trying to learn here. – Victor Vizcaino Nov 17 '15 at 01:28

1 Answers1

0

Do you have 1) a MySQL database with a table named "Newsletter" in it, 2) another PHP script named "connect.php" in a sub-folder named "scripts" that connects to that database, and 3) another page (HTML or PHP) with an input form with the fields "name" and "email" in it that calls this PHP script shown above? (If not, that code won't work for you.) :)

If you get those pieces together, you're on your way. To actually send out an email from the server, check out the PHP "mail" function - it's an easy 1-line command. But you'll have to make sure that you have mailing functionality configured on that server or nothing will go out.

For a separate option for the user to unsubscribe, the easiest way might be to create a separate page for that. You might include the link to that page in the email you send to the user.

If you don't have the time to code all this, you might register for an existing mailing list subscription service that handles this kind of stuff automatically and just provide the email addresses for the user to subscribe and unsubscribe.

John Doe
  • 905
  • 8
  • 9
  • Thank you, my friend! Yes, I do have all of those things. I didn't include them because it's too much, so I just posted the important part. I tried the mail($to, $subject, $message, $headers); thing, but it didn't work. I might just be putting it in the wrong place. – Victor Vizcaino Nov 17 '15 at 01:25
  • Have you checked the `junk` or `spam` folder in your email? – Mi-Creativity Nov 17 '15 at 01:30
  • You should be close then. If your server is set up to send outgoing mail (some servers aren't as a safeguard against spamming), the mail() command should work. You might put it after the 7th line in your script- just after where you set the $name and $email variables. If you place it farther down, it might be inside the section of code where the $email value must be blank - and it might not send. – John Doe Nov 17 '15 at 01:33