鍍金池/ 問答/PHP  HTML/ php表單提交至mysql如何同時發(fā)送至指定郵箱

php表單提交至mysql如何同時發(fā)送至指定郵箱

現(xiàn)在通過html提交表單,可以實現(xiàn)把數(shù)據(jù)插入到mysql中,

php代碼如下

  <?php 
    session_start(); 
    $username=$_REQUEST["username"]; 
    $phone=$_REQUEST["phone"]; 
    $datetime=$_REQUEST["datetime"]; 
    $con=mysql_connect("localhost","root","root"); 
    if (!$con) { 
      die('數(shù)據(jù)庫連接失敗'.$mysql_error()); 
    } 
    mysql_select_db("user_info",$con); 
    $dbusername=null; 
    $dbphone=null; 
    $result=mysql_query("select * from user_info where phone ='{$phone}' and isdelete =0;"); 
    while ($row=mysql_fetch_array($result)) { 
      $dbusername=$row["username"]; 
      $dbphone=$row["phone"]; 
    } 

    if(!is_null($dbphone)){ 
  ?> 
  <script type="text/javascript"> 
    alert("手機號已存在"); 
    window.location.href="index.html"; 
  </script>  
  <?php  
    } 
    
    mysql_query("insert into user_info (username,phone,datetime) values('{$username}','{$phone}',now())") or die("存入數(shù)據(jù)庫失敗".mysql_error()) ; 
    mysql_close($con); 
  ?> 
  <script type="text/javascript"> 
    alert("注冊成功"); 
    window.location.href="index.html"; 
  </script> 

如何在這個基礎(chǔ)上同時把插入數(shù)據(jù)庫的內(nèi)容發(fā)送至指定郵箱呢?有沒有案例參考?

回答
編輯回答
尐潴豬
  1. 可以使用php mail擴展。按手冊添加相關(guān)配置之后調(diào)用

http://php.net/manual/zh/mail...

$to = "someone@example.com";         // 郵件接收者
$subject = "參數(shù)郵件";                // 郵件標題
$message = "Hello! 這是郵件的內(nèi)容。";  // 郵件正文
$from = "someonelse@example.com";   // 郵件發(fā)送者
$headers = "From:" . $from;         // 頭部信息設(shè)置
mail($to,$subject,$message,$headers);
echo "郵件已發(fā)送";

2.也可以使用phpmailer發(fā)送
https://github.com/PHPMailer/...

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
2017年9月20日 14:32
編輯回答
尛曖昧

composer 引入phpmailer/phpmailer 依賴phpmailer/phpmailer

2017年8月19日 12:10
編輯回答
影魅

headfirst php&mysql有同樣項目

2017年1月14日 00:49
編輯回答
瘋子范

發(fā)送郵件放入異步隊列處理

2017年6月29日 14:35
編輯回答
何蘇葉

我就是這么做的,但是發(fā)送很慢,導(dǎo)致提交表單要等很久。

2017年10月17日 18:06
編輯回答
呆萌傻

我覺得直接用大廠的郵件發(fā)送api比較方便點

2017年2月17日 15:54