使用PHP脚本发送邮件

时间:2013-12-05 10:28:56

标签: php email connection

<?php
if(isset($_POST['email'])) {
    // CHANGE THE TWO LINES BELOW
    $email_to = "mail@domain.com";
    $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();
?>

这是我发送邮件的PHP代码,如果我想向多个收件人发送邮件,并且电子邮件收件人存储在数据库中应该对此代码进行哪些更改。基本上我想实现通知我的功能,它存在于任何e上 - 电子商务网站。所以我想在这段代码中实现mysql数据库连接,发送多封邮件,发送邮件后该条目应该从数据库中删除。

1 个答案:

答案 0 :(得分:0)

尝试使用PHPMailer。 http://phpmailer.worxware.com/

以下是您可以修改的代码段

    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('from@domain.com', 'From');
    $mail->AddReplyTo('from@domain.com', 'From');

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

    $to = 'email1@email.com, email2@email.com';

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

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

或使用PHP邮件功能

$to = "address@one.com, address@two.com, address@three.com"

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

要从数据库构建逗号分隔的字符串,您可以执行以下操作:

$arrEmail = array();

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

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

$to = join(',', $arrEmail);