SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sending mail with PHP and sockets?

    Hi,

    I know of the mail() function but what I want to do is connect to a SMTP server (using sockets), send the needed commands and then close the socket. Can anyone show me how to do this or maybe give me a link to a site/tutorial that does this?

    I also have another question regarding this issue; is it possible to check if an email is valid using sockets? Not just validate the pattern but validate the email itself...

    Thanks in advance!

    -- lilleman

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    with my kids
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://phpmailer.sourceforge.net

    includes class.smtp.php. works great.

  3. #3
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'm gonna download that and take a look at it. The reason to why I started this thread was because I want to get the basics of how to communicate with a SMTP server using PHP and sockets. But thanks anyway!

    -- lilleman

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All you'll need is within this script

    As to your other question, yes it is possible to verify if an email address actually exists or not via sockets - just don't ask my how though

    It has been brought up before with solutions if it helps

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    with my kids
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you haven't already, you'll also want to look at the SMTP RFC

  6. #6
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh ****, when you said "It has been brought up before" I realized that I havent even tried to search the forum for an answer...

    -- lilleman

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good Luck Let me know when you find something useful since I was looking earlier this year and didn't turn up the thread I was thinking of

  8. #8
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't find anything that was of interest to me. I've tried to use fsockopen() to open a socket to the SMTP server but I just can't make it work. This is what I've tried:

    PHP Code:
    $socket fsockopen('mail.server.com'80$errno$errstr30 ); 
    Unfortunately, that doesn't work. Anyone?

    -- lilleman

  9. #9
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I was using the wrong port. It should be port 25, not port 80.

    -- lilleman

  10. #10
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is how far I've come by following the instructions on this page.

    PHP Code:
    <?php
    function writeln($input) {
        echo 
    htmlentities($input) . '<br />' "\n";
    }

    // open a new socket
    $socket fsockopen('mail1.telia.com'25$errno$errstr30 );

    // define the sender
    echo '<p>';
        
    $command "MAIL FROM:<erik@riklund.org>\r \n";
        
    writeln$command );
        
    fputs$socket$command );
        
    writelnfgets$socket128 ) );
    echo 
    '</p>';

    // define the recipient
    echo '<p>';
        
    $command "RCPT TO:<erik_riklund@hotmail.com> \r\n";
        
    writeln$command );
        
    fputs$socket$command );
        
    writelnfgets$socket128 ) );
    echo 
    '</p>';

    ?>
    The result:

    Code:
    MAIL FROM:<erik@riklund.org>
    220 smtp2-1-sn4.m-sp.skanova.net ESMTP Postfix
    
    RCPT TO:<erik_riklund@hotmail.com>
    503 Error: need MAIL command
    What might be wrong?

    -- lilleman

  11. #11
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got a reply on a swedish forum where a member pointed out to me that I haven't logged in into the server. I've also read that the first command that you should send is the "HELO" command. Therefor, I tried this code but instead of getting a "250 OK" reply on the HELO command, I get a "220 smtp2-1-sn4.m-sp.skanova.net ESMTP Postfix". What should I do next?

    Here's the code as it is now:

    PHP Code:
    <?php
    function writeln($input) {
        echo 
    htmlentities($input) . '<br />' "\n";
    }
    function 
    send_command(&$socket$command) {
        echo 
    '<p>';
            
    writeln$command );
            
    fputs$socket$command );
            
    writelnfgets$socket1024 ) );
        echo 
    '</p>' "\n";
    }

    // open a new socket
    $socket = @fsockopen('mail1.telia.com'25$errno$errstr30 ) or die( "$errno$errstr);

    // send the commands
    send_command$socket"HELO www.telia.com\r\n" );
    #send_command( $socket, "MAIL FROM:<erik@riklund.org>\r\n" );
    send_command$socket"QUIT\r\n" );

    ?>
    -- lilleman

  12. #12
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    with my kids
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try doing it manually from telnet first. this is what it looks like when i just did it:
    telnet mailserver.example.com 25
    220 mailserver.example.com GroupWise Internet Agent 6.5.1
    2003 Novell, Inc. All rights reserved. Ready
    HELO mailserver.example.com
    250 mailserver.example.com Ok
    MAIL FROM:me@example.com
    250 Ok
    RCPT TO:you@example.com
    250 Ok
    DATA
    354 Enter mail, end with "." on a line by itself
    This is my mail.
    I hope I get it.
    .
    250 Ok
    QUIT
    221 mailserver.example.com Closing transmission channel

  13. #13
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked as it should. I recieved the email without any complications.

    -- lilleman

  14. #14
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Brisbane, QLD
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'd suggest reading up on the RFC, the basics of mail are as follows though (summarizing what everyone else has stated already):

    first, send HELO / EHLO to identify yourself (the latter if you're using ESMTP). then, send the MAIL FROM, then RCPT TO, assuming you're still good, then you can send DATA (which is the full email including headers).

    a space after the headers determines the body of the message. finally, a "." on a line by itself signifies the end of the email.

    finally, QUIT to terminate the session.

  15. #15
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    with my kids
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so is it working from your script to?
    you need to read the response from the server, and then send your command. look at the get_lines function in that phpmailer class.smtp.php file. as pseudo-code:
    PHP Code:
    connect($server);
    get_lines();
    if(
    220send_command('HELO...');
    get_lines();
    if(
    250send_command('MAIL FROM...');
    get_lines();
    if(
    250send_command('RCPT TO...');
    get_lines();
    if(
    250send_command('DATA');
    get_lines();
    if(
    354send_command("THIS IS MY EMAIL...\R\N.");
    get_lines();
    if(
    250send_command('QUIT'); 

  16. #16
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lilleman
    I know of the mail() function but what I want to do is connect to a SMTP server (using sockets), send the needed commands and then close the socket. Can anyone show me how to do this or maybe give me a link to a site/tutorial that does this?
    You may want to try this class for composing and sending messages that comes with a wrapper function named smtp_mail() . It emulates the mail() function but it always send messages via SMTP regardless of your platform and even provides additional features like SMTP authetication that you may need.

    Quote Originally Posted by lilleman
    I also have another question regarding this issue; is it possible to check if an email is valid using sockets? Not just validate the pattern but validate the email itself...
    This email validation class does exactly that.
    Manuel Lemos

    Metastorage - Data object relational mapping layer generator
    PHP Classes - Free ready to use OOP components in PHP


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
  •