Page 1 of 1

[SOLVED] Help making a Newsletter email system

Posted: Fri Oct 15, 2010 6:46 am
by zacthespack
For my A level ICT i need to create a PHP newsletter email system for which people can sign up from one page and then a admin can log in and send out the email to the people that have signed up. My problem is i also need to attach a PDF to these emails, however i cant work out how to add this into the scripts i have for the newsletter system. Even my teacher cant help me :banghead:, please can someone help me :( .
The code i have is below.
admin.php

Code: Select all

<?php
  //load configuration
  require("config.php");
  
  //connect to database
  @mysql_connect($db_server,$db_user,$db_password) or die("Database server connection failed. Check variables \$db_server, \$db_user and \$db_password in config.php");
  @mysql_select_db($db_name) or die("Selecting database failed. Check variable \$db_name in config.php");
  
  //print header
  echo $header;
?>
<h1><?php echo $title; ?> - Administration</h1>
<hr>
<?php 
  $pwd = $_GET["pwd"];

  //simple login
  if(isset($pwd) && ($pwd == $password)){
    $submit = $_POST["submit"];
    $submit_newsletter = $_POST["submit_newsletter"];
    $del = $_GET["del"];
	  
    //insert new address
    if(isset($submit)){
      $name = $_POST["name"];
      $email = $_POST["email"];
      
      @mysql_query("INSERT INTO $db_table (email,name) VALUES ('$email','$name');");
      
      //error occurred
      if(@mysql_error()){
        ?><p>Inserting entry failed: <?php echo @mysql_error(); ?></p>
	<p><a href="javascript:history.back();">Click here to go back.</a></p><?php
      //successful
      }else{
        ?><p>Entry has been inserted successfully. <a href="admin.php?pwd=<?php echo $pwd; ?>">Click here to continue.</a></p><?php
      }
    //delete an entry
    }else if(isset($del)){
      @mysql_query("DELETE FROM $db_table WHERE id=$del;");
      
      //error occurred
      if(@mysql_error()){
        ?><p>Deleting entry failed: <?php echo @mysql_error(); ?></p>
	<p><a href="javascript:history.back();">Click here to go back.</a></p><?php
      //successful
      }else{
        ?><p>Entry has been deleted successfully. <a href="admin.php?pwd=<?php echo $pwd; ?>">Click here to continue.</a></p><?php
      }
    //send newsletter
    }else if(isset($submit_newsletter)){
      $sent = 0;
      $result = @mysql_query("SELECT name,email FROM $db_table ORDER BY email ASC;");
      $subject = $_POST["subject"];
      $message = $_POST["message"];
      
      ?><p>Sending emails to ...</p>
      <ul><?php
      
      //send emails one by one
      while($row=@mysql_fetch_array($result)){
        $name = $row["name"];
        $email = $row["email"];
	
        ?><li><?php echo $name; ?> (<?php echo $email; ?>) ... <?php

        if(@mail($email,$subject,$message,"From: $admin <$admin>\n")){
          ?>sent<?php
          $sent++;
        }else{
          ?>failed<?php
	}
	
	?></li><?php
      }
      
      ?></ul>
      <p><strong><?php echo $sent; ?> emails sent.</strong> <a href="admin.php?pwd=<?php echo $pwd; ?>">Click here to continue.</a></p><?php
    //print forms
    }else{
       ?><h2>Send newsletter</h2>
       <form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>?pwd=<?php echo $pwd; ?>">
         <table>
           <tr>
             <td>Subject:</td>
             <td><input type="text" name="subject" class="fixedwidth"></td>
           </tr>
           <tr>
             <td valign="top">Message:</td>
             <td><textarea name="message" cols="60" rows="20" class="fixedwidth"><?php echo $signature; ?></textarea></td>
           </tr>
           <tr>
             <td>&nbsp;</td>
             <td><input type="submit" name="submit_newsletter" value="Send"></td>
           </tr>
         </table>
       </form>

       <h2>Add an email address</h2>
       <form action="<?php echo $_SERVER["PHP_SELF"]; ?>?pwd=<?php echo $pwd; ?>" method="post">
         <table>
           <tr>
             <td>Name:</td>
             <td><input type="text" name="name" value="" maxlength="255"></td>
           </tr>
           <tr>
             <td>Email address:</td>
             <td><input type="text" name="email" value="" maxlength="255"></td>
           </tr>
           <tr>
             <td>&nbsp;</td>
             <td><input type="submit" name="submit" value="Submit"></td>
           </tr>
         </table>
       </form>

       <h2>Email addresses</h2>
       <table cellpadding="5" cellspacing="2">
         <tr class="header">
           <td><strong>Name</strong></td>
           <td><strong>Email address</strong></td>
           <td>&nbsp;</td>
         </tr>
         <?php
           $result = @mysql_query("SELECT * FROM $db_table ORDER BY email ASC;");
	   $colored = false;

           while($row=@mysql_fetch_array($result)){
             $colored = !$colored;
             ?><tr<?php if($colored){ ?> class="colored"<?php } ?>>
               <td width="200"><?php echo $row["name"]; ?></td>
               <td width="200"><?php echo $row["email"]; ?></td>
               <td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?pwd=<?php echo $pwd; ?>&del=<?php echo $row["id"]; ?>">remove</a></td>
             </tr><?php
	   }
         ?>
       </table>
       <?php
    }
  }else{
    //print login form
    echo $login;
  }
  
  //print link to news
  ?><p align="right"><a href="index.php">Newsletter</a></p><?php
  
  //print footer
  echo $footer;

  //close database connection
  @mysql_close();
?>
config.php

Code: Select all

<?php
/* ######################## DATABASE ######################## */

  // Database server
  $db_server = "localhost";
  
  // Database name
  $db_name = "zpwebsi1_news";
  
  // Database username
  $db_user = "zpwebsi1_zac";

  // Database password
  $db_password = "powell";
  
  // Database table to store news
  // (will be created automatically)
  $db_table = "easy_newsletter";

/* ##################### CONFIGURATION ###################### */

  // Complete URL of the script
  // (begins with http://, ends with slash)
  $url = "http://www.charsfieldthreehorseshoes.co.uk/alemail.html";
  
  // Password for the administration
  $password = "admin";
  
  // Administrator email address (will be used as sender of
  // the newsletter emails)
  $admin = "zac@zpwebsite.com";

  // Title (will be displayed in the browser's header
  $title  = "Ale Mail";

  // Signature (will be inserted when creating a fresh
  // newsletter but can be changed before submitting)
  $signature  = "\n\n\n\n\n---------------------\nYou receive this email because you have registered with our newsletter $title. Click the following link to unsubscribe: $url";

/* ######################### LAYOUT ######################### */

  // Header to be used on each page
  $header = <<<EOT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="style.css" rel="stylesheet" type="text/css">
    <title>$title</title>
  </head>
  <body>
EOT;

  // Footer to be used on each page
  $footer = <<<EOT
  </body>
</html>
EOT;

  // Login form
  $login = <<<EOT
    <form action="admin.php" method="get">
      Password: <input name="pwd" type="password"> <input type="submit" value="Login">
    </form>
EOT;
?>

And index.php

Code: Select all

<?php
  //load configuration
  require("config.php");
  
  //print header
  echo $header;
?>
<h1><?php echo $title; ?></h1>
<hr>
<?php  
  //form submitted
  if(isset($_POST["submit"])){
    //connect to database
    @mysql_connect($db_server,$db_user,$db_password) or die("Database server connection failed. Check variables \$db_server, \$db_user and \$db_password in config.php");
    @mysql_select_db($db_name) or die("Selecting database failed. Check variable \$db_name in config.php");

    $name = $_POST["name"];
    $email = $_POST["email"];
    
    //subscribe
    if($_POST["action"]=="subscribe"){
      //check if name is long enough
      if(strlen($name)<3){
        ?><p>Name must consist of at least 3 characters.</p><?php
      //check if email is valid
      }else if(!eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,3}$",$email)){
        ?><p>Email address is not valid.</p><?php
      //check if email already exists
      }else if(@mysql_num_rows(@mysql_query("SELECT id FROM $db_table WHERE email='$email';"))){
        ?><p>Email address exists already.</p><?php
      //insert values
      }else{
        @mysql_query("INSERT INTO $db_table (email,name) VALUES ('$email','$name');");

        //error occurred
        if(@mysql_error()){
          ?><p>An unknown error occurred. Please try again later.</p><?php
        //successful
        }else{
          ?><p>Subscription was successful. Thank you!</p><?php
        }
      }
    //unsubscribe
    }else{
      @mysql_query("DELETE FROM $db_table WHERE email='$email';");

      //error occurred
      if(@mysql_error()){
        ?><p>An unknown error occurred. Please try again later.</p><?php
      //email not found
      }else if(@mysql_affected_rows()==0){
        ?><p>Email address not found.</p><?php
      //successful
      }else{
        ?><p>You have unsubscribed successfully!</p><?php
      }
    }
  }
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
  <table>
    <tr>
      <td>Your name:</td>
      <td><input type="text" name="name" value="" maxlength="255"></td>
    </tr>
    <tr>
      <td>Your email address:</td>
      <td><input type="text" name="email" value="" maxlength="255"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="radio" name="action" value="subscribe" id="action_subscribe" checked> <label for="action_subscribe">subscribe</label>&nbsp;<input type="radio" name="action" value="unsubscribe" id="action_unsubscribe"> <label for="action_unsubscribe">unsubscribe</label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" value="Submit"></td>
    </tr>
  </table>
