Jump to content
zacthespack

Help making a Newsletter email system

Recommended Posts

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  :facewall:, please can someone help me  :'( :wtf:.

The code i have is below.

admin.php

<?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&#058;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&#058;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> </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> </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> </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

<?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

<?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> </td>
      <td><input type="radio" name="action" value="subscribe" id="action_subscribe" checked> <label for="action_subscribe">subscribe</label> <input type="radio" name="action" value="unsubscribe" id="action_unsubscribe"> <label for="action_unsubscribe">unsubscribe</label></td>
    </tr>
    <tr>
      <td> </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();
?>

Share this post


Link to post
Share on other sites

Generally for attaching files, it is recommended to go with PEAR::Mail  as it handles MIME types much better or something like phpMailer. If you want to do this on your own, I would suggest looking at the source code of phpMailer and see how they handle the attachments and follow their lead, or just use their class to begin with. Also of note, you can look at the manual for mail as it gives information on how to attach files as well. Just requires a bit if reading and trial / error in a small scale on your part to get it right.

Share this post


Link to post
Share on other sites

Generally for attaching files, it is recommended to go with PEAR::Mail  as it handles MIME types much better or something like phpMailer. If you want to do this on your own, I would suggest looking at the source code of phpMailer and see how they handle the attachments and follow their lead, or just use their class to begin with. Also of note, you can look at the manual for mail as it gives information on how to attach files as well. Just requires a bit if reading and trial / error in a small scale on your part to get it right.

 

Thanks man i will have a look at those and see if i can make something :)

Share this post


Link to post
Share on other sites

A few hours ago, I searched for a way to make an Email subscription form, and followed instructions. Here's what I did:index.phpアニメ『スクニマイフレンズ』 - とうろくスクニマイフレンズ

ここにとうろく

 

Name:

Email:

 

©2014-2015 PNJ Studios.

Share this post


Link to post
Share on other sites

A few hours ago, I searched for a way to make an email subscription form, and followed instructions. Here's what I did:

 

index.php


アニメ『スクニマイフレンズ』 - とうろく



スクニマイフレンズ
ここにとうろく



Name: 
Email: 



©2014-2015 PNJ Studios.

subscribe.php
When I created both files and tried the form out, it resulted in a page saying "Parse error: syntax error: unexpected T_VARIABLE in home/u312568728/subscribe/subscribe.php on line 22". What did I do wrong?

			
		

Share this post


Link to post
Share on other sites

A few hours ago, I searched for a way to make an email subscription form, and followed instructions. Here's what I did:

 

index.php

<html>
<head>
<title>アニメ『スクニマイフレンズ』 - とうろく</title>
<meta charset="utf-8">
</head>
<body bgcolor=#000000 text=#FFFFFF align="center">
<h1>スクニマイフレンズ</h1>
<p>ここにとうろく</p>
<br />
<form method="POST" action="subscribe.php">
<p>Name: <input type="text" name="Name" sizd="20"></p>
<p>Email: <input type="text" name="Email" size="20"></p>
<p><input type="submit" value="Submit" name="Submit"></p>
</form>
<br />
©2014-2015 PNJ Studios.
</body>
</html>
subscribe.php
<?php

## CONFIG ##

# LIST EMAIL ADDRESS
$recipient="contact@scunimifriends.tk";

# SUBJECT
$subject="Subscription Request";

# RESULT PAGE
$location="http://www.scunimifriends.tk/";

## FORM VALUES ##

# SENDER - WE ALSO USE RECIPIENT AS SENDER
# DON'T INCLUDE UNFILTERED USER INPUT IN MAIL HEADER!
# SEE ALSO: How to protect a php Email Form using php mail or mb_send_mail against Mail Header Injection

# MAIL BODY
body ."Name: ".$_REQUEST['Name']." \n";
body ."Email: ".$_REQUEST['Email']." \n";
# add more fields here if required

## SEND MESSAGE ##
mail($recipient, $sender, $body, "From: $sender") or die ("Failed to subscribe.")

## SHOW RESULT PAGE ##
header("Location: $location");
?>
When I created both files and tried it out, the result was "Parse error: syntax error: unexpected T_VARIABLE in home/u312568728/subscribe/subscribe.php on line 22". What did I do wrong?

Share this post


Link to post
Share on other sites

Define body variable and then add to it this way.

$body = '';
$body .= "Name: ".$_REQUEST['Name']." \n";
$body .= "Email: ".$_REQUEST['Email']." \n";

Or use heredoc or nowdoc

$body = <<<EOF
Name: {$_REQUEST['Name']} \n
Email: {$_REQUEST['Email']} \n
EOF;
  • Like 1

Share this post


Link to post
Share on other sites

I thought the guy was posting spam, so didn't bother to reply.

heredoc is my favorite when you need to write line after line of text.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.