0

Following is my HTML Select field which gets all the emails from my database. Now I'm trying to send single or multiple emails with php. But It doesn't send any emails. Can you tell me what is wrong with my code ?

Html Code:

<tr>
<td valign="top">To</td>
<td>
<select multiple="multiple" size="7" name="to[]">
    <?php 
        $getemail = mysql_query("SELECT email FROM clients");
        while($res = mysql_fetch_array($getemail)){
        $email = inputvalid($res['email']); 
        echo "<option value='$email'>$email</option>";
        }
    ?>s
</select>
</td>
</tr>  

Php Code:

foreach($to as $total){                 
$total;
$headers  = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";  
$mail = mail($total, $subject, $msg, $headers);
}                       

Update-Full Code:

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" />
<table width="400" border="0" cellspacing="10" cellpadding="0" style="float:left;    
position:relative;">
<tr>
<td>Subject</td>
<td><input type="text" name="subject" value="<?php if(isset($_POST['subject'])) echo 
$_POST['subject']; ?>" class="tr"/></td>
</tr>
<tr>
<td valign="top">Message</td>
<td><textarea name="msg" class="textarea_email"><?php if(isset($_POST['msg'])) echo 
$_POST['msg']; ?></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Send Message" class=" submit" name="Submit"/></td>
</tr>  
</table>
<table style="float:left; position:relative; border:0px #000 solid;"  width="400" border="0" 
cellspacing="10" cellpadding="0">
<tr>
<td valign="top">To</td>
<td>
<select multiple="multiple" size="7" name="to[]">
    <?php 
        $getemail = mysql_query("SELECT email FROM clients") or die(mysql_error());;
        while($res = mysql_fetch_array($getemail)){
        $email = inputvalid($res['email']); 
        echo "<option value='$email'>$email</option>";
        }
    ?>s
</select>
</td>
</tr>  
</table>
</form>

Php code:

if(isset($_POST['Submit']) && $_POST['Submit'] == "Send Message")

{
$subject = inputvalid($_POST['subject']);   
$msg = inputvalid($_POST['msg']);   
$to = $_POST['to'];

if(isset($subject) && isset($msg) && isset($to)){
if(empty($subject) && empty($msg) && empty($to))    
    $err[] = "All filed require";
}
else{
    if(empty($to))
        $err[] = "Please select email address"; 

    if(empty($subject))
        $err[] = "Subject require"; 

    if(empty($msg))
        $err[] = "Message require"; 


}

if(!empty($err))
    {
        echo "<div class='error'>"; 
        foreach($err as $er)
        {
            echo "<font color=red>$er.</font>
<br/>";             
        }
        echo "</div>";
        echo "<br/>";
    }   
else{
    foreach($to as $total){                 
        echo $total;
        $headers  = "From: $from\r\n";
        $headers .= "Content-type: text/html\r\n";  
        $mail = mail($total, $subject, $msg, 
$headers);                      
        }
        if($mail)
        echo "<font color=green>Successfully sent your message. We will be get in 
touch with you. Thank You.</font/><br/><br/>";
        header("Refresh:10; url=email.php");
    }

}
3
  • What are your errors? What is the value of $mail? Have you checked your mail logs? It's up to your mail server / MTA once it receives the input from the PHP mail() command. PHP simply hands it off to the MTA and any errors from there will appear in your mail logs (that is, until it is sent; when it's sent, it's up to the receiving MTA). Also check your firewall settings. Commented Jul 16, 2013 at 9:37
  • @BLaZuRE I checked my email. I didn't get any email. There is no error but It's don't work. If you want then i can show my full code. Commented Jul 16, 2013 at 9:40
  • It's not as simple as that. PHP doesn't send mail. It tells another program to send mail. We don't know if you have such a program simply by your code. Read the documentation on the mail() function. It will return false if your email is not accepted for delivery by such a program. PHP does not send mail. It uses another program to send mail You need to check the mail logs of such a program (Postfix, Sendmail, etc.). Please output the value of $mail (using var_dump($mail); is helpful). Commented Jul 16, 2013 at 9:44

4 Answers 4

3

You can send multiple emails for one mail function.

Only pass email address with comma separated.

like "[email protected],[email protected]"

Sign up to request clarification or add additional context in comments.

Comments

1

first make sure that you query is correct.

Change the following line:

$getemail = mysql_query("SELECT email FROM clients");

to

$getemail = mysql_query("SELECT email FROM clients") or die(mysql_error());

Change the following line:

$total;

to

echo $total . '<BR />';

Save and refresh the page What is your output?

Do you get an errormessage?

Do you see emailadresses?

edit:

change the following line

$mail = mail($total, $subject, $msg, $headers);

to

if (!mail($total, $subject, $msg, $headers);) {
   echo 'This is not working';
}

What is your result?

1 Comment

Prevod, No there is no error message and I can see email address.
1

Check your apache-configs! I had the same problem on my local machine. as soon as i uploaded it on a fully configured Webserver, the code started to work! Maybe this solves your problem, too

Comments

0

Some time mail() function may not work so you should go with PHPMailer, for more details to use this, you can go through a good documentation :

Send mail using PHPMailer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.