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="update@marksmarathonmadness.co.uk";
//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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.