简体   繁体   English

用Javascript发送PHP邮件

[英]PHP mail sending with Javascript

I'm having a problem with sending a mail from my site to my own Outlook.com account, the problem is that if some one is using my contact form it doesn't send the mail to me. 我将网站上的邮件发送到我自己的Outlook.com帐户时遇到问题,问题是如果有人使用我的联系表,则不会将邮件发送给我。

Normally everything in PHP with the mail process would work but this time I wonted to use JavaScript with it so my form does everything automatically. 通常,PHP中用于邮件处理的所有内容都可以使用,但是这次我不想将JavaScript与它一起使用,因此我的表单可以自动执行所有操作。 I'm not getting an error, I'm getting the message that the e-mail was send. 我没有收到错误,我收到了电子邮件已发送的消息。

    //Contact script.
if(isset($_POST['submit']))
{
    $sName     = trim($_POST['author']);
    $sEmail    = trim($_POST['email']);
    $sMessage  = trim($_POST['comment']);

    if(empty($sName))
    {
        $nameError = 'U bent uw naam vergeten';
        $hasError  = true;
    }

    if(empty($sEmail))
    {
        $emailError = 'U bent uw email adres vergeten';
        $hasError   = true;
    }
    elseif(!filter_var($sEmail, FILTER_VALIDATE_EMAIL))
    {
        $emailError = 'Uw mail adres is niet geldig!';
        $hasError   = true;
    }

    if(empty($sMessage))
    {
        $commentError = 'U hebt geen bericht opgegeven';
        $hasError     = true;
    }

 //Mail versturen.
 if(!isset($hasError))
 {
   $weNaam    = 'RASolutions';
   $eiMail    = 'info@rasolutions.nl';
   $erMail    = 'info@rasolutions.nl';
   $nAfzender = 'RASolutions';
   $afMail    = '**********@.nl';
   $baMail    = 'info@seobeheer.nl'; 
   $aHtml     = true;

        // De headers samenstellen
        $headers     = 'From: <' . $weNaam . '> '. PHP_EOL ;
        $headers    .= 'Reply-To: <' . $nAfzender . '> <' . $eiMail . '>' . PHP_EOL; 
        $headers    .= ($baMail != '') ? 'Bcc: <' . $baMail . PHP_EOL : '';
        $headers    .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
        $headers    .= 'X-Priority: Normal' . PHP_EOL;
        $headers    .= ($aHtml) ? 'MIME-Version: 1.0' . PHP_EOL : '';
        $headers    .= ($aHtml) ? 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL : '';

        $sBericht = "
        Beste, <br />
        <br />
        er is gebruik gemaakt van het contact formulier op RASolutions.<br />
        Het mail adres dat hiervoor is gebruikt: <strong>".$sEmail."</strong>, de                naam van degene is <strong>".$sName."</strong>.<br />
        <br />
        <i>Onderstaande het bericht:</i>
        ".$sMessage."
        <br /> <br />

        Met vriendelijke groet, <br />
        RASolutions helpdesk.
        ";

                //Verzonden.
        $mail = mail($afMail, "Contact || RASolutions", $sBericht, $headers);
        $emailSent = true;
                }

                }

My HTML form and the JS part: 我的HTML表单和JS部分:

             <?php 
                if(isset($emailSent) && $emailSent == true) 
                { 
                ?>
                <div class="succes closable">Uw mail is succesvol verstuurd! U zal in 48 uur een antwoord krijgen.</div>
                <?php
                }
                if(isset($hasError) || isset($captchaError) ) 
                { 
                ?>
                <div class="warning closable">Er zijn wat problemen opgetreden.</div>
                <?php 
                } 
                ?>
                <form id="contact-us" action="" method="post">
                        <p>
                            <label for="author">Naam</label>
                            <input id="author" class="aqua_input" name="author" type="text"  placeholder=""/>
                        </p>
                        <p> 
                            <label for="email">Email</label> 
                            <input id="email" class="aqua_input" name="email" type="email"  placeholder=""/>
                        </p>
                        <p>     
                            <label for="comment">Bericht</label>
                            <textarea id="comment" rows="8" class="aqua_input" name="comment"  placeholder=""></textarea>
                        </p>
                        <p class="form-submit">
                            <input name="submit" type="submit" id="submit" value="Verstuur" class="sm_button">
                        </p>                        
                    </form>
    <script type="text/javascript">
    <!--//--><![CDATA[//><!--
    $(document).ready(function()
     {
        $('form#contact-us').submit(function() 
        {
            $('.warning closable').remove();
            var hasError = false;
            $('.aqua_input').each(function() 
            {
                if($.trim($(this).val()) == '') 
                {
                    var labelText = $(this).prev('label').text();
                    $(this).parent().append('<div class="warning closable">U bent uw '+labelText+' vergeten op te geven</div>');
                    $(this).addClass('inputError');
                    hasError = true;
                } 
                else if($(this).hasClass('email')) 
                {
                    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                    if(!emailReg.test($.trim($(this).val()))) 
                    {
                        var labelText = $(this).prev('label').text();
                        $(this).parent().append('<div class="warning closable"><strong>Helaas!</strong> Uw ingevoerde waardes zijn ongeldig: '+labelText+'</div>');
                        $(this).addClass('inputError');
                        hasError = true;
                    }
                }
            });
            if(!hasError)
             {
                var formInput = $(this).serialize();
                $.post($(this).attr('action'),formInput, function(data)
                {
                    $('form#contact-us').slideUp("fast", function() 
                    {                  
                        $(this).before('<div class="success closable"><strong>Bedankt!</strong> U zal in 48 uur een antwoord krijgen. </div>');
                    });
                });
            }

            return false;   
        });
    });
    //-->!]]>
