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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php code - Sending mail with external SMTP server

    Hello i'm working on a project and i've reach a dead end. What i'm trying to do with the code is this:

    1. Connecting to an external smtp server;
    2. Authenticating with login and pass using AUTH LOGIN using an external hostnames file (line-by-line), and external user and password file.
    3. Send a email to a predefined email address on file using an external file with the email body.

    My problem is that I dont receive any email, what i'm guessing is that the auth login code is broken. Also I want to make a single user and password file using this format user : password. Also I want to make the code do this: to get the first smtp hostname and to login with the usersassword in the external file provided. If unsuccessful login to try the next line in the users : password file. When finished with the login and password to jump to the next hostname in the hostname file.

    Here's my work so far:

    Code:
    <?php
    function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
    {
    
    function loadini($path) {
    $fp = fopen($path, "r");
    fclose($fp);
        }
    
    $message = loadini("message");
    $to = "broken@code.com";
    $from = "test@test.com";
    $namefrom = "test";
    $subject = "test also";
    $nameto = "notme";
    authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
    
    ?>
    //SMTP + SERVER DETAILS
    /* * * * CONFIGURATION START * * * */
    $smtpServer = loadini("logfile");;
    $port = "25";
    $timeout = "30";
    $username = loadini("users");
    $password = loadini("password");
    $localhost = "localhost";
    $newLine = "\r\n";
    /* * * * CONFIGURATION END * * * * */
    
    //Connect to the host on the specified port
    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
    $smtpResponse = fgets($smtpConnect, 515);
    if(empty($smtpConnect))
    {
    $output = "Failed to connect: $smtpResponse";
    return $output;
    }
    else
    {
    $logArray['connection'] = "Connected: $smtpResponse";
    }
    
    //Say Hello to SMTP
    fputs($smtpConnect, "HELO $localhost" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['heloresponse'] = "$smtpResponse";
    
    //Request Auth Login
    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authrequest'] = "$smtpResponse";
    
    //Send username
    fputs($smtpConnect, base64_encode($username) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authusername'] = "$smtpResponse";
    
    //Send password
    fputs($smtpConnect, base64_encode($password) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authpassword'] = "$smtpResponse";
    
    //Email From
    fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailfromresponse'] = "$smtpResponse";
    
    //Email To
    fputs($smtpConnect, "RCPT TO: $to" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailtoresponse'] = "$smtpResponse";
    
    //The Email
    fputs($smtpConnect, "DATA" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $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($smtpConnect, 515);
    $logArray['data2response'] = "$smtpResponse";
    
    // Say Bye to SMTP
    fputs($smtpConnect,"QUIT" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['quitresponse'] = "$smtpResponse";
    }
    ?>

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    May I suggest obtaining and using phpMailer which will make your life a LOT easier.
    Dave .... HostMonster for all of your hosting needs

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by djm0219 View Post
    May I suggest obtaining and using phpMailer which will make your life a LOT easier.
    Since its a school project i need to finish that code. Can u give me some tips since I'm quite new to this?
    Last edited by galford; 09-02-2011 at 07:11 AM.

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Well some debugging is certainly in order. You appear to be stashing responses in an array named $logArray but you don't display it so you can't tell what you are getting back from the server. Use print_r at various points to see what responses you are getting which should give you a clue to what is going wrong where.
    Dave .... HostMonster for all of your hosting needs

  • #5
    New Coder
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by djm0219 View Post
    Well some debugging is certainly in order. You appear to be stashing responses in an array named $logArray but you don't display it so you can't tell what you are getting back from the server. Use print_r at various points to see what responses you are getting which should give you a clue to what is going wrong where.
    Like I said I'm quite noobish... Can you please modify the code for my needs and repaste it here? I will be quite grateful.

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Errr, no but I will give you a clue.

    PHP Code:
    $logArray['connection'] = "Connected: $smtpResponse"
    You're capturing the response after every interaction but you aren't testing to see whether or not the interaction worked. Start by displaying the responses you are getting so you can see what is happening. Something like this:

    PHP Code:
    $logArray['connection'] = "Connected: $smtpResponse";
    echo 
    'Response from connection attempt was: ' $smtpResponse
    Add that echo with appropriate changes to where you are any time you are getting a response back.
    PHP Code:
    $logArray['heloresponse'] = "$smtpResponse";
    echo 
    'Response from HELO  was: ' $smtpResponse
    Dave .... HostMonster for all of your hosting needs

  • #7
    New Coder
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok i'll add those lines and see what went wrong. I'll come back.

  • #8
    New Coder
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I added those lines and yet to be amazed no echos, hope I made it right. The code is:

    Code:
    <?php
    function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
    {
    
    
    function loadini($path) {
    $fp = fopen($path, "r");
    $fpcontents = fread($fp, filesize($path));
    fclose($fp);
    return $fpcontents;
        }
    
    $message = loadini("message");
    $to = "broken@code.com";
    $from = "test@test.com";
    $namefrom = "test";
    $subject = "test also";
    $nameto = "notme";
    authSendEmail($from, $namefrom, $to, $nameto, $subject, $message); 
    
    
    //SMTP + SERVER DETAILS
    /* * * * CONFIGURATION START * * * */
    $smtpServer = loadini("logfile");;
    $port = "25";
    $timeout = "30";
    $username = loadini("users");
    $password = loadini("password");
    $localhost = "localhost";
    $newLine = "\r\n";
    /* * * * CONFIGURATION END * * * * */
    
    //Connect to the host on the specified port
    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
    $smtpResponse = fgets($smtpConnect, 515);
    if(empty($smtpConnect))
    {
    $output = "Failed to connect: $smtpResponse";
    return $output;
    }
    else
    {
    $logArray['connection'] = "Connected: $smtpResponse";
    
    }
    echo 'Response from connection attempt was: ' . $smtpResponse;
    //Say Hello to SMTP
    fputs($smtpConnect, "HELO $localhost" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['heloresponse'] = "$smtpResponse";
    echo 'Response from HELO was: ' . $smtpResponse;
    
    //Request Auth Login
    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authrequest'] = "$smtpResponse";
    echo 'Response from AUTH LOGIN was: ' . $smtpResponse;
    
    
    //Send username
    fputs($smtpConnect, base64_encode($username) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authusername'] = "$smtpResponse";
    echo 'Response from USERNAME was: ' . $smtpResponse;
    
    //Send password
    fputs($smtpConnect, base64_encode($password) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authpassword'] = "$smtpResponse";
    echo 'Response from PASSWORD was: ' . $smtpResponse;
    
    //Email From
    fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailfromresponse'] = "$smtpResponse";
    echo 'Response from EMAIL FROM was: ' . $smtpResponse;
    
    //Email To
    fputs($smtpConnect, "RCPT TO: $to" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailtoresponse'] = "$smtpResponse";
    echo 'Response from EMAIL TO was: ' . $smtpResponse;
    
    //The Email
    fputs($smtpConnect, "DATA" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['data1response'] = "$smtpResponse";
    echo 'Response from DATA was: ' . $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($smtpConnect, 515);
    $logArray['data2response'] = "$smtpResponse";
    echo 'Response from EMAIL RESPONSE was: ' . $smtpResponse;
    
    // Say Bye to SMTP
    fputs($smtpConnect,"QUIT" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['quitresponse'] = "$smtpResponse";
    echo 'Response from QUIT was: ' . $smtpResponse;
    }
    ?>
    Any suggestions? Bare with me a little longer.
    Last edited by galford; 09-02-2011 at 08:30 AM.

  • #9
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Sounds like your initial connection may not be working. Trying echoing the response from the connection attempt itself.

    PHP Code:
    $smtpResponse fgets($smtpConnect515);
    echo 
    'Connection response was: ' $smtpResponse
    I also notice that you appear to be recursively calling authSendEmail which may not be what you really mean to be doing (look at the last line of the snippet of your code that I pasted below). At the very least I would expect an error from PHP complaining about loadini being declared more than once. Do you have error reporting enabled?

    PHP Code:
    function authSendEmail($from$namefrom$to$nameto$subject$message)
    {


    function 
    loadini($path) {
    $fp fopen($path"r");
    $fpcontents fread($fpfilesize($path));
    fclose($fp);
    return 
    $fpcontents;
        }

    $message loadini("message");
    $to "broken@code.com";
    $from "test@test.com";
    $namefrom "test";
    $subject "test also";
    $nameto "notme";
    authSendEmail($from$namefrom$to$nameto$subject$message); 
    It would also help if you would use PHP tags around your code instead of CODE tags.
    Dave .... HostMonster for all of your hosting needs

  • #10
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    welcome to this Forum Site here u can find lots of thing in no minutes.i think for your doubt you should ask with your friends & i hope you will get your answer,thanks.....

    Top Ten Classified Websites

  • #11
    New Coder
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have error reporting on. Added those lines and still no echos.

    Here's the code:

    PHP Code:
    <?php
    function authSendEmail($from$namefrom$to$nameto$subject$message)
    {

    function 
    loadini($path) {
    $fp fopen($path"r");
    $fpcontents fread($fpfilesize($path));
    fclose($fp);
    return 
    $fpcontents;
        }
    $message loadini("message");
    $to "broken@code.com";
    $from "test@test.com";
    $namefrom "test";
    $subject "test also";
    $nameto "notme";

    authSendEmail($from$namefrom$to$nameto$subject$message);



    //SMTP + SERVER DETAILS
    /* * * * CONFIGURATION START * * * */
    $smtpServer loadini("logfile");;
    $port "25";
    $timeout "30";
    $username loadini("users");
    $password loadini("password");
    $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);
    echo 
    'Connection response was: ' $smtpResponse;

    if(empty(
    $smtpConnect))
    {
    $output "Failed to connect: $smtpResponse";
    return 
    $output;
    }
    else
    {
    $logArray['connection'] = "Connected: $smtpResponse";

    }
    echo 
    'Response from connection attempt was: ' $smtpResponse;

    //Say Hello to SMTP
    fputs($smtpConnect"HELO $localhost" $newLine);
    $smtpResponse fgets($smtpConnect515);
    $logArray['heloresponse'] = "$smtpResponse";
    echo 
    'Response from HELO was: ' $smtpResponse;

    //Request Auth Login
    fputs($smtpConnect,"AUTH LOGIN" $newLine);
    $smtpResponse fgets($smtpConnect515);
    $logArray['authrequest'] = "$smtpResponse";
    echo 
    'Response from AUTH LOGIN was: ' $smtpResponse;


    //Send username
    fputs($smtpConnectbase64_encode($username) . $newLine);
    $smtpResponse fgets($smtpConnect515);
    $logArray['authusername'] = "$smtpResponse";
    echo 
    'Response from USERNAME was: ' $smtpResponse;

    //Send password
    fputs($smtpConnectbase64_encode($password) . $newLine);
    $smtpResponse fgets($smtpConnect515);
    $logArray['authpassword'] = "$smtpResponse";
    echo 
    'Response from PASSWORD was: ' $smtpResponse;

    //Email From
    fputs($smtpConnect"MAIL FROM: $from" $newLine);
    $smtpResponse fgets($smtpConnect515);
    $logArray['mailfromresponse'] = "$smtpResponse";
    echo 
    'Response from EMAIL FROM was: ' $smtpResponse;

    //Email To
    fputs($smtpConnect"RCPT TO: $to" $newLine);
    $smtpResponse fgets($smtpConnect515);
    $logArray['mailtoresponse'] = "$smtpResponse";
    echo 
    'Response from EMAIL TO was: ' $smtpResponse;

    //The Email
    fputs($smtpConnect"DATA" $newLine);
    $smtpResponse fgets($smtpConnect515);
    $logArray['data1response'] = "$smtpResponse";
    echo 
    'Response from DATA was: ' $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";
    echo 
    'Response from EMAIL RESPONSE was: ' $smtpResponse;

    // Say Bye to SMTP
    fputs($smtpConnect,"QUIT" $newLine);
    $smtpResponse fgets($smtpConnect515);
    $logArray['quitresponse'] = "$smtpResponse";
    echo 
    'Response from QUIT was: ' $smtpResponse;
    }
    ?>

  • #12
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    What is calling the authSendEmail function? If you are not getting any output at all it would seem that the function is never being called.
    Dave .... HostMonster for all of your hosting needs

  • #13
    New Coder
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I erased that line.

    Connection response was: 220 **************************************************************************************************** ******************
    Response from connection attempt was: 220 **************************************************************************************************** ******************
    Response from HELO was: 250 localhost Hello [localhost]
    Response from AUTH LOGIN was: 503 5.5.2 Send hello first.
    Response from USERNAME was: 500 5.3.3 Unrecognized command
    Response from PASSWORD was: 500 5.3.3 Unrecognized command

    it seems like after send HELO command it stucks. But the smtp server receives the HELO command.
    Last edited by galford; 09-02-2011 at 09:16 AM.

  • #14
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Now you're getting somewhere. The response from HELO appear to be correct though the SMTP server you are trying to talk to may not be happy with localhost or it may be expecting EHLO instead of HELO.

    The AUTH command expects the first parameter to be the type of authentication to be used not the word LOGIN
    Dave .... HostMonster for all of your hosting needs

  • #15
    New Coder
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All looks fine now except the mailing procedure.

    Connection response was: 220 **************************************************************************************************** ******************
    Response from connection attempt was: 220 **************************************************************************************************** ******************
    Response from HELO was: 250-localhost [localhost]
    Response from AUTH LOGIN was: 250-XXXA
    Response from USERNAME was: 250-SIZE
    Response from PASSWORD was: 250-ETRN
    Response from EMAIL FROM was: 250-PIPELINING
    Response from EMAIL TO was: 250-DSN
    Response from DATA was: 250-ENHANCEDSTATUSCODES
    Response from EMAIL RESPONSE was: 250-8bitmime
    Response from QUIT was: 250-BINARYMIME

    I'm guessing its not implemented. That function that I erased but needs to be reworked I guess since i'm not receiving the email.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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