【问题标题】:Sending multiple data with SMTP使用 SMTP 发送多个数据
【发布时间】:2016-01-26 09:26:39
【问题描述】:

这里是 index.html 代码;

<form name="contactform" method="POST" action="send.php">
Name: <input type="text" name="ad_soyad" size="25"><br />
Telephone: <input type="text" name="tel" size="25"><br />
E-Mail: <input type="text" name="email"  size="25"><br />
Address: <textarea rows="5" name="adres"  cols="25"></textarea><br />
Message: <textarea rows="5" name="mesaj"  cols="25"></textarea><br /> 
<input type="submit" name="button" value="Gonder"> 
</form>

这里是 send.php 代码:

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();                                    
$mail->Host = 'blablabla.com'; 
$mail->SMTPAuth = true;                     
$mail->Username = 'blablabla.com';                
$mail->Password = 'password';                          
$mail->SMTPSecure = 'tls';                           
$mail->Port = 587;

if(($ad_soyad=="") or ($tel =="") or ($email=="") or ($mesaj=="")){

echo "<center>Please fill the required fields.<br><a href=index.html>Go back</a></center>";

}
else
{

$mail->setFrom($_POST['email']);
$mail->addAddress('contact@blablabla.com', 'BlaBlaBla');        

$mail->isHTML(true);                              
$mail->Subject='Contact Form Message';
$mail->Body = ($_POST['mesaj']);


$smtp = new SMTP;    
//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;

if(!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent.';
}


}

?>

它有效。
但只发送姓名、电子邮件和消息。
因此,我想添加更多字段,例如电话或地址等。
但是当我尝试将电话和消息一起写时,我无法将它们放入 $mail->body 它给了我错误:“未正确配置” 我必须使用 SMTP,因为我的主机只提供 SMTP.. 如何使用 SMTP 发送表单从用户获取的所有数据?

【问题讨论】:

    标签: php email smtp


    【解决方案1】:

    我不知道这是否能解决问题 - 我不明白为什么如果您向消息正文添加更多内容会失败,但由于您没有表明我只能猜测它是在这样的如何进行无效标记?

    <?php
    
        /* Are these variables defined before this point? */
        if( ( $ad_soyad=="" ) or ( $tel =="" ) or ( $email=="" ) or ( $mesaj=="" ) ){
            echo "<center>Please fill the required fields.<br><a href=index.html>Go back</a></center>";
        } else {
    
            /* only load the libraries if the above variables are not empty */
            require 'PHPMailerAutoload.php';
    
            $mail = new PHPMailer;
            $mail->isSMTP();                                    
            $mail->Host = 'blablabla.com'; 
            $mail->SMTPAuth = true;                     
            $mail->Username = 'blablabla.com';                
            $mail->Password = 'password';                          
            $mail->SMTPSecure = 'tls';                           
            $mail->Port = 587;
    
    
            /* some basic filtering of user supplied variables */
            $email=filter_input( INPUT_POST, 'email', FILTER_VALIDATE_EMAIL );
            $message=filter_input( INPUT_POST, 'mesaj', FILTER_SANITIZE_STRING );
            $name=filter_input( INPUT_POST, 'ad_soyad', FILTER_SANITIZE_STRING );
            $tel=filter_input( INPUT_POST, 'tel', FILTER_SANITIZE_STRING );
            $address=filter_input( INPUT_POST, 'adres', FILTER_SANITIZE_STRING );
    
    
            /* prepare message components */
            $msg=array();
            $msg[]="Name:".$name;
            $msg[]="Phone:".$tel;
            $msg[]="Address:".$address;
            $msg[]="Message:".$message;
    
    
    
    
            $mail->setFrom( $email );
            $mail->addAddress('contact@blablabla.com', 'BlaBlaBla');        
            $mail->isHTML(true);                              
            $mail->Subject='Contact Form Message';
            $mail->Body = implode( "\r\n", $msg );
    
    
            $smtp = new SMTP;    
            //Enable connection-level debug output
            $smtp->do_debug = SMTP::DEBUG_CONNECTION;
    
            if( !$mail->send() ) {
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            } else {
                echo 'Message sent.';
            }
            $mail = $smtp = null;
        }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-11-11
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 2016-04-09
      • 2013-09-20
      • 2012-03-08
      • 2020-09-12
      • 2016-11-17
      相关资源
      最近更新 更多