2016-03-01 104 views
-6

我得到了正確的發送郵件,除了裏面有他們沒有數據的簡單的電子郵件的形式發送。它也發送兩次電子郵件。這可能是什麼原因?電子郵件沒有數據

contact.php:

<form class="contact-form columns_padding_5" method="post" action="mail/mail-send.php"> 
     <div class="col-sm-6"> 
      <p class="contact-form-name"> 
       <label for="name">Naam<span class="required">*</span></label> 
       <input type="text" aria-required="true" size="30" value="" name="name" id="name" class="form-control" placeholder="Naam"> 
      </p> 
     </div> 
     <div class="col-sm-6"> 
      <p class="contact-form-subject"> 
       <label for="subject">Onderwerp<span class="required">*</span></label> 
       <input type="text" aria-required="true" size="30" value="" name="subject" id="subject" class="form-control" placeholder="Onderwerp"> 
      </p> 
     </div> 
     <div class="col-sm-6"> 
      <p class="contact-form-phone"> 
       <label for="email">Email-adres<span class="required">*</span></label> 
       <input type="email" aria-required="true" size="30" value="" name="email" id="email" class="form-control" placeholder="Email-adres"> 
      </p> 
     </div> 
     <div class="col-sm-6"> 
      <p class="contact-form-email"> 
       <label for="phone">Telefoonnummer<span class="required">*</span></label> 
       <input type="text" aria-required="true" size="30" value="" name="phone" id="phone" class="form-control" placeholder="Telefoonnummer"> 
      </p> 
     </div> 
     <div class="col-sm-12"> 

      <p class="contact-form-message"> 
       <label for="message">Bericht</label> 
       <textarea aria-required="true" rows="6" cols="45" name="message" id="message" class="form-control" placeholder="Bericht"></textarea> 
      </p> 
     </div> 

     <div class="col-sm-12"> 

      <p class="contact-form-submit topmargin_30"> 
       <button type="submit" id="contact_form_submit" name="contact_submit" class="theme_button wide_button color1">Verstuur bericht</button> 
       <button type="reset" id="contact_form_reset" name="contact_reset" class="theme_button wide_button">Leeg formulier</button> 
      </p> 
     </div> 


</form> 

郵件send.php:

