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

http://phpmailer.sourceforge.net

includes class.smtp.php. works great.

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

All you’ll need is within this script :slight_smile:

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 :wink:

It has been brought up before with solutions if it helps :slight_smile:

if you haven’t already, you’ll also want to look at the SMTP RFC

Oh ****, when you said “It has been brought up before” I realized that I havent even tried to search the forum for an answer… :blush:

– lilleman

Good Luck :slight_smile: 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 :frowning:

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:

$socket = fsockopen('mail.server.com', 80, $errno, $errstr, 30 );

Unfortunately, that doesn’t work. Anyone?

– lilleman

Sorry, I was using the wrong port. It should be port 25, not port 80.

– lilleman

This is how far I’ve come by following the instructions on this page.

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

// open a new socket
$socket = fsockopen('mail1.telia.com', 25, $errno, $errstr, 30 );

// define the sender
echo '<p>';
	$command = "MAIL FROM:<erik@riklund.org>\\r \
";
	writeln( $command );
	fputs( $socket, $command );
	writeln( fgets( $socket, 128 ) );
echo '</p>';

// define the recipient
echo '<p>';
	$command = "RCPT TO:<erik_riklund@hotmail.com> \\r\
";
	writeln( $command );
	fputs( $socket, $command );
	writeln( fgets( $socket, 128 ) );
echo '</p>';

?>

The result:

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

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
function writeln($input) {
	echo htmlentities($input) . '<br />' . "\
";
}
function send_command(&$socket, $command) {
	echo '<p>';
		writeln( $command );
		fputs( $socket, $command );
		writeln( fgets( $socket, 1024 ) );
	echo '</p>' . "\
";
}

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

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

?>

– lilleman

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

That worked as it should. I recieved the email without any complications.

– lilleman

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.

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:


connect($server);
get_lines();
if(220) send_command('HELO...');
get_lines();
if(250) send_command('MAIL FROM...');
get_lines();
if(250) send_command('RCPT TO...');
get_lines();
if(250) send_command('DATA');
get_lines();
if(354) send_command("THIS IS MY EMAIL...\\R\\N.");
get_lines();
if(250) send_command('QUIT');

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.

This email validation class does exactly that.