使用SMTP发送多个数据

时间:2016-01-26 09:26:40

标签: php email smtp

这是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-&gt;正文中 它给了我错误:&#34;未正确配置&#34; 我必须使用SMTP,因为我的主机只提供SMTP .. 如何使用SMTP发送从用户获取的所有表格数据?

1 个答案:

答案 0 :(得分:0)

我不知道这是否能解决问题 - 如果您向邮件正文中添加更多内容,我无法理解为什么它会失败但是因为您没有表明我只能猜到它是是否以无效标记的方式完成?

<?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;
    }
?>