Jump to content

[SOLVED] Noob PHP & MySql mail question


DasHaas

Recommended Posts

I am workong on a script that sends an email to all the email addresses in one of the tables in my database. The email will be created in a form. Once i submit the form I want the script to send an email to each email address in my table. This is what i have thus far and when I submit the form I get the following error  ???:

sendmail: No recipients specified although -t option used

 

here is my code:

<html>
<head>
<link href="css/StyleSheet1.css" rel="stylesheet" type="text/css">
<body>
<!-- standard page header begins -->
<table width="100%" class="table1">
  <tr> 
    <td><div align="center" class="text2">Send Email to newsletter subscribers </div></td>
  </tr>
</table>
<!-- standard page header ends -->

<?php
// form not yet submitted
// display initial form
if (!$_POST['submit'])
{
?>
<br>
<table cellpadding="5" cellspacing="5" class="table1">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<tr>
    <td valign="top" class="text5">Group</td>
    <td> </td>
</tr>
<tr>
    <td valign="top" class="text5">Subject</td>
    <td><input name="txtsubject" type="text" class="table2" id="txtsubject" size="50" maxlength="250"></td>
</tr>
<tr>
    <td valign="top" class="text5">Email Body</td>
    <td valign="top" class="text5"><textarea name="txtbody" cols="47" rows="10" class="table2" id="txtbody"></textarea></td>
</tr>
<tr>
  <td colspan="2" valign="top" class="text5"><input name="submit" type="submit" id="submit" value="Submit">
    <input type="reset" name="Submit2" value="Reset"></td>
  </tr>
</form>
</table><br>
<br>
<?php
}
else
{
    // includes
include('Includes/PXS_Conf.php');

    // set up error list array
    $errorList = array();
    
    $msgsubject = $_POST['txtsubject'];
    $msgbody = $_POST['txtbody'];	    

    // validate text input fields
    if (trim($_POST['txtsubject']) == '') 
    { 
        $errorList[] = 'Invalid entry: Message Subject'; 
    }

    if (trim($_POST['txtbody']) == '') 
    { 
        $errorList[] = "Invalid entry: Message Body"; 
    }
   
    // check for errors
    // if none found...
    if (sizeof($errorList) == 0)
    {
        // open database connection
        $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect to MySql server! Please contact PXS customer support.');

        // select database
        mysql_select_db($db) or die ('Unable to select database! Please contact PXS customer support.');

        // generate and execute query
        $query = "SELECT email FROM PXS_Newsletter WHERE verified = 1";
        $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// send email
$To = $row['$result'];
$Subject = "<".$msgsubject.">";
$Body =  "<".$msgbody.">";
mail($To, $Subject, $Body);

// print result
echo '<center>Message sent</center>';

        // close database connection
        mysql_close($connection);
    }
    else
    {
        // errors found
        // print as list
        echo '<font size=-1>The following errors were encountered:'; 
        echo '<br>';
        echo '<ul>';
        for ($x=0; $x<sizeof($errorList); $x++)
        {
            echo "<li>$errorList[$x]";
        }
        echo '</ul></font>';
    }
}
?>

<!-- standard page footer begins -->
<table width="100%" class="table1">
  <tr> 
    <td><div align="center" class="text1">
      <div align="center">Copyright © 2006 Projekt-X-Studios. 
        All Rights Reserved </div>
    </div></td>
  </tr>
</table>
</body>
</html>

 

Any help would be greatly appreciated :'(

Link to comment
Share on other sites

Instead of

 

        $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// send email
$To = $row['$result'];
$Subject = "<".$msgsubject.">";
$Body =  "<".$msgbody.">";
mail($To, $Subject, $Body);

 

you should do

 

        $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// send email
if (mysql_num_rows($result) == 0) {
  # Error! Could not find email
} else {
  $To = mysql_fetch_result($result, 0, 'email');
  $Subject = "<".$msgsubject.">";
  $Body =  "<".$msgbody.">";
  mail($To, $Subject, $Body);
}

 