</form>
<p align="right"><a href="admin.php">Administration</a></p>
<?php
  //print footer
  echo $footer;

  //close database connection
  @mysql_close();
?>

Re: Help making a Newsletter email system

Posted: Fri Oct 15, 2010 7:00 pm
by mecha_godzilla
For one thing, you haven't got any MIME type or content-type headers in your code, so you'll only be able to send plain text emails.

Take a look at the following site to see if it's any help, then report back if you get stuck. As you're probably aware, email wasn't really designed for sending attachments so you have to add lots of header information in there to get it to do anything useful:

http://www.webcheatsheet.com/PHP/send_e ... chment.php

This site also shows how to create HTML newsletters (which are much simpler, just a couple of extra lines of headers). Remember that you should also include plain text versions of any HTML newsletters that you create to support older mail clients.

HTH,

Mecha Godzilla

Re: Help making a Newsletter email system

Posted: Sun Oct 17, 2010 3:36 am
by Bind
So you want us to do your homework or test for you.

If your teacher can't help you why should we?

that kind of circumvents the rules as to why your teacher cant help you, doesnt it ?

I would go back to my reference materials provided and do some further research if I were you.

online forums should have rules against helping students cheat.

Re: Help making a Newsletter email system

Posted: Sun Oct 17, 2010 7:35 am
by jason
We do not support people trying to get others to solve school work. I have no issue with you getting help in trying to resolve any problem, but we aren't going to do the work for you. If you simply want help in trying to understand and fix the problem, that's fine. We don't have a problem here. Continue to get help. =)

