Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
<?php
if(isset($_POST['email'])) {
    // CHANGE THE TWO LINES BELOW
    $email_to = "[email protected]";
    $email_subject = "Website Enquiry";
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone']) ||
        !isset($_POST['comment'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
    $first_name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['phone']; // not required
    $comments = $_POST['comment']; // required
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
    $email_message .= "First Name: ".clean_string($first_name)."\n\n";
    $email_message .= "Email: ".clean_string($email_from)."\n\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n\n";
    $email_message .= "Comments: ".clean_string($comments)."\n\n";   
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
require("404.html"); 
?>
<!-- place your own success html below 
Thank you for contacting us. We will be in touch with you very soon.-->
<?php
}
die();
?>

This is my php code for sending mail if i want to send mail to multiple recipient and that email addressees are stored in database what changes should make on this code.Basically I want to implement notify me feature which is present on any e-commerce website.So I want to implement in this code mysql database connection,sending multiple mails,after sending mail that entry should get deleted from database

Thanks is advance

share|improve this question
1  
What kind of question is that: "what changes should make on this code"? How should we know what you want to change? You didn't even tell us why you want to change things. –  arkascha Dec 5 '13 at 10:32
    
i want to send multiple emails .... –  Er Pathak Harshal Dec 5 '13 at 10:35
2  
Then do it, what is your problem with that? Sorry, you cannot simply dump some code here, not add any details to what it is that you want to do and why you cannot do what you want to do and expect others to help! Try to think from a readers perspective: he does not know what your problem is in this... –  arkascha Dec 5 '13 at 10:41
1  
And: there is one general rule for questions here: first you have to try yourself and only if you fail, then you should ask here. And add your attempts so far with a description of why things fail. –  arkascha Dec 5 '13 at 10:43

1 Answer 1

Try using PHPMailer. http://phpmailer.worxware.com/

Here is a snippet u could modify

    require_once(LIB.'class.phpmailer.php');

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPDebug  = SMTP_DEBUG;
    $mail->SMTPAuth   = SMTP_AUTH;
    $mail->SMTPSecure = SMTP_SECURE; 
    $mail->Host       = SMTP_HOST;
    $mail->Port       = SMTP_PORT;
    $mail->Username   = SMTP_USERNAME;
    $mail->Password   = SMTP_PASSWORD; 
    $mail->SetFrom('[email protected]', 'From');
    $mail->AddReplyTo('[email protected]', 'From');

    // set subject
    $mail->Subject = 'Subject';

    $to = '[email protected], [email protected]';

    $arrTo = explode(',', $to);
    foreach ($arrTo as $to) {
        $mail->AddAddress($to);
    }

    $mail->MsgHTML('Your message');
    $mail->Send();

Or with the PHP mail function

$to = "[email protected], [email protected], [email protected]"

mail($to, 'subject', 'body');

To build the comma separated string from a database you could do something like:

$arrEmail = array();

while ($row = mysql_fetch_assoc($result)) {

    $arrEmail[] = $row['emailAddress'];
}

$to = join(',', $arrEmail);
share|improve this answer
    
thanks Andy Sir –  Er Pathak Harshal Dec 5 '13 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.