Actually, that will only work for ONE email only.  Do you want to send emails to several people?  If so it needs to be re-written a little

Link to comment
Share on other sites

Assuming the table is called users, and you want to email every user with the address in the email field:

 

<?php

$subject = "Test Email Subject";
$body = "Test message body.";

$query = mysql_query("SELECT email FROM users") or die(mysql_error());

while($row = mysql_fetch_row($query)) {

$email = $row[0];

if(mail($email, $subject, $body)) {
  echo "Email sent to $email<br />";
} else {
  echo "<span style=\"color: red;\">Email not sent to $email</span><br />";
}

}

?>

 

That can be adapted to suit your needs :)

Link to comment
Share on other sites

Im still getting the following error:

 

sendmail: No recipients specified although -t option used

 

Here is my code with the changes you gave me:

<html>
<head>
<link href="css/StyleSheet1.css" rel="stylesheet" type="text/css">
<body>
<!-- standard page header begins -->
<table width="100%" class="table1">
  <tr> 
    <td><div align="center" class="text2">Send Email to newsletter subscribers </div></td>
  </tr>
</table>
<!-- standard page header ends -->

<?php
// form not yet submitted
// display initial form
if (!$_POST['submit'])
{
?>
<br>
<table cellpadding="5" cellspacing="5" class="table1">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<tr>
    <td valign="top" class="text5">Group</td>
    <td> </td>
</tr>
<tr>
    <td valign="top" class="text5">Subject</td>
    <td><input name="txtsubject" type="text" class="table2" id="txtsubject" size="50" maxlength="250"></td>
</tr>
<tr>
    <td valign="top" class="text5">Email Body</td>
    <td valign="top" class="text5"><textarea name="txtbody" cols="47" rows="10" class="table2" id="txtbody"></textarea></td>
</tr>
<tr>
  <td colspan="2" valign="top" class="text5"><input name="submit" type="submit" id="submit" value="Submit">
    <input type="reset" name="Submit2" value="Reset"></td>
  </tr>
</form>
</table><br>
<br>
<?php
}
else
{
    // includes
include('Includes/PXS_Conf.php');

    // set up error list array
    $errorList = array();
    
    $msgsubject = $_POST['txtsubject'];
    $msgbody = $_POST['txtbody'];	    

    // validate text input fields
    if (trim($_POST['txtsubject']) == '') 
    { 
        $errorList[] = 'Invalid entry: Message Subject'; 
    }

    if (trim($_POST['txtbody']) == '') 
    { 
        $errorList[] = "Invalid entry: Message Body"; 
    }
   
    // check for errors
    // if none found...
    if (sizeof($errorList) == 0)
    {
        // open database connection
        $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect to MySql server! Please contact PXS customer support.');

        // select database
        mysql_select_db($db) or die ('Unable to select database! Please contact PXS customer support.');

        // generate and execute query
        $query = mysql_query("SELECT email FROM PXS_Newsletter WHERE verified = 1") or die (mysql_error());
        $email = $row[0];

// send email
$To = $email;
$Subject = "<".$msgsubject.">";
$Body =  "<".$msgbody.">";
mail($To, $Subject, $Body);

// print result
echo '<center>Message sent</center>';

        // close database connection
        mysql_close($connection);
    }
    else
    {
        // errors found
        // print as list
        echo '<font size=-1>The following errors were encountered:'; 
        echo '<br>';
        echo '<ul>';
        for ($x=0; $x<sizeof($errorList); $x++)
        {
            echo "<li>$errorList[$x]";
        }
        echo '</ul></font>';
    }
}
?>

<!-- standard page footer begins -->
<table width="100%" class="table1">
  <tr> 
    <td><div align="center" class="text1">
      <div align="center">Copyright © 2006 Projekt-X-Studios. 
        All Rights Reserved </div>
    </div></td>
  </tr>
</table>
</body>
</html>

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.