Jump to content

Send Email


bravo14

Recommended Posts

Hi guys

 

I am probably missing something completely obvious, but what I am trying to do is everytime an entry gets added to a database an email is sent out.

 

<?php 
  
  // Connect to the database 
  $cnx = mysql_connect('ip, 'user', 'pwd') 
         OR die("Unable to connect to database!"); 
  mysql_select_db('db', $cnx); 
   
   
  if ($_POST['submit_form'] == 1)  { 
    // Set Variables
    $data = mysql_real_escape_string(trim($_POST['fcktext'])); 
$topic = ($_POST['topic']);
$title = ($_POST['title']);
$mileage = $_POST['mileage'];
$intro = substr ($data,0,100);
$subject = "A new Post has been added to marksmarathonmadness.co.uk";
$adminmail="[email protected]";
//Send Email Update
$getlist='SELECT * FROM `email_table` WHERE `validated` = \'1\''; //select validated e-mails
      $getlist2=mysql_query($getlist) or die("Could not get list");
      while($getlist3=mysql_fetch_array($getlist2))
      {
         $headers = "From: $adminmail \r\n"; 
         $headers.= "Content-Type: text/html; charset=ISO-8859-1 ";  //send HTML enabled mail
         $headers .= "MIME-Version: 1.0 ";     
         mail("$getlist3[email]","$subject","Hi, There has been a new post added to www.marksmarathonmadness.co.uk",$headers);
      }

// Save to the database 
    $res = mysql_query("INSERT INTO posts (topic_id, post, title,intro,distance) VALUES('$topic','$data','$title','$intro','$mileage')");
$post = mysql_query("UPDATE `topics` SET `posts` = posts+'1' WHERE `topic_id` ='".$topic."'");
     
    if (!$res) 
      die("Error saving the record!  Mysql said: ".mysql_error()); 
     
    // Redirect to self to get rid of the POST 
    header("Location: index.php"); 
  }
  ?>

 

The entry gets added to the database, but the email doesn't get sent.

 

Any ideas

 

Many thanks

 

Mark

Link to comment
https://forums.phpfreaks.com/topic/136912-send-email/
Share on other sites

Shouldn't it be $getlist3['email']?  Although I would have thought that would generate an error...

 

To troubleshoot you should echo the contents of mail() to check all is OK:

 

echo "$getlist3[email]","$subject","Hi, There has been a new post added to www.marksmarathonmadness.co.uk",$headers;

Link to comment
https://forums.phpfreaks.com/topic/136912-send-email/#findComment-715179
Share on other sites

Have you successfully sent any emails from a php script on your server, using that From: address, so that you know that email is working at all?

 

Each header line needs a \r\n and since the header is static (does not change) the code that sets it should be before your while() loop.

 

Your $getlist= statement has some questionable use of quotes in it and if it is not generating a php syntax error, it is likely not matching any rows in your database. Have you tried echoing something (like the email address) inside of the while() loop so that you know that it is being executed? Edit: Which is what Mad Mick has suggested.

 

Give this a try for $getlist -

$getlist="SELECT * FROM `email_table` WHERE `validated` = 1"; //select validated e-mails

 

You need to use mysql_real_escape_string() on each $_POST variable that you place into a query.

Link to comment
https://forums.phpfreaks.com/topic/136912-send-email/#findComment-715185
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

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.