0

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.

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. 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    = '[email protected]';
   $erMail    = '[email protected]';
   $nAfzender = 'RASolutions';
   $afMail    = '**********@.nl';
   $baMail    = '[email protected]'; 
   $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:

             <?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>

2 Answers 2

0
$.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. 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) ?

3
  • I'm sorry but I don't know what you mean by: "Replace this value by the real path"? Which value does belong there? Because I thought PHP and JS didn't need to connect with each other. Commented Nov 30, 2013 at 18:39
  • You need to post your datas somewhere, here your action field in the form is empty. So specify the real path/file where you want to send your form. Check examples here: api.jquery.com/jQuery.post Commented Nov 30, 2013 at 18:46
  • Thanks, after some hours of trying to fix it, I've decided to use just PHP. But I've used your link to fix other JS forms on my website and that worked :-) Commented Nov 30, 2013 at 23:46
0
 $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.

Then, if you try the php side alone, is it working?

Like if you run it like this:

<?php
$_POST['author'] = 'asdasdas';
$_POST['email'] = '[email protected]';
$_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    = '[email protected]';
        $erMail    = '[email protected]';
        $nAfzender = 'RASolutions';
        $afMail    = '**********@.nl';
        $baMail    = '[email protected]';
        $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;
    }

}
1
  • If I run it PHP side only it will work, but with JS side of it, it won't. Thank you for the help, but how does it work with JS? Commented Nov 30, 2013 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.