<?PHP 
require_once("../phpMailer/class.phpmailer.php"); 
$isValid = true; 
if(isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['email']) && isset($_POST['message'])) 
{ 
    $name = $_POST['name']; 
    $subject = $_POST['subject']; 
    $email = $_POST['email']; 
    $message = $_POST['message']; 
    $phone = $_POST['phone']; 
    $mail = new PHPMailer; 
    $mail->From = $email; 
    $mail->FromName = $name; 
    $mail->addAddress("[email protected]"); // Add a recipient 
    $mail->isHTML(true);      // Set email format to HTML 
    $mail->Subject = $subject; 
    $texts = 'Er is een aanvraag op de website van website<br /> <br /> 
    <b>Naam:</b> '.$name.'<br /> 
    <b>E-mail adres:</b> '.$email.'<br /> 
    <b>Onderwerp:</b> '.$subject.'<br /> 
    <b>Telefoonnummer:</b>'.$phone.'<br /> 
    <b>Vragen/Opmerkingen:</b> '.$message.'<br /><br /><br /> 
    '; 

    $handtekening = ' 
    <table border="0" width="100%" cellspacing="0" cellpadding="0" style="font-family:calibri;color: #5C5C5C; font-size:10pt;line-height:22px;"> 
    <tr> 
    <td width="160" valign="top" style="font-family:calibri;padding-left:10px;padding-top:20px;"> 
    [contents] 
    </td> 
    </tr> 
    <tr> 
    <td width="160" valign="top" style="font-family:calibri;padding-left:10px;padding-top:20px;"> 
    <br><br>Met vriendelijke groet,<br><br> 
    Helpdesk<br> 
    <b>website</b><br> 
    <p></p> 
    </td> 
    </tr> 
    </table> 
    <table height="120" border="0" width="100%" cellspacing="0" cellpadding="0" style="font-family:calibri;color: #5C5C5C; font-size:10pt;line-height:22px;"> 
    <tr> 
    <td width="250" valign="top" style="font-family:calibri;padding-left:10px;padding-top:20px;border-top: 1px #000000 dotted; border-bottom: 1px #000000 dotted;"> 
    E:&nbsp;&nbsp; 
    <a href="mailto:[email protected]" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;">[email protected]</a><br> 
    T:&nbsp;&nbsp; 
    <a href="tel:0615086609" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;">0615086609</a><br> 
    W:&nbsp;&nbsp; 
    <a href="http://website.nl" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;" target="_blank">www.website.nl</a><br> 
    </td> 
    <td align="right" style="font-family:calibri;padding-right:10px;padding-top:5px;border-top: 1px #000000 dotted; border-bottom: 1px #000000 dotted;"> 
    <a href="http://website.nl/" target="_blank" title="Ga naar de website"> 
    <img src="http://website.nl/_extern/website/images/logo/logo.png" alt="Ga naar de website" style="font-family:calibri;text-align:right;margin:0px;padding:10px 0 10px 0;" border="0" width="232"> 
    </a> 
    </td> 
    </tr> 
    <tr> 
    <td colspan="2" style="font-family:calibri;color:#a3a3a3;font-size:11px;margin-top:6px;line-height:14px;"> 
    <br>Dit e-mailbericht is uitsluitend bestemd voor de geadresseerde. Als dit bericht niet voor u bestemd is, wordt u vriendelijk verzocht dit aan de afzender te melden. website staat door de elektronische verzending van dit bericht niet in voor de juiste en volledige overbrenging van de inhoud, noch voor tijdige ontvangst daarvan. Voor informatie over website raadpleegt u <a href="http://website.nl" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;" target="_BLANK">website</a>.<br><br> 
    </td> 
    </tr> 
    </table>'; 

    $contents = preg_replace('/\[contents]/',$texts, $handtekening); 
    $mail->msgHTML($contents); 
    $mail->AltBody = $texts; 
    if(!$mail->send()) 
    { 
     $isValid = false; 
    } 

    $mail = new PHPMailer; 
    $mail->From = '[email protected]'; 
    $mail->FromName = 'website'; 
    $mail->addAddress($email);  // Add a recipient 
    $mail->isHTML(true);   // Set email format to HTML 
    $mail->Subject = 'Bedankt voor uw aanvraag bij website'; 
    $texts = 'Geachte heer/mevrouw '.$naam.',<br /><br /> 
    Hartelijk dank voor uw aanvraag bij website<br /> 
    Wij reageren zo spoedig mogelijk op uw aanvraag.<br /><br /> 
    Uw gegevens worden nooit aan derden ter hand gesteld. 
    '; 
    $contents = preg_replace('/\[contents]/',$texts, $handtekening); 
    $mail->msgHTML($contents); 
    $mail->AltBody = $texts; 
    if(!$mail->send()) 
     $isValid = false; 
    } 
    if($isValid == true) { 
     $result["submit_message"] = _msg_send_ok; 
    } else { 
     $result["submit_message"] = _msg_send_error; 
    } 
     if($_POST["name"]=="" || $_POST["name"]==_def_name) 
      $result["error_name"] = _msg_invalid_data_name; 
     if($_POST["email"]=="" || $_POST["email"]==_def_email || !preg_match("#^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$#", $_POST["email"])) 
      $result["error_email"] = _msg_invalid_data_email; 
     if($_POST["message"]=="" || $_POST["message"]==_def_message) 
      $result["error_message"] = _msg_invalid_data_message; 

    $result['isValid'] = $isValid; 

    echo json_encode($result); 

這是處理的聯繫方式阿賈克斯部分:

