电子邮件没有数据发送

时间:2016-03-01 13:52:17

标签: javascript php json ajax email

我有一个简单的电子邮件表单,可以正确发送邮件,但内部没有数据。它还会发送两次电子邮件。可能是什么原因造成的?

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>

mail-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@live.nl");   // 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:info@website.nl" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;">info@website.nl</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 = 'info@website.nl';
    $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);

这是处理联系表格的ajax部分:

//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', 'info@website.nl');

define('_to_name', '');
define('_to_email', 'website@live.nl');

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

1 个答案:

答案 0 :(得分:0)

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

$mail = new PHPMailer;

$mail->From = 'info@website.nl';
$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())两次。