简体   繁体   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();
?>

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. 这是我用于发送邮件的php代码,如果我想向多个收件人发送邮件,并且电子邮件收件人存储在数据库中,则应对此代码进行哪些更改。基本上,我想实现任何电子商务网站上都存在的通知我功能。 So I want to implement in this code mysql database connection, sending multiple mails, after sending mail that entry should get deleted from database. 因此,我想在此代码中实现mysql数据库连接,发送多封邮件,发送邮件后,该条目应从数据库中删除。

Try using PHPMailer. 尝试使用PHPMailer。 http://phpmailer.worxware.com/ 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('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();

Or with the PHP mail function 或配合PHP邮件功能

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

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM