1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

"if mail()" function in php, returns always true

Discussion in 'PHP' started by revathyelan, Jul 12, 2009.

  1. #1
    Hi, I want Email validation code in PHP. For this, I used "if mail()" function in php.
    But my code always returns true if I give the mail id like this,
    a@a.com.

    I need if the mail sent successfully without failure notice then only it will do the true part, otherwise it goes to false part. But my code do the true part and sent failure notice to the "From address". Can anyone know solution for this, please help me. Its very urgent... :confused:

    Thanks in advance,
    Revathy.
     
    revathyelan, Jul 12, 2009 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    That is impossible. You can check the host name, but even then, the email address can still be invalid. The only way to solve this is to have email confirmation.
     
    ThePHPMaster, Jul 12, 2009 IP
  3. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    theres a mail function in php?
    FANTASTIC. really. I did not no this. thanks!
     
    Dirty-Rockstar, Jul 12, 2009 IP
  4. revathyelan

    revathyelan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you for your kind reply Master, Sorry, i can't understand your post.

    What my exact need is, "Is there any possibilities to check fake mail id's are given?"
    (Mail id's like this,
    sgdjsgd@asgd.com
    gd@djg.sfdhsld)

    If i give mail id revathyelan@yahoo.com then it should send a mail to that ID and download a file which was I uploaded already in my site. If i give mail id like this, asdjga@ashdg.cgaksd, then it show an error msg "Invalid mail id". For this I used the php code

    $user =$Email;
    $sub = "Hai";
    $content = "You are registered in our site";

    if(mail($user, $sub, $content))
    {
    header('.../files/'.$filename);
    }
    else
    {
    echo "Invalid Mail ID";
    }

    But it always download my file. If I give revathyelan@yahoo.com then it sent an appropriate mail for me. No problem in that. But if tried by using "asfdgha@asdj.casydgui",
    something like this, then also it downloads my file. I want to avoid the second type of user to download my file.


    -Revathy.
     
    revathyelan, Jul 13, 2009 IP
  5. KRISHNA KUMAR

    KRISHNA KUMAR Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can do this but you should first verify email id with php when email is valid then it show download. By implementing this your can safe you to mail failure notice.
     
    KRISHNA KUMAR, Jul 13, 2009 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    you can never be 100% certain - unless you implement some iron clad rules. the idea is, to stop doing anything when the email is invalid, the domain does not exist or there are no email servers associated with the domain. yet, it needs to allow for all accepted email types that can exist.

    you can do it like this (I've left comments throughout, hope this helps) - i wrote this to use on on our production sites and also for vetting email addresses when cleaning up email lists:

    <?PHP
    
    $emails = Array(
        '"Abc\@def"@google.com', // valid as per RFC
        '"Fred Bloggs"@google.com', // valid as per RFC
        '"Joe\\Blow"@google.com', // valid as per RFC
        '"Abc@def"@digitalpoint.com', // valid as per RFC
        'customer/department=shipping@shopping.com', // valid as per RFC
        '$A12345@google.com', // valid as per RFC
        '!def!xyz%abc@yahoo.com', // valid as per RFC
        '_somename@gmail.com', // valid as per RFC
        'asdasd @sdasdasd', // invalid
        'foo@barbarbar.com', // valid domain (some1 bought it but it's parked and has no mail)
        'foo@barbarbarasdasd.com', // this is a domain that does not exist at all
    );
    
    
    function checkEmail($email) {
        // checks if an email is composed correctly, if its domain exists and if it has valid MX records.
    
        // the following regex guarantees RFC compatibility with the largest and yet compliant / strict form of an email address.
        // check:
        //  - http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
        //  - http://fragged.org/dev/email_test.php - using this in javascript
        $validEmailRegex = '/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/';
    
        // default is valid.
        $check = true;
    
        if (!preg_match($validEmailRegex, $email)) {
            // if not composed correctly, has spaces etc this will trigger first.
            $check = "Sorry, this is not a valid email";
        }
        else {
            // composed ok, now get the domain:
            $parts = preg_split('/\@/', $email);
    
            if (count($parts) > 0) {
                // get domain
                $dom = array_pop($parts);
    
                // check if it has any dns records, it's a fast check
                if (checkdnsrr($dom, "ANY")) {
    
                    // if so, perform the much slower MX check (especially so if the domain looks valid but has no data)
                    getmxrr($dom, $mxhosts);
    
                    // need at least 1 MX priority host
                    if (count($mxhosts) < 1)
                        $check = "$dom has no valid MX";
    
                    // you can now do a socket connection to port 25 of the MX hosts to see if the servers are up
                    // but this is excessive.
                    // you can also try rcpt-to:<user@host> to see if it recognises the user@ bit... 
                }
                else {
                    $check = "$dom has no DNS records at all";
                }
    
            }
            else {
                // should not really come here if regex above works.
                $check = "Invalid / unknown domain.";
            }
        }
    
        return $check;
    } // end checkEmail
    
    
    foreach($emails as $email) {
        $result = checkEmail($email);
        echo ($result === true) ? "$email is valid<br />" : "$email failed: $result<br />";
    }
    
    ?>
    PHP:
    this will output:
    http://fragged.org/dev/emailPHPtest.php
     
    dimitar christoff, Jul 13, 2009 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    if your objective is to only give a download link once they leave an address, then the checks can be circumvented. some1 can just give you or etc etc to infinity, always valid. you really should think about creating a system whereby they need to login to their email and get the download link from there instead, this way you'd ensure genuine users will always leave a working email.
     
    dimitar christoff, Jul 13, 2009 IP
  8. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I am not sure I understood your question.. forgive me if it is wrong.

    The mail function is not for validating. You will have to validate it using javascript or using PHP's regular expression matching (preg_match).

    The mail() function just send the mail with the attributes to, from, and subject. By default it will be using the sendmail installed in linux.
     
    HivelocityDD, Jul 13, 2009 IP