17.4. Posting Messages to Usenet Newsgroups
Problem
You want to post a message to a Usenet newsgroup, such as comp.lang.php.
Solution
Use imap_mail_compose( )
to format the message, then write the message
to the server using
sockets:
$headers['from'] = 'adam@example.com'; $headers['subject'] = 'New Version of PHP Released!'; $headers['custom_headers'][] = 'Newsgroups: comp.lang.php'; $body[0]['type'] = TYPETEXT; $body[0]['subtype'] = 'plain'; $body[0]['contents.data'] = 'Go to http://www.php.net and download it today!'; $post = imap_mail_compose($headers, $body); $server = 'nntp.example.com'; $port = 119; $sh = fsockopen($server, $port) or die ("Can't connect to $server."); fputs($sh, "POST\r\n"); fputs($sh, $post); fputs($sh, ".\r\n"); fclose($sh);
Discussion
No built-in PHP functions can post a message to a newsgroup.
Therefore, you must open a direct socket connection to the news
server and send the commands to post the message. However, you can
use imap_mail_compose( )
to format a post and
create the headers and body for the message. Every message must have
three headers: the From
: address, the message
Subject
:, and the name of the newsgroup:
$headers['from'] = 'adam@example.com'; $headers['subject'] = 'New Version of PHP Released!'; $headers['custom_headers'][ ] = 'Newsgroups: comp.lang.php';
Create an array,
$headers
, to hold the message headers. You can
directly assign the values for the From
: and
Subject
: headers, but you can’t
do so for the Newsgroups
: header. Because ...
Get PHP Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.