发送的电子邮件没有数据

Posted

技术标签:

【中文标题】发送的电子邮件没有数据【英文标题】:Email is sent without data 【发布时间】:2016-06-13 23:31:14 【问题描述】:

我得到了一个简单的电子邮件表单,它可以正确发送邮件,但里面没有数据。它还会发送两次电子邮件。这可能是什么原因?

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>

邮件发送.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"  cellspacing="0" cellpadding="0" style="font-family:calibri;color: #5C5C5C; font-size:10pt;line-height:22px;">
    <tr>
    <td  valign="top" style="font-family:calibri;padding-left:10px;padding-top:20px;">
    [contents]
    </td>
    </tr>
    <tr>
    <td  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  border="0"  cellspacing="0" cellpadding="0" style="font-family:calibri;color: #5C5C5C; font-size:10pt;line-height:22px;">
    <tr>
    <td  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"  style="font-family:calibri;text-align:right;margin:0px;padding:10px 0 10px 0;" border="0" >
    </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.');
?>

正确的行为应该是:

一封确认邮件给客户填写的邮件,一封客户写给公司的邮件。

现在我收到公司的两封邮件(其中没有数据),而客户什么也没收到。

在这里您可以看到邮件已正确发送:

这是我得到的两条成功消息(在json中,我不知道如何解码)

【问题讨论】:

请查看如何创建minimal reproducible example。 没有人会为一个可能是微不足道的错误检查那么多代码。 【参考方案1】:
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-&gt;send()) 两次。

【讨论】:

我认为那是因为他发送了 2 封不同的电子邮件

以上是关于发送的电子邮件没有数据的主要内容,如果未能解决你的问题,请参考以下文章

从 iPad 应用程序发送电子邮件后,sqlite 数据库没有数据

有没有办法查看联系表格将其内容发送到哪个电子邮件地址

有没有一种安全的方式通过电子邮件以明文形式向用户发送密码?

电子邮件未发送。 ajax发送的值[重复]

kettle 有新插入数据则发送邮件提醒?

发送带有数据框的电子邮件作为附件