Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    mail() as a variable?

    I want to do this:

    PHP Code:
    <?php

    $function_a 
    mail('me@mydomain.com''Test Subject''Test message.');

    if (
    $function_a) { echo "Success!"; } else { echo "Failure!"; }

    ?>
    When I run this, the mail is sent in the definition and not the if statement. Is there a way I can prevent the mail from being sent until the "if"? Thanks ...

  • #2
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    try this-

    PHP Code:
    <?php

    if ($function_a mail('me@mydomain.com''Test Subject''Test message.')) { echo "Success!"; } else { echo "Failure!"; }

    ?>
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Not sure what you're trying to do.

    If you're trying to determine if the function exists:
    if (function_exists('mail'))

    IF you're trying to whether mail returned true, then as nomanic says.
    Last edited by tangoforce; 01-08-2012 at 08:37 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for the replies.

    nomanic, that works - I've used it before. But I'm trying to list a series of functions and submit them at the end with the if statement.

    tangoforce, I'm trying to list multiple mail functions by variables and make sure they (all) were successful by the if statement at the end.

    Thanks ...

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    you only need to test the return values in the if condition
    PHP Code:
    $r1 mail(...);
    $r2 mail(...);

    if (
    $r1 && $r2)
    {
        
    // all success

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Dormilich, thanks.

    When I run that, it sends the mail from the definition and not the if (I think, because if the if is deleted, the mails are still sent).

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The if doesn't check if the mails are sent or not, it only checks if it was queued to the mail program. The only time it will ever return false is if there is an issue contacting the smtp.

    Not sure exactly what you are looking for either. As soon as mail() is called as a function it executes at this point. You capture the results into the variable (optional) and use an if to see if it was queued. If you are looking to capture the signature definition, you can do it either as a callable type:
    PHP Code:
    $fpFunc 'mail';
    $fpFunc('to@to.com''subject''body', ...);
    // or
    call_use_func($fpFunc'to''subject''body', ...);
    // can be checked:
    if (function_exists($fpFunc))
    {
        
    $fpFunc('to''subject''body');

    And the other option is via ReflectionFunction and calling the ->invoke method.

    If its just a matter of checking the results for each, then you can use an array to capture the results. The only thing to note is that this result does not indicate if a user received an email, as that cannot be detected in PHP.

  • #8
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks, Fou-Lu. That's interesting.

    I guess what I want isn't really possible. I have a cron setup to run a php script, which generates contents from a database and sends an email. I wanted it to send me an email if the cron failed somehow, but I guess I'll just make a mail function at the end so I know if it gets stuck ...


  •  

    Posting Permissions

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