2015-06-29 29 views
0

請我需要幫助。我的聯繫表格不發送消息給我的Gmail。我也檢查了我的垃圾郵件,但沒有發現。不知道這是否與我的代碼有關。提前致謝。我意識到這個消息正在進入我的godaddy cpanel。主題是聯繫表格不發送郵件到Gmail

郵件失敗 - 畸形的收件人地址

郵件正文說

您發送包含在被錯誤地構建了收件人地址的消息:從 :丟失或格式錯誤的本地部分(期望單詞或「<」) 該郵件尚未傳送給任何收件人。

也請忽略$ip字段,我的部分代碼未添加。

<?php 
$emailErr = ""; 
$commentErr = ""; 
if(isset($_POST['submit'])){ 
    //declares variable 
    $email = $_POST['email']; 
    $comment = $_POST['comment']; 
    if(empty($_POST['email'])){ 
     $emailErr = "Please enter your email"; 
    } 
    if(empty($_POST['comment'])){ 
     $commentErr = "comment field can't be empty"; 
    } 
} 
if(!empty($_POST['email']) && !empty($_POST['comment'])){ 
    // Send the email 
    $to = "[email protected]"; 
    $email = "From: $email"; 
    $comment = "Message: $comment"; 
    $message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -=="; 
    $headers = "From: $email,"; 
    $send_contact = mail($to,$email,$comment,$message,$headers); 
    header("Location: index.php"); 
} 
?> 
+4

'$評論= $ _ POST [「評論'];'這不是一個單引號,這是一個聰明的MS報價。你把它們分散在你的代碼中。糾正它們全部,打開錯誤報告。 – chris85

+0

你真的在編寫代碼嗎? –

+0

從頭看看它錯了 –

回答

0

這應該有效。

<?php 
    $emailErr = ""; 
    $commentErr = ""; 
    if(isset($_POST['submit'])){ 

     //declares variable 
     $email = $_POST['email']; 
     $comment = $_POST['comment']; 

     if(empty($_POST['email'])){ 
      $emailErr = "Please enter your email"; 
     } 
     if(empty($_POST['comment'])){ 
      $commentErr = "comment field can't be empty"; 
     } 
    } 

    if(!empty($_POST['email']) && !empty($_POST['comment'])){ 
     // Send the email 
     $to = "[email protected]"; 
     $title = "Message from my website"; 

     $message = "Comment: {$comment}" . "\r\n"; 
     $message .= "Sent from the website with IP Address: {$ip}" . "\r\n"; 

     $headers = "From: " . strip_tags($email) . "\r\n"; 
     $headers .= "Reply-To: ". strip_tags($email) . "\r\n"; 
     $headers .= "MIME-Version: 1.0\r\n"; 
     $headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n"; 
     mail($to,$title,$message,$headers); 

     header("Location: index.php"); 
    } 
?> 

我建議你學習如何使用此郵件功能http://php.net/manual/en/function.mail.php

+1

謝謝大家的貢獻。 @ OlaitanMayowa-你給我的代碼完成了這項工作。非常感謝你。 – mindblee

0

Heyy早安,

如果您在離線模式下工作,也許你需要PHPMailer的。

首先,你需要從這裏https://github.com/PHPMailer/PHPMailer/archive/master.zip

下載PHPMailer的那麼您的文件夾中粘貼。如果我的編碼不明確,你可以檢查從

https://github.com/PHPMailer/PHPMailer

<?php 
require 'PHPMailerAutoload.php'; // Your Path 

$mail = new PHPMailer; 

//$mail->SMTPDebug = 3;        // 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 = '[email protected]';     // Your mail 
$mail->Password = 'secret';       // Your mail password 
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;  

$mail->From = '[email protected]'; 
$mail->FromName = 'Mailer'; 
$mail->addAddress('[email protected]', 'Joe User');  // Add a recipient 
$mail->addAddress('[email protected]');    // Name is optional 
$mail->addReplyTo('[email protected]', 'Information'); 
$mail->addCC('[email protected]'); 
$mail->addBCC('[email protected]'); 

$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name 
$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'; 

//Check Condition 
if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
} 

方式二。

如果您在在線模式下進行測試(擁有自己的域名和託管),您可以隨意複製並粘貼。

不需要phpmailer。

<?php 

$error = []; 

$receipientName="Fido"; 
$receipientEmail ="receipientmail.gmail.com"; 
$ccEmail =""; 

//declares variable 
if(isset($_POST['name'])) $name = $_POST['name']; 
else $name = ""; 

if(isset($_POST['email'])) $email = $_POST['email']; 
else $email = ""; 



function send_mail($myname, $myemail, $contactname, $contactemail, $subject, $message) { 


    $headers = "MIME-Version: 1.0\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
    $headers .= "X-Priority: 1\n"; 
    $headers .= "X-MSMail-Priority: High\n"; 
    $headers .= "X-Mailer: php\n"; 
    $headers .= "From: \"".$myname."\" <".$myemail.">\r\n"; 
    return(mail("\"".$contactname."\" <".$contactemail.">", $subject, $message, $headers)); 
} 

if(isset($Submit) && $Submit=="Go") { 

    $emailContent =''; 


    $sent=send_mail($name, "yourmailname.gmail.com", "Fido", $receipientEmail, "Testing", $emailContent); 
    if($sent) { 
     echo $emailContent; 

     header('Location: contact.php'); 
    }else{ 
     echo "Failed"; 
     exit; 
    } 

} 


?>