I am planning on building a way to use PEAR to send out receipts via email. I will be sending html and plain text versions. From what I've read, PEAR seems to be the best fit for this. I've looked at numerous examples and feel pretty good about getting things started. I've only seen examples of sending static emails. I plan to send dynamic emails and want to get some feedback on how I plan to implement. Any help is appreciated and hopefully this makes sense. Let me know if you need more info.
doEmail.php
This PHP document is required from another document that handles processing orders.
function sendEmail($action, $oId, $email) {
// Email customer a receipt
include('Mail.php');
include('Mail\mime.php');
$message = new Mail_mime();
$text = textEmail($action, $oId);
$html = htmlEmail($action, $oId);
$message->setTXTBody($text);
$message->setHTMLBody($html);
$body = $message->get();
$extraheaders = array('From'=>'shop@mycompany.com', 'Subject'=>'STORE ORDER - THANK YOU');
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send('$email', $headers, $body);
}
Below is the function to create the plain text email. There will be more lines to it, but hopefully gives you the idea on how I plan to implement.
// Build the customer text receipt email
function textEmail($lastOrder) {
$theOrder = getOrder($lastOrder);
extract($theOrder);
$txtReceipt = 'Promosio Receipt' . "\n";
$txtReceipt .= '-----------------------------------------------------------' . "\n\n\n";
$txtReceipt .= ''.$notice.' . "\n\n\n"';
$txtReceipt .= 'ORDER INFO:' . "\n";
return $txtReceipt;
}
Below is the function that will generate the HTML email. I've set up the actual HTML to be called from a require_once. Originally I was thinking this would help keep the doEmail.php document more manageable by separating it. Not sure if this is wise or not? The htmlEmail.php document would then build the html and make any calls to the database and return it back to the PEAR sendEmail function (function at top of post).
// Build the customer html receipt email
function htmlEmail($lastOrder) {
$theOrder = getOrder($lastOrder);
extract($theOrder);
$htmlReceipt = '';
require_once(LIBRARY_PATH . '/htmlEmail.php');
return $htmlReceipt;
}