Why do we not support this? It's simple, really. If we did everyone's homework for them, we'd eventually run into one of these people at our jobs, and suddenly we'd have to deal with someone who has "years of experience" of writing code worth for TDWTF. =)

Re: Help making a Newsletter email system

Posted: Sun Oct 17, 2010 2:39 pm
by mecha_godzilla
I'm not sure who's at fault here so apologies if I've added to this problem - I do agree with jason and Bind that you can't expect people to help you complete your assignments, otherwise what good is that to you really if you want to learn to be a good web developer?

My offering up a few pointers to try and get you started would (I hope) be viewed in the context that it's better to offer some kind of support rather than none but, yes, a lot of the people on these forums are professionals who give up their time for nothing to try and help other people, so it's not fair to take advantage of that where coursework/studies are concerned. After all, who gets the accreditation or diploma at the end of it?

M_G

Re: Help making a Newsletter email system

Posted: Mon Oct 18, 2010 4:04 am
by Bind
In all my years of schooling, I have never been given as assignment, test question, major project, or problem to solve where I have not been supplied with the educational reference materials to derive the solution from by the institution.

ANY assistance to students should be forbidden. If the teacher says they cant give any assistance, as the original poster has already stated, then any assistance is cheating. Meaning the student should already have this knowledge by the educational reference materials and educating done by the teacher.

If the student did not listen in class or read and research reference materials as instructed, and is now having problems, it is THEIR concern and they SHOULD fail the task given to them, so they can obtain grade commensurate with their skill levels. This may require a class or course repeat so they can learn - possibly proving a valuable life lesson in the process about personal responsibility, note-taking, study skills, paying attention in class and lecture, and how important a good education actually is and not wasting it away.

Re: Help making a Newsletter email system

Posted: Wed Jul 20, 2011 7:57 am
by zacthespack
Hello, only just noticed this again been a while, Just to let you all know the reason the teacher couldn't help me was because he didn't understand the system I wanted to build. He taught the rest of the class using Microsoft access which I did not want to do, it was not a case of he shouldn't of been helping me but he really couldn't this is why I posted it here as he told me to try and find help on-line, the was no reason why I couldn't use help from anywhere as long as I said what I had help with and why etc.
Either way I finished the project some time ago and got a A after finding some help via Google etc. and teaching myself stuff, thanks anyway guys