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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    function mail() not working

    ello,

    rather nasty problem,
    a website moved to another host, and there the mail() function just won't work. sysop said it should work, and even when forcing the smtp host and port it fails

    the sysop pointed out to me to use HELO. So I tried this function i found in the comments on mail() in the php manual

    PHP Code:
    Hope the links might be useful to some people :)
    josephcmiller2 at gmail dot com
    26
    -May-2006 04:26
    I have been using the 
    function mymail() provided by Antony Male (belowto send mail on my serverbut with a couple of modifications.  FirstI needed to enable AUTH LOGIN in order to use my smtp server from my ISP.  Secondhis mymail() function allows for a $from to be usedbut the function does not accept any such parameter.  I have addressed the latter issue by parsing the headers to obtain the Fromaddress.  If this is not providedfailed mail will not be returned properly to the sender.

    // modified to provide authenticated logins
    function mymail($to,$subject,$message,$headers)
    {

      
    // set as global variable
      
    global $GLOBAL;
     
      
    // get From address
      
    if ( preg_match("/From:.*?[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+.*/"$headers$froms) ) {
         
    preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+/"$froms[0], $fromarr);
         
    $from $fromarr[0];
      }

      
    // Open an SMTP connection
      
    $cp fsockopen ($GLOBAL["SMTP_SERVER"], $GLOBAL["SMTP_PORT"], &$errno, &$errstr1);
      if (!
    $cp)
       return 
    "Failed to even make a connection";
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "220") return "Failed to connect";

      
    // Say hello...
      
    fputs($cp"HELO ".$GLOBAL["SMTP_SERVER"]."\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "250") return "Failed to Introduce";
     
      
    // perform authentication
      
    fputs($cp"auth login\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "334") return "Failed to Initiate Authentication";
     
      
    fputs($cpbase64_encode($GLOBAL["SMTP_USERNAME"])."\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "334") return "Failed to Provide Username for Authentication";
     
      
    fputs($cpbase64_encode($GLOBAL["SMTP_PASSWORD"])."\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "235") return "Failed to Authenticate";

      
    // Mail from...
      
    fputs($cp"MAIL FROM: <$from>\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "250") return "MAIL FROM failed";

      
    // Rcpt to...
      
    fputs($cp"RCPT TO: <$to>\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "250") return "RCPT TO failed";

      
    // Data...
      
    fputs($cp"DATA\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "354") return "DATA failed";

      
    // Send To:, From:, Subject:, other headers, blank line, message, and finish
      // with a period on its own line (for end of message)
      
    fputs($cp"To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "250") return "Message Body Failed";

      
    // ...And time to quit...
      
    fputs($cp,"QUIT\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "221") return "QUIT failed";

      return 
    true;

    but that wont work either.
    does anyone has an idea how to fix?

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    What do you mean "doesn't work"? Comes up with errors, doesn't send an email, mail function not enabled, kermit the frog doesn't dance?

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Kermit never could dance... now Miss Piggy ... thats another matter

    don't mess around with talking directly to mailservers, net time you move you may well have to do this all over again for a different type of mailserver (been there burnt the t-shirt)

    try phpmailer or similar class , it lets you use your local sendmail or any SMTP server that will relay for you , it does what you are trying to do but without the headaches.

    http://phpmailer.sourceforge.net/
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire View Post
    What do you mean "doesn't work"? Comes up with errors, doesn't send an email, mail function not enabled, kermit the frog doesn't dance?
    mail() only gives back a boolean. so it returns FALSE. doh

    Quote Originally Posted by firepages View Post
    Kermit never could dance... now Miss Piggy ... thats another matter

    don't mess around with talking directly to mailservers, net time you move you may well have to do this all over again for a different type of mailserver (been there burnt the t-shirt)

    try phpmailer or similar class , it lets you use your local sendmail or any SMTP server that will relay for you , it does what you are trying to do but without the headaches.

    http://phpmailer.sourceforge.net/
    thnx for the tip m8. Never had any problem like this before. I'll look into the phpmailer class. And well, wouldn't have much headaches from it by the way. I included the function above als function mail2(), so easily replaced with the 'normal' function when moving

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Maybe you put this on the very top of your document:

    ini_set("error_reporting","E_ALL");

    And then tell us what errors you get.
    PHP 5 & MySQL 5 (Y)


  •  

    Posting Permissions

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