Mail and cookie are not working together?

Please read my post before making it duplicate?

Yesterday one of the developer helped me to solve cookie issue but after adding that code my form not sending any mail to admin and form filler. The problem i was facing yesterday that i was not able to access cookie immediately.

Read More

Previous code :

<?php if(isset($_COOKIE['username']) || isset($_POST['username'])){ ?>
    <script type="text/javascript">
        jQuery(document).ready(function($){
            $('a.down').show().css("display", "block");
        });
    </script>
<?php } else {  include('login.php'); } ?>

My Current Code :

<?php 
if(!isset($_COOKIE['username'])){
    setcookie("username", $_POST['username'], time() + (86400 * 30), "/");
    $username=$_POST['username'];
}
?>

<?php if (isset($_COOKIE['username']) || isset($username)){ ?>
    <script type="text/javascript">
        jQuery(document).ready(function($){
            $('a.down').show().css("display", "block");
        });
    </script>
<?php } else { include('login.php'); ?>

Thanks for your time.

My cookie code is working now and i am able to submit form in one time but the problem is i am not getting mails.

here my login.php file code :

<?php if(!isset($_COOKIE['username'])){
        setcookie("username", $_POST['username'], time() + (86400 * 30), "/");
       $username=$_POST['username'];
       }
?>

<?php if (isset($_COOKIE['username']) || isset($username)){ ?>

    <script type="text/javascript">
        jQuery(document).ready(function($){
            $('a.down-resources').show().css("display", "block");
        });
    </script>

<?php } else { 

    if(isset($_POST['submit'])){  

    if(!empty($_POST['username']) && !empty($_POST['email']) && !empty($_POST['phone'])) {

    $username = $_POST['username'];  
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $company = $_POST['company'];

    if ($username == $_POST['username'] && $email == $_POST['email']){
        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
                echo "$email is not a valid email address";
            }

        $udomain = end(explode('@', $email));

        $domains = array("aol.com", "att.net", "comcast.net",
            "facebook.com", "gmail.com", "gmx.com", "googlemail.com", "google.com",
            "hotmail.com", "hotmail.co.uk", "mac.com", "me.com", "mail.com", "msn.com",
            "live.com", "sbcglobal.net", "verizon.net", "yahoo.com", "yahoo.co.uk",
            "email.com", "games.com", "gmx.net", "hush.com", "hushmail.com", "inbox.com",
            "lavabit.com", "love.com", "pobox.com", "rocketmail.com", "safe-mail.net",
            "wow.com", "ygm.com", "ymail.com", "zoho.com", "fastmail.fm", "bellsouth.net",
            "charter.net", "cox.net", "earthlink.net", "juno.com",
            "btinternet.com", "virginmedia.com", "blueyonder.co.uk", "freeserve.co.uk",
            "live.co.uk", "ntlworld.com", "o2.co.uk", "orange.net", "sky.com",
            "talktalk.co.uk", "tiscali.co.uk", "virgin.net", "wanadoo.co.uk", "bt.com",
            "sina.com", "qq.com", "naver.com", "hanmail.net", "daum.net", "nate.com",
            "yahoo.co.jp", "yahoo.co.kr", "yahoo.co.id", "yahoo.co.in", "yahoo.com.sg",
            "yahoo.com.ph", "hotmail.fr", "live.fr", "laposte.net", "yahoo.fr",
            "wanadoo.fr", "orange.fr", "gmx.fr", "sfr.fr", "neuf.fr", "free.fr",
            "gmx.de", "hotmail.de", "live.de", "online.de", "t-online.de", "web.de",
            "yahoo.de", "mail.ru", "rambler.ru", "yandex.ru", "hotmail.be", "live.be",
            "skynet.be", "voo.be", "tvcablenet.be", "hotmail.com.ar", "live.com.ar",
            "yahoo.com.ar", "fibertel.com.ar", "speedy.com.ar", "arnet.com.ar",
            "hotmail.com", "gmail.com", "yahoo.com.mx", "live.com.mx", "yahoo.com",
            "hotmail.es", "live.com", "hotmail.com.mx", "prodigy.net.mx", "msn.com");


        if (in_array($udomain, $domains)) {
            echo "Only use corporate email address";
        } else {
            define('WP_USE_THEMES', true);
            setcookie("username", $username, time() + (86400 * 30), "/");          
            /* send to 1st recipient */
            $to_1 = "example@yahoo.com";
            $from = "$email";
            $subject_1 = "Test";
            $message_1 = "Hello World!";

            $headers_1 = 'From: ' . $from . "rn";
            $headers_1 .= "MIME-Version: 1.0" . "rn";
            $headers_1 .= "Content-type:text/html;charset=utf-8" . "rn";

            mail($to_1, $subject_1, $message_1, $headers_1);

            /* send to 2nd recipient */
            $to_2 = "$email";
            $from_2 = "example@test.com";
            $subject_2 = "Thank you";
            $message_2 ="2nd Message to visitor";

            $headers_2 = 'From: ' . $from_2 . "rn";
            $headers_2 .= "MIME-Version: 1.0" . "rn";
            $headers_2 .= "Content-type:text/html;charset=utf-8" . "rn";

            mail($to_2, $subject_2, $message_2, $headers_2);
            header("Location: ".$_SERVER['HTTP_REFERER']);
        }  

        } else { echo 'Some Error Occured'; }  
    }  else { echo '<strong>Please enter the require fields.</strong> <br /> <br />'; }
}
?>

<p>To access complete information, please fill-in the form below and click on submit button. <br /></p>

<form action="" method="POST">
        Name * :<input name="username" type="text" value="<?php if(isset($_POST['submit'])){ echo $username;}?>">
        Email * :<input name="email" type="email" value="<?php if(isset($_POST['submit'])){ echo $email;}?>">
        <input name="submit" type="submit" value="Submit"><br />
        Company :<input name="company" type="text" value="<?php if(isset($_POST['submit'])){ echo $company;}?>">
        Phone * :<input name="phone" type="text" value="<?php if(isset($_POST['submit'])){ echo $phone;}?>">
    </div>
</form>
<?php } ?>

Related posts