//function that initiating template plugins on document.ready event 
function documentReadyInit() { 
    /////////// 
    //Plugins// 
    /////////// 
    //contact form processing 
    jQuery('form.contact-form').on('submit', function(e){ 
     e.preventDefault(); 
     var $form = jQuery(this); 
     jQuery($form).find('span.contact-form-respond').remove(); 

     //checking on empty values 
     jQuery($form).find('[aria-required="true"], [required]').each(function(index) { 
      if (!jQuery(this).val().length) { 
       jQuery(this).addClass('invalid').on('focus', function(){jQuery(this).removeClass('invalid')}); 
      } 
     }); 
     //if one of form fields is empty - exit 
     if ($form.find('[aria-required="true"], [required]').hasClass('invalid')) { 
      return false; 
     } 


     //sending form data to PHP server if fields are not empty 
     var request = $form.serialize(); 
     var ajax = jQuery.post("mail/mail-send.php", request) 
      .done(function(data) { 
       jQuery($form).find('[type="submit"]').attr('disabled', false).parent().append('<span class="contact-form-respond highlight">'+data+'</span>'); 
      }) 
      .fail(function(data) { 
       jQuery($form).find('[type="submit"]').attr('disabled', false).parent().append('<span class="contact-form-respond highlight">Mail cannot be sent. You need PHP server to send mail.</span>'); 
      }) 
    }); 

最後我的配置文件,其中一些數據設置,如成功/失敗消息:

的config.php:

<?php 
define('_from_name', 'website'); 
define('_from_email', '[email protected]'); 

define('_to_name', ''); 
define('_to_email', '[email protected]'); 

define('_smtp_host', ''); 
define('_smtp_username', ''); 
define('_smtp_password', ''); 
define('_smtp_port', ''); 
define('_smtp_secure', ''); //ssl or tls 

define('_subject_email', 'website: Contactaanvraag'); 

define('_def_name', 'Uw Naam *'); 
define('_def_email', 'Uw Emailadres *'); 
define('_def_phone', 'Uw Telefoonnummer'); 
define('_def_message', 'Bericht *'); 
define('_def_message_appointment', 'Aanvullende informatie'); 

define('_msg_invalid_data_name', 'Voer een naam in.'); 
define('_msg_invalid_data_email', 'Voer een geldig emailadres in.'); 
define('_msg_invalid_data_message', 'Voer een bericht in.'); 


define('_msg_send_ok', 'Bedankt voor uw bericht!'); 
define('_msg_send_error', 'Dit bericht kan niet worden verzonden.'); 
?> 

正確的做法應該是:

一個確認郵件到郵件客戶填寫,哪些客戶寫信給該公司一個郵件。

現在我收到兩封郵件(在他們沒有數據),爲公司和客戶收到什麼。

在這裏,您可以看到郵件被髮送正確: enter image description here

而且這是兩個成功的消息,我得到(在JSON,我不知道如何解碼)

enter image description here

+3

請看看如何創建[MCVE。 _Noone_將會經歷那麼多的代碼,以便發現可能的微小錯誤。 –

回答

0
if(!$mail->send()) 
{ 
    $isValid = false; 
} 

$mail = new PHPMailer; 

$mail->From = '[email protected]'; 
$mail->FromName = 'website'; 
$mail->addAddress($email);  // Add a recipient 
$mail->isHTML(true);   // Set email format to HTML 
$mail->Subject = 'Bedankt voor uw aanvraag bij website'; 

$texts = 'Geachte heer/mevrouw '.$naam.',<br /><br /> 
Hartelijk dank voor uw aanvraag bij website<br /> 
Wij reageren zo spoedig mogelijk op uw aanvraag.<br /><br /> 
Uw gegevens worden nooit aan derden ter hand gesteld. 
'; 

$contents = preg_replace('/\[contents]/',$texts, $handtekening); 

$mail->msgHTML($contents); 
$mail->AltBody = $texts; 

if(!$mail->send()) 
{ 
    $isValid = false; 
} 

您正在測試if(!$mail->send())兩次。

+1

我以爲這是因爲他發送2封不同的電子郵件 – BugFinder