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 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    224
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem with alternate mail function using smtp

    My webhost has disabled mail() because of spam, and so they gave us this alternate function using SMTP authentication. It is as follows:
    PHP Code:
    <?php
    /* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */

    //Authenticate Send - 21st March 2005
    //This will send an email using auth smtp and output a log array
    //logArray - connection,

    function authSendEmail($from$namefrom$to$nameto$subject$message)
    {
       
    //SMTP + SERVER DETAILS
       /* * * * CONFIGURATION START * * * */
       
    $smtpServer "mail.server.com";
       
    $port "25";
       
    $timeout "30";
       
    $username "smtpusername";
       
    $password "smtppassword";
       
    $localhost "localhost";
       
    $newLine "\r\n";
       
    /* * * * CONFIGURATION END * * * * */
       
       //Connect to the host on the specified port
       
    $smtpConnect fsockopen($smtpServer$port$errno$errstr$timeout);
       
    $smtpResponse fgets($smtpConnect515);
       if(empty(
    $smtpConnect))
       {
           
    $output "Failed to connect: $smtpResponse";
           return 
    $output;
       }
       else
       {
           
    $logArray['connection'] = "Connected: $smtpResponse";
       }

       
    //Request Auth Login
       
    fputs($smtpConnect,"AUTH LOGIN" $newLine);
       
    $smtpResponse fgets($smtpConnect515);
       
    $logArray['authrequest'] = "$smtpResponse";
       
       
    //Send username
       
    fputs($smtpConnectbase64_encode($username) . $newLine);
       
    $smtpResponse fgets($smtpConnect515);
       
    $logArray['authusername'] = "$smtpResponse";
       
       
    //Send password
       
    fputs($smtpConnectbase64_encode($password) . $newLine);
       
    $smtpResponse fgets($smtpConnect515);
       
    $logArray['authpassword'] = "$smtpResponse";

       
    //Say Hello to SMTP
       
    fputs($smtpConnect"HELO $localhost" $newLine);
       
    $smtpResponse fgets($smtpConnect515);
       
    $logArray['heloresponse'] = "$smtpResponse";
       
       
    //Email From
       
    fputs($smtpConnect"MAIL FROM: $from" $newLine);
       
    $smtpResponse fgets($smtpConnect515);
       
    $logArray['mailfromresponse'] = "$smtpResponse";
           
       
    //Email To
       
    fputs($smtpConnect"RCPT TO: $to" $newLine);
       
    $smtpResponse fgets($smtpConnect515);
       
    $logArray['mailtoresponse'] = "$smtpResponse";
       
       
    //The Email
       
    fputs($smtpConnect"DATA" $newLine);
       
    $smtpResponse fgets($smtpConnect515);
       
    $logArray['data1response'] = "$smtpResponse";
       
       
    //Construct Headers
       
    $headers  "MIME-Version: 1.0" $newLine;
       
    $headers .= "Content-type: text/html; charset=iso-8859-1" $newLine;
       
    $headers .= "To: $nameto <$to>" $newLine;
       
    $headers .= "From: $namefrom <$from>" $newLine;
       
       
    fputs($smtpConnect"To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
       
    $smtpResponse fgets($smtpConnect515);
       
    $logArray['data2response'] = "$smtpResponse";
       
       
    // Say Bye to SMTP
       
    fputs($smtpConnect,"QUIT" $newLine);
       
    $smtpResponse fgets($smtpConnect515);
       
    $logArray['quitresponse'] = "$smtpResponse";    
    }
    ?>
    I have that included in the page with the following type of function:
    PHP Code:
    <?php
    $to 
    "post@example.com";
    $nameto "Who To";
    $from "post@example.com";
    $namefrom "Who From";
    $subject "Hello World Again!";
    $message "World, Hello!";
    authSendEmail($from$namefrom$to$nameto$subject$message);
    ?>
    When I run that, I get no errors, but no email is recieved...

    I'm thinking I need the first script before AND after the second one...is that right?

    Spam filter wouldn't be catching it, so its not that...

    Did my host mess up the code, or will that code just not work?

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    It is really difficult to second guess what could be the cause of the problem without being able to test the script on the server.

    In any case, I would suggest that you use PHPMailer. It is open source and I have used it in the past...and was quite impressed with it.

    You can download PHPMailer from here:

    http://phpmailer.sourceforge.net/

    In any case, let's know if that does not work for you.

    Cheers,
    Ess

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    224
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks..
    looks like they just installed phpmailer and it works great.

    But what I don't understand is i can use mail() to send emails via a form, but not just mail() by itself..


  •  

    Posting Permissions

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