fsockopen-udfordring :-)
Jeg er helt tør for idéer og mangler et par gode råd eller konkrete anvisninger til en fsockopen-udfordring.Jeg har læst http://www.eksperten.dk/spm/205869 som giver en ret udførlig anvisning, der er lige til at copy-paste. Såvidt så godt.
De to udfordringer er:
1. Jeg kan ikke finde ud at at slette log-funktionen uden at smadre den øvrige kode.
2. Jeg kan ikke se, hvordan jeg skal få scriptet tilat sende html.
Er der nogen der har et bud på dét?
ab:)
For overskuelighedens skyld paster jeg lige koden herunder.
<?php
class smtp_client {
var $connection;
var $server;
var $elog_fp;
var $log_file='./smtp_client.log';
var $do_log=false;
function smtp_client($server='smtp.udbyder.dk') {
if (!$server) $this->server="localhost";
else $this->server=$server;
$this->connection = fsockopen($this->server, 25);
if ($this->connection <= 0) return 0;
$this->elog(fgets($this->connection, 1024));
$this->elog("HELO xyz\r\n", 1);
fputs($this->connection,"HELO xyz\r\n");
$this->elog(fgets($this->connection, 1024));
}
function email($from_mail, $email, $fornavn, $header, $subject, $body) {
if ($this->connection <= 0) return 0;
$this->elog("MAIL FROM:$from_mail", 1);
fputs($this->connection,"MAIL FROM:$from_mail\r\n");
$this->elog(fgets($this->connection, 1024));
$this->elog("RCPT TO:$email", 1);
fputs($this->connection, "RCPT TO:$email\r\n");
$this->elog(fgets($this->connection, 1024));
$this->elog("DATA", 1);
fputs($this->connection, "DATA\r\n");
$this->elog(fgets($this->connection, 1024));
$this->elog("Subject: $subject", 1);
$this->elog("To: $fornavn", 1);
fputs($this->connection,"Subject: $subject\r\n");
fputs($this->connection,"To: $fornavn\r\n");
if ($header) {
$this->elog($header, 1);
fputs($this->connection, "$header\r\n");
}
$this->elog("", 1);
$this->elog($body, 1);
$this->elog(".", 1);
fputs($this->connection,"\r\n");
fputs($this->connection,"$body \r\n");
fputs($this->connection,".\r\n");
$this->elog(fgets($this->connection, 1024));
return 1;
}
function send() {
if ($this->connection) {
fputs($this->connection, "QUIT\r\n");
fclose($this->connection);
$this->connection=0;
}
}
function close() { $this->send(); }
function elog($text, $mode=0) {
if (!$this->do_log) return;
if (!$this->elog_fp) {
if (!($this->elog_fp=fopen($this->log_file, 'a'))) return;
fwrite($this->elog_fp, "\n-------------------------------------------\n");
fwrite($this->elog_fp, " Sent " . date("Y-m-d H:i:s") . "\n");
fwrite($this->elog_fp, "-------------------------------------------\n");
}
if (!$mode) fwrite($this->elog_fp, " $text\n");
else fwrite($this->elog_fp, "$text\n");
}
}
require ("db.php");
$res = mysql_query("SELECT email FROM user WHERE nyhedsbrev = 'tilmeldt'") or die (mysql_error());
$from_mail = "min@mail.dk";
$header = "Content-type: text/plain; charset=ISO-8859-1";
$header .= "From: $afsender<$emailafsender>\r\n";
$header .= "X-Sender: <xsender@mail.dk>\r\n";
$header .= "X-Mailer: PHP\r\n"; //mailer
$header .= "Return-Path: <retur@allan.dk>\r\n";
//Uncomment this to send html format
$header .= "Content-Type: text/html; charset=iso-8859-1\r\n";
//$header .= "cc: cc@allan.dk\r\n";
//$header .= "bcc: bcc@allan.dk\r\n";
$subject = "Emne";
$body = "Her skal der html ind!";
$smtp = new smtp_client("smtp.udbyder.dk");
while (list($email, $fornavn) = mysql_fetch_row($res)) {
$smtp->email("$from_mail", "$email", "$fornavn", "$header", "$subject", "$body");
}
$smtp->send();
mysql_close();
?>
