PHP - Php Newsletter Help
I have a table in my database named newsletter, with a field named email. Is there a way in php to delete the row with email address field if the email is undeliverable to that email address because it no longer exists?
Thank you in advance! Similar TutorialsIs there anything I should change on this code? I'll use it to send about 20000 emails... I don't like this one, because while sending the emails, the page keep loading... I kind dont "trust" on this script.. index.php Code: [Select] <?php include('config.php'); include('class.phpmailer.php'); include('email.php'); // Get users info $getUser_sql_not_sent = "SELECT * FROM emails WHERE sent = '0'"; $getUser_not_sent = mysql_query($getUser_sql_not_sent); $getUser_num_not_sent = mysql_num_rows($getUser_not_sent); $getUser_sql_sent = "SELECT * FROM emails WHERE sent = '1'"; $getUser_sent = mysql_query($getUser_sql_sent); $getUser_num_sent = mysql_num_rows($getUser_sent); $getUser_sql_all = "SELECT * FROM emails"; $getUser_all = mysql_query($getUser_sql_all); $getUser_num_all = mysql_num_rows($getUser_all); $getUser_sql_accessed = "SELECT * FROM emails WHERE accessed = '1'"; $getUser_accessed = mysql_query($getUser_sql_accessed); $getUser_num_accessed = mysql_num_rows($getUser_accessed); $money = $getUser_num_accessed * 0.20; $money_formated = number_format($money, 2, ',', ''); $x = $getUser_num_sent * 100; $y = $getUser_num_all; $percentage = $x / $y; $percentage_formated = number_format($percentage, 0, '', ''); if ($getUser_num_not_sent == $getUser_num_all) { echo '<form action = "send.php" method = "post">'; echo '<strong>Assunto do Email</strong><br />'; echo $emailSubject; echo '<br />'; echo '<strong>Corpo do Email</strong><br />'; echo $emailBody; echo '<br /><br />'; echo '<input name = "send" type = "submit" value = "Enviar Emails" />'; echo '</form>'; } else { echo "Foram enviados " . $percentage_formated . "&#37; dos emails. " . $getUser_num_accessed . " acessaram o site, bonificação de R&#36; " . $money_formated . " para o Claudius Ibn."; } ?> Code: [Select] <?php include('config.php'); include('class.phpmailer.php'); include('email.php'); echo 'Os emails estao sendo enviados, voce pode sair do site que o processo continuara mesmo assim.'; // Get users info $getUser_sql_not_sent = "SELECT * FROM emails WHERE sent = '0'"; $getUser_not_sent = mysql_query($getUser_sql_not_sent); $getUser_num_not_sent = mysql_num_rows($getUser_not_sent); $mail = new PHPMailer(); while ($row = mysql_fetch_array($getUser_not_sent)) { // Get the current user's email $emailUser = $row['email']; // Define mail object and mail parameters $mail -> From = 'campainha@ibnshare.com'; $mail -> FromName = 'Campainha'; $mail -> AddAddress($emailUser); $mail -> Subject = $emailSubject; $mail -> Body = $emailBody; // Send and verify if(!$mail -> Send()) { echo 'Erro: '. $mail -> ErrorInfo; } else { mysql_query("UPDATE emails SET sent = '1' WHERE email = '$emailUser'"); } sleep(30); } ?> Hey guys so I'm working on creating my own CMS for my website and one of the features I am having trouble with is creating the script to mail a newsletter to everyone in the database. I thought it would be simple enough but I cant seem to figure it out. When I run the code below everything seems to work however only the first email in the database actually receives an email. I think it might be something with my while() loop but I'm not sure. Any help would be greatly appreciated. Code: [Select] $subject = $_POST['subject']; $message = nl2br($_POST['message']); // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'From: B.net' . "\r\n"; // Connect to database // requires Global Functions ratings_db_connect(); // Finds the number of verified subscriptions $qry ="SELECT * FROM subscriptions WHERE verified ='yes'"; $result = mysql_query($qry); while ($email = mysql_fetch_array($result)){ $email = $email['email']; mail ($email,$subject,$message,$headers); } Hello all! I am trying to send mail using the "Mail.php" function but I cant figure out whats going wrong. The insert function works in the database but not the sending mail function. Please let point me in the right direction, thanks for your help in advance!
<?php if(isset($_POST)){ //If the post is not empty, continue if(!empty($_POST)) { include_once 'includes.php'; require_once 'Mail.php'; $from = "Name <email@foobar.com>"; $subject = $_POST['subject']; $text = $_POST['newsletterBody']; $host = "host"; $port = "port"; $username = "EMAILUSERNAME"; $password = "EMAILPASSWORD"; echo "Subject: " . $subject; echo "<br />Body: " . $text; echo "<br />Host: " . $host; echo "<br />Port: " . $port . "<br />"; $sql = 'INSERT INTO newsletter_log (date,title,body) VALUES (:time,:subject,:body)'; $params = array( ':subject' => $subject, ':body' => $text, ':time' => time()); $poststmt = $DBH->prepare($sql); $poststmt->execute($params); if($poststmt) { echo "Saved to the database."; } else { echo "Not saved."; } $sqlSubUsers = 'SELECT * FROM test_newsletter WHERE newsletter_id = 1'; $newsletterStmt = $DBH->prepare($sqlSubUsers); $newsletterStmt->execute(); $userIds = $newsletterStmt->fetch(PDO::FETCH_ASSOC); echo $userIds; foreach ($userIds as $row){ $getUserEmailSql = 'SELECT email_addr FROM newsletter_emails WHERE id =' . $row['people_id']; $getUserEmailStmt = $DBH->prepare($getUserEmailSql); $getUserEmailStmt->execute(); $userEmail = "<" . $getUserEmailStmt->fetch(PDO::FETCH_COLUMN,0) . ">"; $userEmail->closeCursor(); $html = "<html><body>" . $text . "</html></body>"; $headers = array ('From' => $from, 'To' => $userEmail, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $html); echo $userEmail; if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); } else { echo("<p>Message successfully sent!</p>"); } } echo 'Newsletter Sent.'; } else { echo '<h3>No newsletter was sent.</h3>'; } } ?>I have removed the emails, host, port, username and password along with in other information I did not think I should send over. I have jobs portal where registered users posts new jobs searches
This weekly newsletter will send all the jobs posted in the site (in a week) to the registered users but in a more personalized way: For example, posted jobs looking for designers should be sent to users registered as designers.
what is the best way to do this? any recommendations? Thanks in advance.
I found a newsletter script called 'easy newsletter' which allows me to set up a page for users to sign up to a news letter emailing list. There is then another page that a admin can log into and create a simple text email to send out to everyone that has signed up. My problems is i require the email to include a attachment and have no clue how i can add this to the script to get it working. Can anyone help me? The script that sends out the email is included below if this helps: <?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(); ?> 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 , please can someone help me . 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(); ?> This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=313625.0 I am working on building a newsletter with PHP. I have gotten everything to work but the pulling of the email address.
First is the sendnl.php file
<?php require("../PHPMailer/class.phpmailer.php"); include 'includes.php'; $mail = new PHPMailer; $subject = $_POST['subject']; $text = $_POST['newsletterBody']; $mail->IsSMTP(); $mail->Host = 'localhost'; $mail->Port = '465'; $mail->SMTPAuth = true; $mail->Username = '******USERNAME'; $mail->Password = '*****PASSWORD'; $mail->SMTPAuth = true; $mail->SMTPSecure = 'ssl'; $mail->From = '***EMAIL'; $mail->FromName = '****FROMNAME'; $email = getEmail("73", $DBH); foreach ($email as $nlEmail) { $addEmail = $nlEmail->email; $mail->AddAddress($addEmail); $DBH = null; $addEmail = ""; } $mail->AddReplyTo('******EMAIL', '*****EMAILNAME'); $mail->WordWrap = 50; $mail->IsHTML(true); $mail->Subject = $subject; $mail->Body = $text; $mail->AltBody = $text; if(!$mail->Send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit; } echo 'Message has been sent'; ?>Next is part of the includes file going over the function and class function getEmail($inId, $DBH) { if (!empty($inId)) { $blogstmt = $DBH->prepare("SELECT email_addr FROM newsletter_emails WHERE id = :userId"); $blogstmt->bindParam(":userId", $inId, PDO::PARAM_STR, 6); $blogstmt->execute(); } else { $blogstmt = $DBH->prepare("SELECT * FROM newsletter_emails"); $blogstmt->execute(); } $postArray = array(); $results = $blogstmt->fetchAll(PDO::FETCH_ASSOC); foreach($results as $row){ $myPost = new nlEmails($row["id"], $row['email'], $DBH); array_push($postArray, $myPost); } return $postArray; $DBH = null; } class nlEmails { public $id; public $email; function __construct($inId=null, $inEmail=null, $DBH) { if(!empty($inId)) { $this->id = $inId; } if(!empty($inEmail)) { $this->email = $inEmail; } } }It gives me an Unidentified index error message for the "id" and "email" used in the getEmail function. Thanks for any help or pointers. Edited by travisco87, 08 October 2014 - 03:33 PM. Hi, I am using the phpmailer class and a little script I found online, I have a form with a textarea called "plain" which submits and takes the action through to this page: Code: [Select] <?php #include PHPMailer class and database connection include("class.phpmailer.php"); include("connect.php"); #remove slashes from content $plain = stripslashes($_POST["plain"]); #initiate PHPMailer class $mail = new PHPMailer(); #set the from e-mail address $mail->From = "email@email.com"; #set the from name $mail->FromName = "myname"; #the subject of the email $mail->Subject = "Our Newsletter"; #the HTML content of the email $mail->Body = $plain; #loop through e-mail addresses $query = "SELECT email FROM member"; $result = mysql_db_query ("define_pete", $query); while ($myrow = mysql_fetch_array($result)){ #add subscribers address as the recipient $mail->AddAddress($myrow["fldEmail"]); #sends the newsletter $mail->Send(); #clears the recipient address $mail->ClearAddresses(); } ?> It gives no errors and goes through, yet it definitely sends no emails out, I have made sure the emails are correct in the field email in the table member but it is, anyone got any clue what is going wrong? Appreciated |