1<?php
2/***************************************************************************
3* copyright            : (C) 2001-2003 Advanced Internet Designs Inc.
4* email                : forum@prohost.org
5* $Id: smtp.inc.t 21069 2010-03-25 22:47:48Z Caeies $
6*
7* This program is free software; you can redistribute it and/or modify it
8* under the terms of the GNU General Public License as published by the
9* Free Software Foundation; either version 2 of the License, or
10* (at your option) any later version.
11***************************************************************************/
12
13class fud_smtp
14{
15	var $fs, $last_ret, $msg, $subject, $to, $from, $headers;
16
17	function get_return_code($cmp_code='250')
18	{
19		if (!($this->last_ret = fgets($this->fs, 1024))) {
20			return;
21		}
22		if (substr($this->last_ret, 0, 3) == $cmp_code) {
23			return 1;
24		}
25
26		return;
27	}
28
29	function wts($string)
30	{
31		fwrite($this->fs, $string . "\r\n");
32	}
33
34	function open_smtp_connex()
35	{
36		if( !($this->fs = fsockopen($GLOBALS['FUD_SMTP_SERVER'], 25, $errno, $errstr, $GLOBALS['FUD_SMTP_TIMEOUT'])) ) {
37			exit("ERROR: stmp server at ".$GLOBALS['FUD_SMTP_SERVER']." is not avaliable<br>\nAdditional Problem Info: $errno -> $errstr <br>\n");
38		}
39		if (!$this->get_return_code(220)) {
40			return;
41		}
42		$this->wts("HELO ".$GLOBALS['FUD_SMTP_SERVER']);
43		if (!$this->get_return_code()) {
44			return;
45		}
46
47		/* Do SMTP Auth if needed */
48		if ($GLOBALS['FUD_SMTP_LOGIN']) {
49			$this->wts('AUTH LOGIN');
50			if (!$this->get_return_code(334)) {
51				return;
52			}
53			$this->wts(base64_encode($GLOBALS['FUD_SMTP_LOGIN']));
54			if (!$this->get_return_code(334)) {
55				return;
56			}
57			$this->wts(base64_encode($GLOBALS['FUD_SMTP_PASS']));
58			if (!$this->get_return_code(235)) {
59				return;
60			}
61		}
62
63		return 1;
64	}
65
66	function send_from_hdr()
67	{
68		$this->wts('MAIL FROM: <'.$GLOBALS['NOTIFY_FROM'].'>');
69		return $this->get_return_code();
70	}
71
72	function send_to_hdr()
73	{
74		if (!@is_array($this->to)) {
75			$this->to = array($this->to);
76		}
77
78		foreach ($this->to as $to_addr) {
79			$this->wts('RCPT TO: <'.$to_addr.'>');
80			if (!$this->get_return_code()) {
81				return;
82			}
83		}
84		return 1;
85	}
86
87	function send_data()
88	{
89		$this->wts('DATA');
90		if (!$this->get_return_code(354)) {
91			return;
92		}
93
94		/* This is done to ensure what we comply with RFC requiring each line to end with \r\n */
95		$this->msg = preg_replace("!(\r)?\n!si", "\r\n", $this->msg);
96
97		if( empty($this->from) ) $this->from = $GLOBALS['NOTIFY_FROM'];
98
99		$this->wts('Subject: '.$this->subject);
100		$this->wts('Date: '.date("r"));
101		$this->wts('To: '.$GLOBALS['NOTIFY_FROM']);
102		$this->wts('From: '.$this->from);
103		$this->wts('X-Mailer: FUDforum v'.$GLOBALS['FORUM_VERSION']);
104		$this->wts($this->headers."\r\n");
105		$this->wts($this->msg);
106		$this->wts('.');
107
108		return $this->get_return_code();
109	}
110
111	function close_connex()
112	{
113		$this->wts('quit');
114		fclose($this->fs);
115	}
116
117	function send_smtp_email()
118	{
119		if (!$this->open_smtp_connex()) {
120			exit("Invalid STMP return code: ".$this->last_ret."<br>\n");
121		}
122		if (!$this->send_from_hdr()) {
123			$this->close_connex();
124			exit("Invalid STMP return code: ".$this->last_ret."<br>\n");
125		}
126		if (!$this->send_to_hdr()) {
127			$this->close_connex();
128			exit("Invalid STMP return code: ".$this->last_ret."<br>\n");
129		}
130		if (!$this->send_data()) {
131			$this->close_connex();
132			exit("Invalid STMP return code: ".$this->last_ret."<br>\n");
133		}
134
135		$this->close_connex();
136	}
137}
138?>