</script>
 $mail = mail($afMail, "Contact || RASolutions", $sBericht, $headers);
 $emailSent = true;

Instead of doing that, check the $mail value, it's the result of the function, true or false. 不必执行此操作,而是检查$mail值,它是函数的结果,即true或false。

Then, if you try the php side alone, is it working? 然后,如果您仅尝试php方面,它是否有效?

Like if you run it like this: 如果像这样运行它:

<?php
$_POST['author'] = 'asdasdas';
$_POST['email'] = 'asdasd@free.fr';
$_POST['comment'] = 'asdasd';

if (1 || isset($_POST['submit'])) {
    $sName    = trim($_POST['author']);
    $sEmail   = trim($_POST['email']);
    $sMessage = trim($_POST['comment']);

    if (empty($sName)) {
        $nameError = 'U bent uw naam vergeten';
        $hasError  = true;
    }

    if (empty($sEmail)) {
        $emailError = 'U bent uw email adres vergeten';
        $hasError   = true;
    } elseif (!filter_var($sEmail, FILTER_VALIDATE_EMAIL)) {
        $emailError = 'Uw mail adres is niet geldig!';
        $hasError   = true;
    }

    if (empty($sMessage)) {
        $commentError = 'U hebt geen bericht opgegeven';
        $hasError     = true;
    }

    //Mail versturen.
    if (!isset($hasError)) {
        $weNaam    = 'RASolutions';
        $eiMail    = 'info@rasolutions.nl';
        $erMail    = 'info@rasolutions.nl';
        $nAfzender = 'RASolutions';
        $afMail    = '**********@.nl';
        $baMail    = 'info@seobeheer.nl';
        $aHtml     = true;

        // De headers samenstellen
        $headers = 'From: <' . $weNaam . '> ' . PHP_EOL;
        $headers .= 'Reply-To: <' . $nAfzender . '> <' . $eiMail . '>' . PHP_EOL;
        $headers .= ($baMail != '') ? 'Bcc: <' . $baMail . PHP_EOL : '';
        $headers .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
        $headers .= 'X-Priority: Normal' . PHP_EOL;
        $headers .= ($aHtml) ? 'MIME-Version: 1.0' . PHP_EOL : '';
        $headers .= ($aHtml) ? 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL : '';

        $sBericht = "
    Beste, <br />
    <br />
    er is gebruik gemaakt van het contact formulier op RASolutions.<br />
    Het mail adres dat hiervoor is gebruikt: <strong>" . $sEmail . "</strong>, de                naam van degene is <strong>" . $sName . "</strong>.<br />
    <br />
    <i>Onderstaande het bericht:</i>
    " . $sMessage . "
    <br /> <br />

    Met vriendelijke groet, <br />
    RASolutions helpdesk.
    ";

        //Verzonden.
        $mail      = mail($afMail, "Contact || RASolutions", $sBericht, $headers);
        var_dump($mail);
        $emailSent = true;
    }

}
$.post($(this).attr('action'),formInput, function(

I suppose $(this).attr('action') here is empty and the js don't know where to post the datas. 我想这里的$(this).attr('action')是空的,而js不知道在哪里发布数据。 Replace this value by the real path where you post the datas. 用发布数据的真实路径替换此值。 Also are you checking if you have any errors in the console of your browser (F12) ? 您是否还在检查浏览器(F12)的控制台中是否有任何错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM