/**
 * Sendas retmesagxon al iu h**o.
 *
 * $subjekto  - temlinio de la mesagxo (en UTF-8, EO-signoj per c^-kodigo)
 * $korpo     - la teksto de la mesagxo (dito)
 * $to_name   - la nomo de la ricevonto (dito)
 * $to_adress - la retposxtadreso de la ricevonto
 */
function sendu_xxxxx_mesagxon($subjekto, $korpo, $to_name, $to_address)
{
    $subject = eotransformado($subjekto, "utf-8");
    //  $mesagxo  = "### auxtomata mesagxo de la DEJ-aligilo ###\n\n";
    $mesagxo .= eotransformado($korpo, "utf-8");
    $from_name = "Pauxlo Ebermann";
    // TODO: (eble prenu nomon aux el la datumbazo/konfiguro, aux la entajpanton ?)
    $from_address = "*****@*****.**";
    // TODO: Eble prenu el la datumbazo?
    $email_message = new email_message_class();
    $email_message->default_charset = "UTF-8";
    if (!strcmp($error = $email_message->SetEncodedEmailHeader("To", $to_address, eotransformado($to_name, "utf-8")), "") && !strcmp($error = $email_message->SetEncodedEmailHeader("From", $from_address, $from_name), "") && !strcmp($error = $email_message->SetEncodedEmailHeader("Reply-To", $from_address, $from_name), "") && !strcmp($error = $email_message->SetEncodedHeader("Errors-To", $from_address, $from_name), "") && !strcmp($error = $email_message->SetEncodedEmailHeader("Bcc", "*****@*****.**", "Paul Ebermann"), "") && !strcmp($error = $email_message->SetEncodedHeader("Subject", $subject), "") && !strcmp($error = $email_message->AddQuotedPrintableTextPart($email_message->WrapText($mesagxo)), "")) {
        $error = $email_message->Send();
    }
    if ($error) {
        erareldono($error);
        exit;
    }
}
$background_image_content_id = "cid:" . $email_message->GetPartContentID($background_image_part);
/*
 *  The URL of referenced parts in HTML starts with cid:
 *  followed by the Contentp-ID string. Notice the image link below.
 */
$html_message = "<html>\n<head>\n<title>{$subject}</title>\n<style type=\"text/css\"><!--\nbody { color: black ; font-family: arial, helvetica, sans-serif ; background-color: #A3C5CC }\nA:link, A:visited, A:active { text-decoration: underline }\n--></style>\n</head>\n<body>\n<table background=\"{$background_image_content_id}\" width=\"100%\">\n<tr>\n<td>\n<center><h1>{$subject}</h1></center>\n<hr>\n<P>Hello " . strtok($to_name, " ") . ",<br><br>\nThis message is just to let you know that the <a href=\"http://www.phpclasses.org/mimemessage\">MIME E-mail message composing and sending PHP class</a> is working as expected.<br><br>\n<center><h2>Here is an image embedded in a message as a separate part:</h2></center>\n<center><img src=\"cid:" . $image_content_id . "\"></center>" . "Thank you,<br>\n{$from_name}</p>\n</td>\n</tr>\n</table>\n</body>\n</html>";
$email_message->CreateQuotedPrintableHTMLPart($html_message, "", $html_part);
/*
 *  It is strongly recommended that when you send HTML messages,
 *  also provide an alternative text version of HTML page,
 *  even if it is just to say that the message is in HTML,
 *  because more and more people tend to delete HTML only
 *  messages assuming that HTML messages are spam.
 */
$text_message = "This is an HTML message. Please use an HTML capable mail program to read this message.";
$email_message->CreateQuotedPrintableTextPart($email_message->WrapText($text_message), "", $text_part);
/*
 *  Multiple alternative parts are gathered in multipart/alternative parts.
 *  It is important that the fanciest part, in this case the HTML part,
 *  is specified as the last part because that is the way that HTML capable
 *  mail programs will show that part and not the text version part.
 */
$alternative_parts = array($text_part, $html_part);
$email_message->CreateAlternativeMultipart($alternative_parts, $alternative_part);
/*
 *  All related parts are gathered in a single multipart/related part.
 */
$related_parts = array($alternative_part, $image_part, $background_image_part);
$email_message->AddRelatedMultipart($related_parts);
/*
 *  One or more additional parts may be added as attachments.
/*
 *  Set the Return-Path header to define the envelope sender address to
 *  which bounced messages are delivered.
 *  If you are using Windows, you need to use the smtp_message_class to set
 *  the return-path address.
 */
if (defined("PHP_OS") && strcmp(substr(PHP_OS, 0, 3), "WIN")) {
    $email_message->SetHeader("Return-Path", $error_delivery_address);
}
$email_message->SetEncodedHeader("Subject", $subject);
/*
 *  A message with attached files usually has a text message part
 *  followed by one or more attached file parts.
 */
$text_message = "Hello " . strtok($to_name, " ") . "\n\nThis message forwarding another message that is encapsulated as attachment.\n\nThank you,\n{$from_name}";
$email_message->AddQuotedPrintableTextPart($email_message->WrapText($text_message));
$forwarding_message = array("FileName" => "message.eml", "Name" => "[Fwd: Testing Manuel Lemos' MIME E-mail composing and sending PHP class: HTML message]");
$email_message->AddMessagePart($forwarding_message);
/*
 *  The message is now ready to be assembled and sent.
 *  Notice that most of the functions used before this point may fail due
 *  to programming errors in your script. You may safely ignore any errors
 *  until the message is sent to not bloat your scripts with too much error
 *  checking.
 */
