SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A simple mail related question

    Hi

    I am having a script that sends mail. Just wondering, is it possible to check whether the mail has been send or not ? I mean, to make something like:
    PHP Code:
    if ($mail_send) {
    // do this
    } else {
    // do that

    Thanks for any help
    Cheers

  2. #2
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, do it just like that.
    if(mail(info)) {
    echo 'the email was sent';
    }

  3. #3
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where if(mail(info)) is constant irrespective of whatever happens in the script, right ? Or they are variables ???

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Williamsport, PA
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kigoobe
    Where if(mail(info)) is constant irrespective of whatever happens in the script, right ? Or they are variables ???
    I'm not quite sure what you mean by that, I think kigoobe was just writing mail(info) as shorthand for whatever mail( ) command you are using. It might be something like mail($user_email_address, $subject, $body)...

    Anyway, the mail( ) function will return a boolean value, but it is important to remember what the return type represents. According to the PHP documentation,

    Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

    It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
    Basically if the SMTP server accepts the request and queues up your email for delivery, mail( ) will return true. However there is no way to easily determine that the mail was actually recieved. To do that you'd need to request a receipt from the user, and that's a whole other can of worms... This is just the nature of the SMTP protocol at work.

  5. #5
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    that's clearer now. Well, it means, that can I do something like this
    PHP Code:
    if (!mail($whatever_is_there)) {
    header("Location: send_mail.php");

    given that send_mail.php is the name of the same file, ie, the file that sends mail. Just to be sure that the mail is going ... what do u think ?

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Williamsport, PA
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kigoobe
    Hi,

    that's clearer now. Well, it means, that can I do something like this
    PHP Code:
    if (!mail($whatever_is_there)) {
    header("Location: send_mail.php");

    given that send_mail.php is the name of the same file, ie, the file that sends mail. Just to be sure that the mail is going ... what do u think ?
    So what you are getting at with the above code is that, if the script fails to send the email on the first attempt, it will redirect the user back to the mail script to try it again? Yes I suppose you could do this... but make sure to tell the user that the email was not sent, so they know that there was a problem before trying it again.

  7. #7
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I meant, that it will try again, automatically, if the first mail attempt fails, without letting anything know to the users ... the idea is, if there is one page contact.php where I'm having a form, and post.php that sends the mail, I will execute post.php n number of times till the mail is send ...

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Williamsport, PA
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you're saying now, and that would work. May I suggest a slightly different solution though? Instead of sending a redirect header which will reroute the user's browser back to the send_mail.php script (possibly requiring a second or so for each request), you may want to try a loop instead.

    Code:
    $tries = 0;
    $max_tries = 5;
    
    do {
    
        // if the mail is sent, break out of the loop
        if(mail($whatever_is_there)) break;
    
        // The mail failed to send, so increment the counter
        $tries++;
    } while($tries < $max_tries);

  9. #9
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Yeah, a loop seems to be a better solution. Much better indeed. However, the script may be something like this, isn't it ?
    PHP Code:
    $tries 0;
    $max_tries 5;

    do {

        
    // if the mail is sent, break out of the loop
        
    if(mail($whatever_is_there)) break;

        
    // The mail failed to send, so increment the counter
       
    mail($whatever_is_there); // adding the action here ... 
        
    $tries++;

    Thanks for all ur help ...
    Happy Christmass !!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •