Sending e-mail via socket
Sending e-mail via socket
am 23.02.2010 01:16:24 von Andre Polykanine
Hello everyone,
I've just subscribed to the list, and I already have a question.
what I need to do is to send mail using sockets. Actually, the
built-in Mail() function is great and I wouldn't have to search for
something else if I didn't need more than one message to be sent at a
time. Say, I have ten or a hundred of users who want to receive a
notification about new blog entries. If I use the mail() function in
the loop, it will be performed too slow since it constantly opens and
closes the door, I mean, the SMTP connection.
So I need an alternative.
And here's what I'm doing:
function socketmail($to, $subject, $message) {
$from="Oire.org Administration ";
$connect = fsockopen ("smtp.yandex.ru", 25, $errno, $errstr, 30);
if ($connect) {
$out="HELO localhost\r\n";
$out.="MAIL FROM: $from\n";
$out.="RCPT TO: $to\n";
$out.="DATA\r\n";
$out.="Content-Type: text/plain; charset=utf-8\n";
$out.="To: $to\n";
$out.="Subject: $subject\n";
$out.="\n\n";
$out.=$message." \r\n";
$out.=".\r\n";
$out.="RSET\r\n";
fwrite ($connect, $out);
fclose ($connect);
} else {
die ("Error: ".$errstr." ($errno)");
}
}
socketmail ("arthaelon@yandex.ru", "this is a socket mail test",
"Testing mail sending");
?>
And what I get is absolutely nothing. No errors, no warnings, no
message in the mailbox.
So three questions:
1. What's wrong with my script?
2. How to look where the error exactly is? Can't get server logs for
some reason (will talk to tech support probably).
3. How to do the same thing but with an ability to send multiple
messages without closing the connection after each message?
Thanks!
--
With best regards from Ukraine,
Andre
Http://oire.org/ - The Fantasy blogs of Oire
Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org
Yahoo! messenger: andre.polykanine; ICQ: 191749952
Twitter: http://twitter.com/m_elensule
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Sending e-mail via socket
am 23.02.2010 01:58:41 von Rene Veerman
have you tried mail() with a large bcc header?
On Tue, Feb 23, 2010 at 1:16 AM, Andre Polykanine wrote:
> Hello everyone,
> I've just subscribed to the list, and I already have a question.
> what I need to do is to send mail using sockets. Actually, the
> built-in Mail() function is great and I wouldn't have to search for
> something else if I didn't need more than one message to be sent at a
> time. Say, I have ten or a hundred of users who want to receive a
> notification about new blog entries. If I use the mail() function in
> the loop, it will be performed too slow since it constantly opens and
> closes the door, I mean, the SMTP connection.
> So I need an alternative.
> And here's what I'm doing:
>
>
> function socketmail($to, $subject, $message) {
> $from=3D"Oire.org Administration ";
> =A0 =A0$connect =3D fsockopen ("smtp.yandex.ru", 25, $errno, $errstr, 30)=
;
> if ($connect) {
> $out=3D"HELO localhost\r\n";
> $out.=3D"MAIL FROM: $from\n";
> $out.=3D"RCPT TO: $to\n";
> $out.=3D"DATA\r\n";
> $out.=3D"Content-Type: text/plain; charset=3Dutf-8\n";
> $out.=3D"To: $to\n";
> $out.=3D"Subject: $subject\n";
> $out.=3D"\n\n";
> $out.=3D$message." \r\n";
> $out.=3D".\r\n";
> $out.=3D"RSET\r\n";
> fwrite ($connect, $out);
> fclose ($connect);
> } else {
> die ("Error: ".$errstr." ($errno)");
> }
> }
>
> socketmail ("arthaelon@yandex.ru", "this is a socket mail test",
> "Testing mail sending");
> ?>
>
> And what I get is absolutely nothing. No errors, no warnings, no
> message in the mailbox.
> So three questions:
> 1. What's wrong with my script?
> 2. How to look where the error exactly is? Can't get server logs for
> some reason (will talk to tech support probably).
> 3. How to do the same thing but with an ability to send multiple
> messages without closing the connection after each message?
>
> Thanks!
>
> --
> With best regards from Ukraine,
> Andre
> Http://oire.org/ - The Fantasy blogs of Oire
> Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ j=
abber.org
> Yahoo! messenger: andre.polykanine; ICQ: 191749952
> Twitter: http://twitter.com/m_elensule
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Sending e-mail via socket
am 23.02.2010 02:58:17 von Paul M Foster
On Tue, Feb 23, 2010 at 02:16:24AM +0200, Andre Polykanine wrote:
> Hello everyone,
> I've just subscribed to the list, and I already have a question.
> what I need to do is to send mail using sockets. Actually, the
> built-in Mail() function is great and I wouldn't have to search for
> something else if I didn't need more than one message to be sent at a
> time. Say, I have ten or a hundred of users who want to receive a
> notification about new blog entries. If I use the mail() function in
> the loop, it will be performed too slow since it constantly opens and
> closes the door, I mean, the SMTP connection.
> So I need an alternative.
> And here's what I'm doing:
>
>
> function socketmail($to, $subject, $message) {
> $from="Oire.org Administration ";
> $connect = fsockopen ("smtp.yandex.ru", 25, $errno, $errstr, 30);
> if ($connect) {
> $out="HELO localhost\r\n";
> $out.="MAIL FROM: $from\n";
> $out.="RCPT TO: $to\n";
> $out.="DATA\r\n";
> $out.="Content-Type: text/plain; charset=utf-8\n";
> $out.="To: $to\n";
> $out.="Subject: $subject\n";
> $out.="\n\n";
> $out.=$message." \r\n";
> $out.=".\r\n";
> $out.="RSET\r\n";
> fwrite ($connect, $out);
> fclose ($connect);
> } else {
> die ("Error: ".$errstr." ($errno)");
> }
> }
>
> socketmail ("arthaelon@yandex.ru", "this is a socket mail test",
> "Testing mail sending");
> ?>
>
> And what I get is absolutely nothing. No errors, no warnings, no
> message in the mailbox.
> So three questions:
> 1. What's wrong with my script?
> 2. How to look where the error exactly is? Can't get server logs for
> some reason (will talk to tech support probably).
> 3. How to do the same thing but with an ability to send multiple
> messages without closing the connection after each message?
First, if you're using the mail() function to talk to a *local* SMTP
server, you shouldn't have a long lag at all. The local SMTP server
should queue the messages and deal with the remote connections on its
own time, withough blocking..
Second, you're doing this socket operation as though it's a static
one-sided conversation. I'm not an expert, but SMTP conversations don't
normally work this way. You issue the HELO, wait for the response, issue
other commands, wait for the response, etc. The way you're doing it, if
your SMTP conversation runs into any snags (like the RCPT TO is not
recognized), you won't know it. Your function will simply ride over the
error, because it's not listening to the SMTP server.
Again, I'm not an expert, so maybe there's something I've overlooked.
Paul
--
Paul M. Foster
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Sending e-mail via socket
am 23.02.2010 08:39:36 von Per Jessen
Andre Polykanine wrote:
> Hello everyone,
> I've just subscribed to the list, and I already have a question.
> what I need to do is to send mail using sockets. Actually, the
> built-in Mail() function is great and I wouldn't have to search for
> something else if I didn't need more than one message to be sent at a=
> time. Say, I have ten or a hundred of users who want to receive a
> notification about new blog entries. If I use the mail() function in
> the loop, it will be performed too slow since it constantly opens and=
> closes the door, I mean, the SMTP connection.
Use sendmail to drop the emails straight into your MTA queue.
/Per
--=20
Per Jessen, Zürich (6.9°C)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Sending e-mail via socket
am 23.02.2010 09:23:41 von Per Jessen
Paul M Foster wrote:
> Second, you're doing this socket operation as though it's a static
> one-sided conversation. I'm not an expert, but SMTP conversations
> don't normally work this way. You issue the HELO, wait for the
> response, issue other commands, wait for the response, etc. The way
> you're doing it, if your SMTP conversation runs into any snags (like
> the RCPT TO is not recognized), you won't know it. Your function will=
> simply ride over the error, because it's not listening to the SMTP
> server.
Even if the mailserver does pipelining, he'll still need to do the EHLO=
separately and wait for the response to see if it does. After that you=
can fire off everything in one go.
/Per
--=20
Per Jessen, Zürich (7.8°C)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: Sending e-mail via socket
am 23.02.2010 10:45:51 von Andre Polykanine
Hello Rene,
Can't do that since the message is personalized: I need to put in the
user name ("Hello $username") and some other data, so the BCC is not a
solution, unfortunately...
--=20
With best regards from Ukraine,
Andre
Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jab=
ber.org
Yahoo! messenger: andre.polykanine; ICQ: 191749952
Twitter: m_elensule
----- Original message -----
From: Rene Veerman
To: Andre Polykanine
Date: Tuesday, February 23, 2010, 2:58:41 AM
Subject: [PHP] Sending e-mail via socket
have you tried mail() with a large bcc header?
On Tue, Feb 23, 2010 at 1:16 AM, Andre Polykanine wrote:
> Hello everyone,
> I've just subscribed to the list, and I already have a question.
> what I need to do is to send mail using sockets. Actually, the
> built-in Mail() function is great and I wouldn't have to search for
> something else if I didn't need more than one message to be sent at a
> time. Say, I have ten or a hundred of users who want to receive a
> notification about new blog entries. If I use the mail() function in
> the loop, it will be performed too slow since it constantly opens and
> closes the door, I mean, the SMTP connection.
> So I need an alternative.
> And here's what I'm doing:
>
>
> function socketmail($to, $subject, $message) {
> $from=3D"Oire.org Administration ";
> =A0 =A0$connect =3D fsockopen ("smtp.yandex.ru", 25, $errno, $errstr, 30);
> if ($connect) {
> $out=3D"HELO localhost\r\n";
> $out.=3D"MAIL FROM: $from\n";
> $out.=3D"RCPT TO: $to\n";
> $out.=3D"DATA\r\n";
> $out.=3D"Content-Type: text/plain; charset=3Dutf-8\n";
> $out.=3D"To: $to\n";
> $out.=3D"Subject: $subject\n";
> $out.=3D"\n\n";
> $out.=3D$message." \r\n";
> $out.=3D".\r\n";
> $out.=3D"RSET\r\n";
> fwrite ($connect, $out);
> fclose ($connect);
> } else {
> die ("Error: ".$errstr." ($errno)");
> }
> }
>
> socketmail ("arthaelon@yandex.ru", "this is a socket mail test",
> "Testing mail sending");
> ?>
>
> And what I get is absolutely nothing. No errors, no warnings, no
> message in the mailbox.
> So three questions:
> 1. What's wrong with my script?
> 2. How to look where the error exactly is? Can't get server logs for
> some reason (will talk to tech support probably).
> 3. How to do the same thing but with an ability to send multiple
> messages without closing the connection after each message?
>
> Thanks!
>
> --
> With best regards from Ukraine,
> Andre
> Http://oire.org/ - The Fantasy blogs of Oire
> Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ j=
abber.org
> Yahoo! messenger: andre.polykanine; ICQ: 191749952
> Twitter: http://twitter.com/m_elensule
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Sending e-mail via socket
am 23.02.2010 19:06:08 von Skylinux
On 02/23/2010 01:16 AM, Andre Polykanine wrote:
> Hello everyone,
> I've just subscribed to the list, and I already have a question.
> what I need to do is to send mail using sockets. Actually, the
> built-in Mail() function is great and I wouldn't have to search for
> something else if I didn't need more than one message to be sent at a
> time. Say, I have ten or a hundred of users who want to receive a
> notification about new blog entries. If I use the mail() function in
> the loop, it will be performed too slow since it constantly opens and
> closes the door, I mean, the SMTP connection.
Have you figured it out yet?
I have written a SMTP function a couple of years ago which will talk to
any SMTP server directly via socket. The only limitation I have noticed
is that it does not support encrypted logins but that can be added, I
just never bothered to do it.
So let me know if you still require it and I'll dig it out.
--
John
Ihr führt Krieg? Ihr fürchtet euch vor einem Nachbarn? So nehmt doch die
Grenzsteine weg - so habt ihr keinen Nachbarn mehr. Aber ihr wollt den
Krieg und deshalb erst setztet ihr die Grenzsteine.
[Friedrich Nietzsche]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php