หัวข้อ: form ส่งเมล์ไม่ไปเลยครับ แต่อัพบาง Host กลับส่งได้ปกติ
เริ่มหัวข้อโดย: kengzz ที่ 15 มิถุนายน 2016, 23:06:06
ตอนนี้มีปัญหาเกี่ยวกับ Form ครับ ส่งเข้าเมล์ครับ นี่คือ Code ที่ใช้ ใช้กับบาง Host ได้ บาง Host ก็ขึ้น Error ครับ ผมพลาดตรงไหนรบกวนชี้แนะครับผม ขอขอบคุณครับ : ) #ทีมดีไซน์ #ไม่ค่อยได้จับ Code <?php
session_cache_limiter( 'nocache' ); header( 'Expires: ' . gmdate( 'r', 0 ) ); header( 'Content-type: application/json' );
$to = '[email protected]';
$email_template = 'simple.html';
$subject = strip_tags($_POST['name']. ", ต้องการรับบริการตรวจสุขภาพจาก Best-Be"); $email = strip_tags($_POST['email']); $phone = strip_tags($_POST['phone']); $name = strip_tags($_POST['name']); $opp = strip_tags($_POST['opp']); $message = strip_tags($_POST['message']); $income = trim($_POST['income']); $result = array();
if(empty($name)){
$result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong> ไม่ได้ใส่ชื่อ.' ); echo json_encode($result ); die; }
if(empty($email)){
$result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong> ไม่ได้กรอกอีเมล์.' ); echo json_encode($result ); die; } if(empty($opp)){
$result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong> ไม่ได้กรอกอาชีพ.' ); echo json_encode($result ); die; } if(empty($phone)){
$result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong> ไม่ได้ใส่เบอร์โทร.' ); echo json_encode($result ); die; }
$headers = "From: " . $name . ' <' . $email . '>' . "\r\n"; $headers .= "Reply-To: ". $email . "\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$templateTags = array( '{{subject}}' => $subject, '{{email}}'=>$email, '{{message}}'=>$message, '{{name}}'=>$name, '{{opp}}'=>$opp, '{{income}}'=>$income, '{{phone}}'=>$phone );
$templateContents = file_get_contents( dirname(__FILE__) . '/email-templates/'.$email_template);
$contents = strtr($templateContents, $templateTags);
if ( mail( $to, $subject, $contents, $headers ) ) { $result = array( 'response' => 'success', 'message'=>'<strong>ขอบคุณครับ</strong> <br><img src="assets/images/logo.png" width="100" height="61">.' ); } else { $result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong> Can\'t Send Mail.' ); }
echo json_encode( $result );
die;
หัวข้อ: Re: form ส่งเมล์ไม่ไปเลยครับ แต่อัพบาง Host กลับส่งได้ปกติ
เริ่มหัวข้อโดย: rapiz ที่ 15 มิถุนายน 2016, 23:30:17
น่าจะเป็นที่ ISP ของ Hosting ที่คุณใช่งานไม่มี Relay SMTP นะครับ ในกรณีแนะนำ PHPMailer ครับ (ใช้ SMTP Server ของ GMAIL)
ลองเชคว่า IP ติด Blacklist อยู่รึป่าวที่ลิ้งนี้ http://mxtoolbox.com/blacklists.aspx
ฝากโฮสลายเซ็นด้วยคับ
หัวข้อ: Re: form ส่งเมล์ไม่ไปเลยครับ แต่อัพบาง Host กลับส่งได้ปกติ
เริ่มหัวข้อโดย: kengzz ที่ 15 มิถุนายน 2016, 23:36:28
น่าจะเป็นที่ ISP ของ Hosting ที่คุณใช่งานไม่มี Relay SMTP นะครับ ในกรณีแนะนำ PHPMailer ครับ (ใช้ SMTP Server ของ GMAIL)
ลองเชคว่า IP ติด Blacklist อยู่รึป่าวที่ลิ้งนี้ http://mxtoolbox.com/blacklists.aspx
ฝากโฮสลายเซ็นด้วยคับ
ได้ครับผม เดี๋ยวผมลอง PHPMailer ดูนะครับผม ได้ผลอย่างไรจะมาบอกครับผม ขอบคุณมากนะครับผม ปล.ตอนเย็น ๆ ผมเทส ๆ อัพโหลดขึ้นไปก็ส่งได้ปกติ สักพักอ้าวส่งไม่ได้ Error เลยครับ T T
หัวข้อ: Re: form ส่งเมล์ไม่ไปเลยครับ แต่อัพบาง Host กลับส่งได้ปกติ
เริ่มหัวข้อโดย: kengzz ที่ 16 มิถุนายน 2016, 09:07:48
ตอนนี้ทำได้แล้วนะครับผม ขอบคุณมากครับ ใช้ PHP Mailer ตามที่บอกครับผม : )
ใช้ Code ดังนี้ครับ <?php
$name = $_REQUEST['name'] ; $email = $_REQUEST['email'] ; $phone = $_REQUEST['phone'] ; $opp = $_REQUEST['opp'] ; $income = $_REQUEST['income'] ;
require_once 'PHPMailerAutoload.php';
$results_messages = array();
$mail = new PHPMailer(true); $mail->CharSet = 'utf-8';
class phpmailerAppException extends phpmailerException {}
try { $to = '[email protected]'; if(!PHPMailer::validateAddress($to)) { throw new phpmailerAppException("Email address " . $to . " is invalid -- aborting!"); } $mail->isSMTP(); $mail->SMTPDebug = 1; $mail->Host = "mail.host.com"; $mail->Port = "25"; $mail->SMTPSecure = "none"; $mail->SMTPAuth = true; $mail->Username = "[email protected]"; $mail->Password = "password"; $mail->addReplyTo("[email protected]", "Best-Be"); $mail->From = "[email protected]"; $mail->FromName = "Best-Be"; $mail->addAddress("[email protected]", "Keng"); $mail->IsHTML(true); $mail->Subject = strip_tags($_POST['name']. ", ชื่อเรื่อง "); $body = <<<'EOT'
EOT; $subject = strip_tags($_POST['name']. ", ชื่อเรื่อง"); $email = strip_tags($_POST['email']); $phone = strip_tags($_POST['phone']); $name = strip_tags($_POST['name']); $opp = strip_tags($_POST['opp']); $message = strip_tags($_POST['message']); $income = trim($_POST['income']); $result = array(); $mail->Body = " <html> <h2><b>ชื่อ : ".$name.",<br> อีเมล์ : ".$email.", <br>โทร : ".$phone.", <br>อาชีพ : ".$opp.",<br>รายได้ต่อเดือน : ".$income."</b></h2> </html>";
try { $mail->send(); $results_messages[] = "Message has been sent using SMTP"; header('Location: thankyou.html'); } catch (phpmailerException $e) { throw new phpmailerAppException('Unable to send to: ' . $to. ': '.$e->getMessage()); } } catch (phpmailerAppException $e) { $results_messages[] = $e->errorMessage(); }
if (count($results_messages) > 0) { echo " Run results
\n"; echo " \n"; foreach ($results_messages as $result) { echo " $result \n"; } echo " \n"; } ?>
หัวข้อ: Re: form ส่งเมล์ไม่ไปเลยครับ แต่อัพบาง Host กลับส่งได้ปกติ
เริ่มหัวข้อโดย: rapiz ที่ 16 มิถุนายน 2016, 12:43:10
ตอนนี้ทำได้แล้วนะครับผม ขอบคุณมากครับ ใช้ PHP Mailer ตามที่บอกครับผม : )
ใช้ Code ดังนี้ครับ <?php
$name = $_REQUEST['name'] ; $email = $_REQUEST['email'] ; $phone = $_REQUEST['phone'] ; $opp = $_REQUEST['opp'] ; $income = $_REQUEST['income'] ;
require_once 'PHPMailerAutoload.php';
$results_messages = array();
$mail = new PHPMailer(true); $mail->CharSet = 'utf-8';
class phpmailerAppException extends phpmailerException {}
try { $to = '[email protected]'; if(!PHPMailer::validateAddress($to)) { throw new phpmailerAppException("Email address " . $to . " is invalid -- aborting!"); } $mail->isSMTP(); $mail->SMTPDebug = 1; $mail->Host = "mail.host.com"; $mail->Port = "25"; $mail->SMTPSecure = "none"; $mail->SMTPAuth = true; $mail->Username = "[email protected]"; $mail->Password = "password"; $mail->addReplyTo("[email protected]", "Best-Be"); $mail->From = "[email protected]"; $mail->FromName = "Best-Be"; $mail->addAddress("[email protected]", "Keng"); $mail->IsHTML(true); $mail->Subject = strip_tags($_POST['name']. ", ชื่อเรื่อง "); $body = <<<'EOT'
EOT; $subject = strip_tags($_POST['name']. ", ชื่อเรื่อง"); $email = strip_tags($_POST['email']); $phone = strip_tags($_POST['phone']); $name = strip_tags($_POST['name']); $opp = strip_tags($_POST['opp']); $message = strip_tags($_POST['message']); $income = trim($_POST['income']); $result = array(); $mail->Body = " <html> <h2><b>ชื่อ : ".$name.",<br> อีเมล์ : ".$email.", <br>โทร : ".$phone.", <br>อาชีพ : ".$opp.",<br>รายได้ต่อเดือน : ".$income."</b></h2> </html>";
try { $mail->send(); $results_messages[] = "Message has been sent using SMTP"; header('Location: thankyou.html'); } catch (phpmailerException $e) { throw new phpmailerAppException('Unable to send to: ' . $to. ': '.$e->getMessage()); } } catch (phpmailerAppException $e) { $results_messages[] = $e->errorMessage(); }
if (count($results_messages) > 0) { echo " Run results
\n"; echo " \n"; foreach ($results_messages as $result) { echo " $result \n"; } echo " \n"; } ?> :wanwan011:
|