$mail2 = new email('This is an email, that has another email attached.');
$mail2->add_attachment($rfc822_email, 'Forwarded Email', 'message/rfc822');
echo $mail2->direct_send('Fisk <[email protected]>', 'Allan <[email protected]>', 'My Subject 3');
*/
class email
{
var $mime;
var $html;
var $body;
var $do_html;
var $multipart;
var $html_text;
var $html_images;
var $headers;
var $parts;
var $charset;
var $charsetlist;
var $messagebuilt = 0;
/**
* Constructor. Do not set body if sending MIME mail.
*
* @access public
* @param string body Body content of non HTML mails.
*/
/**
* Change/set additional message header.
*
* Shouldn't really be necessary as you could use the constructor and send functions,
* it's here nonetheless. Takes any number of arguments, which can be either strings
* or arrays full of strings.
*
* @access public
*/
/**
* Set the content charset.
*
* @access public
*/
function set_charset($charset = '', $raw = false)
{
if ($raw == true) {
$this->charset= $charset;
return true;
}
if (is_string($charset)) {
while (list($k,$v)= each($this->charsetlist)) {
if ($k == $charset) {
$this->charset = $v;
return true;
}
}
}
return false;
}
/**
* Adds a HTML part to the mail.
*
* Also replaces image names with content-id's.
*
* @access public
* @param string html HTML code to add.
* @param string text Text alternative for non-HTML clients.
* Will be generated from $html if not specified.
*/
function add_html($html, $text = null)
{
// Generate plain text alternative to $html
if (is_null($text)) {
$text = HTML2Text($html);
}
/**
* Add a file to the list of attachments.
*
* @access public
* @param string file Full path/file name.
* @param string name Display name.
* @param string c_type MIME type.
*/
/**
* Sends message to one recipient with sendmail.
*
* @access public
* @param string from Sender - e.g. Allan Hansen <[email protected]>
* @param string to Recipient - e.g. Allan Hansen <[email protected]>
* @param string subject Mail subject.
* @param string headers Additional headers to this message only.
*/
function send($from, $to, $subject = '', $headers = '')
{
// Build message
if (!$this->messagebuilt) {
$this->build_message();
}
// Make header
$Headers = trim("From: $from\n".implode("\n", $this->headers)."\n$headers");
// Extract to e-mail address.
if (ereg("^(.*)<(.*)>", $to, $regs)) {
$to_e = $regs[2];
}
else {
$to_e = $to;
}
// Extract from e-mail address. Pass to sendmail -f to set Return-Path
if (ereg("^(.*)<(.*)>", $from, $regs)) {
$from_e = $regs[2];
}
else {
$from_e = $from;
}
/**
* Sends message to one recipient direct SMTP connection.
*
* Requires PEAR.
*
* @access public
* @param string from Sender - e.g. Allan Hansen <[email protected]>
* @param string to Recipient - e.g. Allan Hansen <[email protected]>
* @param string subject Mail subject.
* @param string headers Additional headers to this message only.
*/
function direct_send($from, $to, $subject= '', $headers= '')
{
// Build message
if (!$this->messagebuilt) {
$this->build_message();
}
// Make header
if (!strstr($headers, "To: ")) {
$Headers = "To: $to\n";
}
$Headers .= trim("From: $from\nSubject: $subject\n".implode("\n", $this->headers)."\n$headers");
// Extract to e-mail address.
if (ereg("^(.*)<(.*)>", $to, $regs)) {
$to_e = $regs[2];
}
else {
$to_e = $to;
}
/**
* Use this method to return the email in message/rfc822 format.
* Useful for adding an email to another email as an attachment.
* There's a commented out example at the top of this file.
*
* @access public
* @param string from Sender - e.g. Allan Hansen <[email protected]>
* @param string to Recipient - e.g. Allan Hansen <[email protected]>
* @param string subject Mail subject.
* @param string headers Additional headers to this message only.
*/
function get_rfc822($to, $from, $subject= '', $headers= '')
{
// Build message
if (!$this->messagebuilt) {
$this->build_message();
}
// Make up the date header as according to RFC822
$date = 'Date: '.date('D, d M y H:i:s');
if (is_string($subject)) {
$subject = 'Subject: '.$subject;
}
if (is_string($headers)) {
$headers = explode("\n", trim($headers));
}
// send
if (ValidateEmail($to)) {
$e->send($from, $to, $subject);
}
}
// remove after PHP5
$GLOBALS["MAILQUEUE"] = array ();
}
/**
* Queue message for delivery
*
* @param string body Message body
* @param string subject Message subject
* @param string to Recipient - single address only
* @param string from From: Sender <[email protected]>
* @access public
*/