Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

  • Upload
    admird

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    1/10

    Email is simply a way to send mail electronically to people in text and image form, withattachments if required. Email has been called the internets killer app and is generallyconsidered one of the first things people think of when they think of the internet.

    Thats why when you want to update your users on whats happening on your website, we use

    a mailing list or a subscription service where the user enters their email address, validates it,and recieves updates from you. In this tutorial well cover how exactly to do that, andhopefully youll learn a thing or two about PHP and MySQL along the way.

    In this little tutorial well be going over how to make a nice email subscription type service.This can be useful if you want to set up your own DIY mailing list, and hopefully itll giveyou a better grasp ofPHP and MySQL.

    CSS, you say?Lets start by making the script that allows users to subscribe and what not. First thing I didwas to set up a little CSS script, to make everything look prettier. This is all rather straightforward, so Im not going to explain it.

    view sourceprint?

    01 body {

    02 background: #23292e;

    03 margin: 20px;

    04 font-family: Helvetica, Arial, sans-serif;

    05 }

    06

    07 .message {

    08 background: #e8ed90;

    09 border: 1pxsolid#a9ae54;

    10 padding: 15px;

    11 width: 500px;

    12 font-weight: bold;

    13 font-size: 14px;

    14 color: #7c7834;

    15 }

    16

    17 .form {

    18 font-weight: bold;

    19 padding: 15px;20 color: #cbd0d3;

    http://www.php.net/http://www.php.net/http://www.mysql.com/http://www.mysql.com/http://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://www.mysql.com/http://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://www.php.net/
  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    2/10

    21 }

    22

    23 input {

    24 padding: 5px;

    25 font-family: Helvetica, Arial, sans-serif;

    26 }

    Just stick it in a file called style.css and include it in your html document:

    view sourceprint?1

    Now that thats out of the way, lets get down to some hard coding and databasing.

    MySQL

    Were going to required a database to store all these emails and what not in, and MySQL isthe database client to do it! If you are a complete newbie to MySQL click here. First youregoing to have to make a MySQL Database, and create a table called subscribers. This tableshould have 5 columns:

    id int(10) (Primary Key) email varchar(255) validated int(10) ip varchar(45) vcode varchar(255)

    In PHPMyAdmin you can do this pretty easily with a nice user interface. However, if youhave any problems with that or are using another system, then just run this query:

    view sourceprint?1 CREATETABLEIF NOTEXISTS `subscribers` (

    2 `id` int(10) NOTNULLauto_increment,3 `email` varchar(255) NOTNULL,

    4 `validated` int(10) NOTNULL,

    5 `ip` varchar(45) NOTNULL,

    6 `vcode` varchar(255) NOTNULL,

    7 PRIMARYKEY (`id`)

    8 )

    This should create a lovely little table in your MySQL database. Now we have to make a way

    to submit data to this table.

    http://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/php-and-mysql-an-introduction/http://www.phpmyadmin.net/home_page/index.phphttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/php-and-mysql-an-introduction/http://www.phpmyadmin.net/home_page/index.phphttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#about
  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    3/10

    Subscribing

    Make a couple of new file, and call them subscribe.php, resend.php and validate.php.

    First, make a form so the user can submit data to the database:

    view sourceprint?

    1

    2 Enter your Email Here: 3

    4

    This is pretty straight forward html. Were just posting some data. Now that the data has beenposted, we can begin to manipulate it. Above the form add the following:

    view sourceprint?01

    The first two lines just connect to the MySQL database. The if statement checks if the datahas been posted, and if it has then do the whatevers in the curly brackets, which well get tonow. So inside the curly brackets, add the following variables:

    view sourceprint?

    1 $email= $_GET['email'];

    2 $ip= $_SERVER['REMOTE_ADDR'];

    This puts the email the user submitted into a variable and gets the users IP Address. Next addthe following line:

    view source

    print?1if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $email)) {

    http://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#about
  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    4/10

    That complicated string right there checks if the email is indeed an email, usingpreg_match.The code used here is Regexp, or regular expression, but lets not get into that. If that returnstrue, then we need to insert the email into the database. So first of all, lets run a query to checkthat the email isnt already in the database:

    view sourceprint?

    1$query= mysql_query("SELECT * FROM subscribers WHERE email = '".$email."'");

    2

    3 if(mysql_num_rows($query) == 0) {

    4

    5 }

    6 else{

    7 8 }

    Above were running a query and then counting the number of results with mysql_num_rows.If the number is equal to 0, then continue and submit the data to the database. Otherwise theemail is already in the database, so theres no point inserting it again. In theif(mysql_num_ro.. bracket, start by adding the following:

    view sourceprint?1 $key= "1234567890abcdefghijklmnopqrstuvwxyz";

    2

    3 $i= 0;

    4 while($i< 15) {5 $random[$i] = $key[mt_rand(0, strlen($key))];

    6 $i++;

    7 }

    8 $implode= implode("", $random);

    This generates a random key for the user, so that they can verify their email later on. $implodeis the final key. A little low down on the code: $key is a variable consisting of the alphabetand numbers. We make $i equal 0, and make a while loop, which sets a random letter forevery part of an array called random, numbered by $i. We then add 1 to i, and repeat the whileloop 15 times. Implodeturns an array into a string so we can use it more easily. Next I made alittle message which will appear in the email:

    view sourceprint?1 $link= "http://www.webtint.net/email/validate.php?vcode=".$implode."";

    2 $message= "Hello there,

    http://php.net/manual/en/function.preg-match.phphttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://uk.php.net/manual/en/function.mysql-num-rows.phphttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://php.net/manual/en/function.implode.phphttp://php.net/manual/en/function.implode.phphttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://php.net/manual/en/function.preg-match.phphttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://uk.php.net/manual/en/function.mysql-num-rows.phphttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://php.net/manual/en/function.implode.phphttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#about
  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    5/10

    3

    4Thank you forsubscribing to our mailing list. By Subscribing to thisyou'll recieve spiffy updates on the latest happenings at Mail List PHPScript Inc.

    5

    6 To validate your email please click on the link below:

    7 ".$link."";

    Youll have to change the link to where ever validate.php is on your server. The link has toend with the ?vcode= bit so that we can define the user to validate their email. To finish it alloff we do 3 things:

    view sourceprint?

    1 mail($email, "Validate your email", $message);

    2echo"Alright. Well Done. Now Validate yourEmail.";

    3mysql_query("INSERT INTO subscribers(email, ip, validated, vcode)VALUES('$email', '$ip', '0', '$implode')") ordie(mysql_error());

    We use the mail function to mail an email to the user, asking them to validate their email. Wethen echo a message to the user, saying that they ahve sucessfully subscribed, and finally, weinsert the data into the database using a mysql query. Remember, the validated is alwaysgoing to be 0 at this stage, because the email isnt validated. In the else bracket, add thefollowing:

    view sourceprint?

    1echo"You've already Subscribed! Need to validate youremail? Click here";

    This is just to give the user a message telling them that theyve already subscribed, and if theyneed it, a link to resend their validation email. To finish off, close all the brackets, and add afinal else statement, incase the user didnt enter the email correctly.

    view sourceprint?1 }

    2 }

    3 else{

    4echo"Failure. You didn't enter the email addresscorrectly.";

    5 }

    6 }

    I added a little feature at the end, to check for the users IP Address and give them a littlenotice if they havent validated yet:

    view source

    http://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSource
  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    6/10

    print?01 $query= mysql_query("SELECT * FROM subscribers");

    02

    03 while($row= mysql_fetch_array($query)) {

    04 05 if($row[ip] == $_SERVER['REMOTE_ADDR'] && $row[validated] == 0) {

    06

    07echo"
    Seems you haven't validated your emailaddress yet. Click here to resend theemail";

    08

    09 }

    10

    11 }

    Now the user can submit their details to the database, but we need a way for them to validateand resend the validation email if the mail function fails.

    Resend my email!Okay so in the cataclysmic event that the email doesnt send, you dont want your user to getangry and march off. So instead we need to make a page to resend this email. Start once again

    by connecting to your database:

    view source

    print?1 mysql_connect("localhost", "username", "password") ordie(mysql_error());

    2 mysql_select_db("database") ordie(mysql_error());

    Were going to be checking for two things this time in the opening if statement. We need tocheck that the user is posting something to the page, and that its an email.

    view sourceprint?

    1if($_GET['email'] && preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+

    (\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $_GET['email'])) {2

    3 }

    Next we put the email into a variable (as we did in subscribe.php) and we get the validationcode and whether or not the email is validated from the database, running a simple littlequery, all inside this first if statement.

    view sourceprint?1 $email= $_GET['email'];

    2

    http://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#about
  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    7/10

    3$query= mysql_query("SELECT * FROM subscribers WHERE email = '".$email."'LIMIT 1");

    4 while($row= mysql_fetch_array($query)) {

    5 $vcode= $row['vcode'];

    6 $valid= $row['validated'];7 }

    Next well do a couple of checks. First, well check that the email exists in the table, and thatit is not validated. Else if its not, check if it is validated. Otherwise the email isnt in thetable, so well tell the user that.

    view sourceprint?1 if(mysql_num_rows($query) > 0 && $valid== 0) {

    2

    3 }

    4 elseif($valid== 1) {5 echo"Your email has already been validated!";

    6 }

    7 else{

    8echo"I'm afraid that email doesn't exist in ourdatabase. Try re-subscribing";

    9 }

    The last two statements are pretty straight forward. The first if statement is where most of thestuff is going to happen, but its still pretty straight forward. Its just a segment of the

    subscribe.php file.

    view sourceprint?1 $link= "http://www.webtint.net/email/validate.php?vcode=".$vcode."";

    2 $message= "Hello there,

    3

    4Thank you forsubscribing to our mailing list. By Subscribing to thisyou'll recieve spiffy updates on the latest happenings at Mail List PHPScript Inc.

    5 To validate your email please click on the link below:

    6 ".$link."";

    7 mail($email, "Validate your email", $message);

    8 echo"Email has been resent!";

    Pretty straight forward huh? So lets assume the user has their email, and now they want tovalidate it. Well were going to need validate.php for that!

    http://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#about
  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    8/10

    Validate me to pieces

    Validate.php comes down to basically one thing, and that is changing a value from 0 to 1 in adatabase. Again, begin by connecting to your database:

    view sourceprint?

    1mysql_connect("localhost", "username", "password") ordie(mysql_error());mysql_select_db("database") ordie(mysql_error());

    Then we need to check that a validation code has been sent in the url:

    view sourceprint?1 if($_GET['vcode']) { }

    Finally, insert all the guts of the if statement:

    view sourceprint?

    1 $vcode= $_GET['vcode'];

    2$query= mysql_query("SELECT * FROM subscribers WHERE vcode ='".$vcode."'");

    3 if(mysql_num_rows($query) > 0) {

    4mysql_query("UPDATE subscribers SET validated = '1' WHERE vcode = '".

    $vcode."'");

    5echo"Your email has been validated sucessfully!

    ";

    6 }

    7 else{

    8 echo"Incorrect validation code!";

    9 }

    All this does is checks if there is an email with the corresponding validation code, and if thereis, validate that email. Otherwise its an incorrect validation code, and to tell the user that.

    Okay so now the user can easily subscribe and validate their emails, but for you to be able tosend them emails, were going to need some sort of admin type panel.

    Admin Sending Emails

    This is a pretty simple little script. Make a new file, called index.php, and put it in a foldercalled admin. Then make a form as shown below:

    view sourceprint?

    http://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#about
  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    9/10

    1

    2 Title:

    3Message:

    4

    Then above the form, again, connect to your mysql database:

    view sourceprint?1 mysql_connect("localhost", "username", "password") ordie(mysql_error());

    2 mysql_select_db("database") ordie(mysql_error());

    Next, to keep yourself right, you better check youve filled in the title and message fields!

    view sourceprint?01 if($_POST['message'] != ""&& $_POST['message'] != "") {

    02

    03 }

    04 else{

    05 if($_POST['message'] == "") {

    06 echo"Please Enter a Message!
    ";

    07 }

    08 if($_POST['title'] == "") {

    09 echo"Please Enter a Title!
    ";

    10 }

    11 }

    This just gives you a little error message if youve left one blank. In the first if statement, addthe following:

    view source

    print?1 $message= $_POST['message'];

    2 $title= $_POST['title'];

    3 $query= mysql_query("SELECT * FROM subscribers WHERE validated = '1'");

    4 while($row= mysql_fetch_array($query)) {

    5 mail($row['email'], $title, $message);

    6 }

    7

    8 echo"Message sucessfully sent to everybody!";

    http://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#abouthttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#viewSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#printSourcehttp://webtint.net/tutorials/how-to-make-a-email-based-subscription-service/#about
  • 8/8/2019 Email is Simply a Way to Send Mail Electronically to People in Text and Image Form

    10/10

    This mails all users with a validated email, and gives you a little message telling you it did it!You may want to password protect the admin folder. You can do this with a simple littlehtaccess script which you can find here.

    I hope youve enjoyed this tutorial, and I hope in some way it has been useful.

    http://www.javascriptkit.com/howto/htaccess3.shtmlhttp://www.javascriptkit.com/howto/htaccess3.shtmlhttp://www.javascriptkit.com/howto/htaccess3.shtmlhttp://www.javascriptkit.com/howto/htaccess3.shtml