Email script problems

Im trying to make a script that uses a form inside a web page to email every email inside a database that simply stores inside a table their Name Email Password, i asked on another forum for help on how to make it and they gave me a while loop that echoed the information out and then told me to try and work it out to help me learn myself i got the idea they said it would work and it hasn’t, here is the script below with the lines marked that adobe dreamweaver CS5 believes are problems

<?php mysql_connect("mysql12.********.com","a9855336_*******","********"); mysql_select_db("a9855336_mail"); THIS LINE$mail = "$_POST['from']"; THIS LINE$result = "mysql_query("SELECT email FROM mail)"; while ($row = mysql_fetch_array($result)) { THIS LINE$to = $row['email']; $subject = $_POST['subject']; $message = $_POST['message']; THIS LINE $from = "[email][email protected][/email]"; mail('$to', '$subject', '$message', null, '-$from'); ?>

This is my database structure
My database info is as follows
database name: a9855336_mail
Table Name: mail
Row name containing email addresses: email
If anyone can help me fix it point me to a guide or help me make a better script please reply any help is greatly appriciated.



try this:

$mail = $_POST[‘from’];
$result = mysql_query(“SELECT email FROM mail”);
while ($row = mysql_fetch_array($result))
$to = $row[‘email’];
$subject = $_POST[‘subject’];
$message = $_POST[‘message’];
$from = “$mail”;
mail(’$to’, ‘$subject’, ‘$message’, null,’-$from’);

Thanks thats cleared all the problems dreamweaver had with the script i just get a Parse error: syntax error, unexpected $end in /home/a9855336/public_html/massemail.php now

Right that’s due to a missing closing } after the mail line to close the while loop:

[php]mail(’$to’, ‘$subject’, ‘$message’, null,’-$from’);

ahhhh, thanks once again!, Im guessing i need to set that script up with an email server to get it emailing, or what do i do?

the script should loop through all items in the database table and send an email to each one, but the script is trying to pull items from a form that have been sent via a post method so if you have a form before this step then you can test the script.

the email column in the database should contain the email address.

This is what i have and when i use the form it doesnt send me an email, My email is in the database, and i have no mailserver at the moment

       <form action="massemail.php" method="post">
      Name:<br />
      <input type="text" name="from" /><br />
      Subject:<br />
      <input type="text" name="subject" /><br />
      Password:<br />
      <input type="text" name="password" /><br />
      Message:<br />
      <textarea cols="75" rows="12" message></textarea><br />
      <input type="submit" /></form>
$mail = $_POST['from'];
$result = mysql_query("SELECT email FROM mail");
while ($row = mysql_fetch_array($result))  
$to = $row['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = "[email protected]";
mail('$to', '$subject', '$message', null, '-$from');

are you running this from a live domain or using a local setup such as xampp/lamp?

I’ve changed your script to use a single file to send out emails and collect date from the form, I’ve put comments so you can see what’s going on.

the php will not run untill the form has been submitted by placing all the code inside an if statement.

//connect to the database
mysql_select_db(“database name”);

if(isset($_POST[‘submit’])){ //if form has been sumitted process it

//get data from form

$from = $_POST[‘from’];
$subject = $_POST[‘subject’];
$password = $_POST[‘password’];
$message = $_POST[‘message’];

//add the email to from
$from = $from."";

//query database and pull email addresses
$result = mysql_query(“SELECT email FROM mail”);
while ($row = mysql_fetch_array($result))
$to = $row[‘email’];//email from database

    //send out the email
    $additionalheaders = "From: <$from>\r\n";
    $additionalheaders .= "Reply-To: $from";
    mail($to, $subject, $message, $additionalheaders);

echo "Email has been sent out.";

} // close if form submitted






A word of warning don’t post sensative information like your database host, username and password someone could easily come along and do things you wouldn’t want them to. I recommend editing your posts and removing this information from them.

THANK YOU SO VERY MUCH!, im usually very good with my db i usually *** it out, and im moving my host once i finish constructing the site

your welcome hopefully you can understand how the script works and learn from it, why are you leaving your current host?

Its a free host therefore it has limited bandwith and although reliable uptime it has limits, my dad has some small low energy using linux servers at home so i should be running it of that, if that doesnt work i will probably still use the free host, its great for a free host as it includes php :slight_smile:

yeah not many free hosts let you use php. there’s plenty of good hosting at cheap rates if you look about. If your dad has linux servers then your sorted.


Not sure if you know this or not but will allow you to send up to 100 emails per hour using the sendmail / phpmail commands however you will have to use your regular email address and smtp login if i remember correctly I haven’t had to set up sendmail with them in a while so am unsure exactly what all commands you have to use

hope this helps

Sponsor our Newsletter | Privacy Policy | Terms of Service