X

Procedure for Sending Newsletters in PHP

For someone like me has a few thousand e-mail addresses from having to manage, acquire a complex system of newsletters would be very expensive compared to economic return, so I created a web page from which only I can send the email to a number fixed addresses.
The obstacle that we must overcome to send our newsletter with a simple shared web hosting is the number of time that must be respected. It is a way that the provider of these services for not be inserted in the black list and be defined as spam. For example in my case I have the opportunity to send 300 emails per hour, so I decided to treat blocks of 200 addresses at a time.
Create a new PHP page and connect to the MySQL database in which there are addresses to which to send the email. We protect everything with a password, insert a text box for the body of the message and the last button that will trigger the sending of the message.

<?php

   $strInfo = $_REQUEST['regpers'];
   $strInit = $_REQUEST['deleteall'];

   $bInit = false;
   if (strlen($strInit) > 0 && $strInit == "on")
    $bInit = true;

   $totUsers = 0;
   $maxSend = 200;

   require_once('connection.php');
   mysql_select_db($database, $mydata);

   $query = "SELECT * FROM Users WHERE Send = 0
           AND Active = 1";
   $users = mysql_query($query, $mydata) or
           die(mysql_error());
   $userRow = mysql_fetch_assoc($users);
   $totUsers = mysql_num_rows($users);
   mysql_free_result($users);

   if (strlen($strInfo) > 0)
   {
 $strPws = $_REQUEST['pws'];
 if (strlen($strPws) <= 0)
 {
      mysql_close($mydata);
    exit();
 }
 if ($strPws != "mypassword")
 {
    mysql_close($mydata);
    exit();
 }

 if ($strInfo == "mysend")
 {
    $strInfo = $_REQUEST['news'];
    if (strlen($strInfo) > 0)
    {
  if ($bInit)
  {
      // delete last send
     $query = "UPDATE Users SET Send = 0";
     mysql_query($query, $mydata) or
                      die(mysql_error());
  }

  $strHeaders = "From: noreply@mydomain.com\n";
  $strHeaders .= "X-Mailer: News Corporation\n";
  $strHeaders .= "MIME-Version: 1.0\n";
  $strHeaders .= "Content-Type: text/html\n\n";

  $strMsg = '<br><br><p align="left">
                You have received this e-mail because
    subscribe to my business, if you want to
                stop the service please click ';

   $realSend = 0;

   $query = "SELECT IDUser, EMail FROM
                             Users WHERE Send = 0 
                             AND Active = 1 LIMIT 0,$maxSend";
   $mail = mysql_query($query, $mydata) or
                             die(mysql_error());
   $mailRow = mysql_fetch_assoc($mail);
   $mailNum = mysql_num_rows($mail);

   for ($i = 0; $i < $mailNum; ++$i)
   {
      $strMessage = $strInfo.$strMsg.'
      <a href="http://www.mydomain.com/
                                cancnews.php?idnews=XXXXXX'.
      $mailRow['IDUser'].'XXXXX">this
                                link</a></p></body>
                                </html>';
      if (!mail(strtolower(
                                $mailRow['EMail']), "News from
                                My Corporation", $strMessage,
                                $strHeaders))
      {
         $bMailError = true;
         break;
      }

      $query = "UPDATE Users
                            SET Send = 1 WHERE IDUser = ".
                            $postaRow['IDUser'];
      mysql_query($query, $mydata) or
                            die(mysql_error());

      $realSend++;

      $mailRow = mysql_fetch_assoc($mail);
   }

   mysql_free_result($mail);

   if ($bMailError)
      $strInfo .= " ERROR!!!!!";

   mail("myaddress@mydomain.com",
                          "Send to ".$realSend." users",
                          $strInfo.$strMsg."this link.
                          </body></html>", $strHeaders);
     }
      }
 }

 mysql_close($mydata);

?>

<html>
<head><title>Send News</title></head>
<body>

<h1 align="center">Send News</h1>

<?php echo '<h1 align="center">Users who have to
              have news: '.$totUsers.'</h1>'; ?>

<div align="center">
<form action="#" method="POST" name="news">

 <input name="pws" type="password" id="pws"
         title="Insert password" size="20" maxlength="20">
 <br><br>
 <input name="deleteall" type="checkbox" id="deleteall"
             title="Delete all send">Init zero
 <br><br>
 <textarea name="news" type="text" title="News"
          id="news" cols="60" rows="20"><html><body></textarea>
 <br><br>
 <input name="regpers" type="hidden" id="regpers"
                value="mysend">
 <input type="submit" name="send" id="send"
               title="Invio" value="Send">

</form>
</div>

</body>
</html>

To be sure I know by entering a password in order to send messages and also a variable that allows you to send email only if you send requests from the same page. The program seems to me quite clear, once and sent messages, users are labeled with a Boolean positive, we also have another field to control those who want to remove their submission. If you have proposals for the extension of the example, or you notice a mistake you can notify us via the comments.

Categories: PHP
Tags: MySQL
giampy107:
X

Cookies Policy

We use cookies to personalize content and ads, provide social media features and analyze our traffic. We also share information about your use of our site with our web analytics, advertising and social media partners who may combine it with other information you have provided to them or that they have collected based on your use of theirs. services.

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings