使用 SMTP 发送多个数据
Sending multiple data with 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->body
它给我错误:"not configured properly "
我必须使用 SMTP,因为我的主机只提供 SMTP..
我如何使用 SMTP 发送从用户那里获取的所有数据?
我不知道这是否会解决问题 - 我不明白为什么如果您向邮件正文添加更多内容它会失败,但由于您没有表明我只能猜测它是在这样的方式来制作无效的标记?
<?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;
}
?>
这里是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
它给我错误:"not configured properly "
我必须使用 SMTP,因为我的主机只提供 SMTP..
我如何使用 SMTP 发送从用户那里获取的所有数据?
我不知道这是否会解决问题 - 我不明白为什么如果您向邮件正文添加更多内容它会失败,但由于您没有表明我只能猜测它是在这样的方式来制作无效的标记?
<?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;
}
?>