$error = $email_message->Send();
if (strcmp($error, "")) {
    echo "Error: {$error}\n";
} else {
    echo "Message sent to {$to_name}\n";
}
$email_message->CreateQuotedPrintableTextPart($message, "", $text_part);
/* Add the empty part wherever it belongs in the message. */
$email_message->AddPart($text_part);
/* Iterate personalization for each recipient. */
for ($recipient = 0; $recipient < count($to); $recipient++) {
    /* Personalize the recipient address. */
    $to_address = $to[$recipient]["address"];
    $to_name = $to[$recipient]["name"];
    $email_message->SetEncodedEmailHeader("To", $to_address, $to_name);
    /* Do we really need to personalize the message body?
     * If not, let the class reuse the message body defined for the first recipient above.
     */
    if (!$email_message->cache_body) {
        /* Create a personalized body part. */
        $message = "Hello " . strtok($to_name, " ") . ",\n\nThis message is just to let you know that Manuel Lemos' e-mail sending class is working as expected for sending personalized messages.\n\nThank you,\n{$from_name}";
        $email_message->CreateQuotedPrintableTextPart($email_message->WrapText($message), "", $recipient_text_part);
        /* Make the personalized replace the initially empty part */
        $email_message->ReplacePart($text_part, $recipient_text_part);
    }
    /* Send the message checking for eventually acumulated errors */
    $error = $email_message->Send();
    if (strlen($error)) {
        break;
    }
}
/* When you are done with bulk mailing call the SetBulkMail function
 * again passing 0 to tell the all deliveries were done.
 */
$email_message->SetBulkMail(0);
if (strlen($error)) {
    echo "Error: {$error}\n";
$html_message = $html_smarty->fetch("mailing.html.tpl");
$email_message->CreateQuotedPrintableHTMLPart($html_message, "", $html_part);
/*
 *  It is strongly recommended that when you send HTML messages,
 *  also provide an alternative text version of HTML page,
 *  even if it is just to say that the message is in HTML,
 *  because more and more people tend to delete HTML only
 *  messages assuming that HTML only messages are spam.
 */
$text_smarty->assign("subject", $subject);
$text_smarty->assign("fromname", $from_name);
$text_smarty->assign("firstname", "");
$text_smarty->assign("balance", "0");
$text_smarty->assign("email", "?");
$text_message = $text_smarty->fetch("mailing.txt.tpl");
$email_message->CreateQuotedPrintableTextPart($email_message->WrapText($text_message), "", $text_part);
/*
 *  Multiple alternative parts are gathered in multipart/alternative parts.
 *  It is important that the fanciest part, in this case the HTML part,
 *  is specified as the last part because that is the way that HTML capable
 *  mail programs will show that part and not the text version part.
 */
$alternative_parts = array($text_part, $html_part);
$email_message->AddAlternativeMultipart($alternative_parts);
/* Iterate personalization for each recipient. */
for ($recipient = 0; $recipient < count($to); $recipient++) {
    /* Personalize the recipient address. */
    $to_address = $to[$recipient]["address"];
    $to_name = $to[$recipient]["name"];
    $email_message->SetEncodedEmailHeader("To", $to_address, $to_name);
    /* Do we really need to personalize the message body?
/**
 * TODO: dokumentado por sendu_konfirmilon
 *
 * $teksto - en tiu variablo ni metos la tekston de la mesagxo,
 *           por ebligi montri gxin ankoraux en la retpagxo (krom
 *           la dissendado).
 *
 * ### uzado:  partrezultoj.php, AligxiloDankon.php ###
 */
function sendu_konfirmilon($partoprenanto, $partopreno, $renkontigxo, &$teksto)
{
    $subject = "unua konfirmilo por la " . $renkontigxo->datoj[nomo];
    $mesagxo = "### auxtomata mesagxo ###\n\n";
    $from_name = "IS-Aligilo";
    $from_address = "*****@*****.**";
    // TODO: forigi retadreson
    $to_name = utf8_decode($partoprenanto->datoj[personanomo] . " " . $partoprenanto->datoj[nomo]);
    $to_address = $partoprenanto->datoj[retposxto];
    $teksto = faru_1akonfirmilon($partoprenanto, $partopreno, $renkontigxo);
    $mesagxo .= $teksto;
    $email_message = new email_message_class();
    $email_message->default_charset = "UTF-8";
    if (!strcmp($error = $email_message->SetEncodedEmailHeader("To", $to_address, $to_name), "") && !strcmp($error = $email_message->SetEncodedEmailHeader("From", $from_address, $from_name), "") && !strcmp($error = $email_message->SetEncodedEmailHeader("Reply-To", $from_address, $from_name), "") && !strcmp($error = $email_message->SetEncodedEmailHeader("Bcc", "*****@*****.**", "Paul Ebermann"), "") && !strcmp($error = $email_message->SetEncodedHeader("Errors-To", $from_address, $from_name), "") && !strcmp($error = $email_message->SetEncodedHeader("Subject", $subject), "") && !strcmp($error = $email_message->AddQuotedPrintableTextPart($email_message->WrapText($mesagxo)), "")) {
        $error = $email_message->Send();
    }
    if ($error) {
        erareldono($error);
        exit;
    }
}