簡體   English   中英

使用PHP腳本發送郵件

[英]Mail Send Using PHP Script

<?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代碼,如果我想向多個收件人發送郵件,並且電子郵件收件人存儲在數據庫中,則應對此代碼進行哪些更改。基本上,我想實現任何電子商務網站上都存在的通知我功能。 因此,我想在此代碼中實現mysql數據庫連接,發送多封郵件,發送郵件后,該條目應從數據庫中刪除。

嘗試使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM