Here are some modifications I have made for a few sites. The code needs to be cleaned up and improved, but it does work.
First, use PHPMailer. Notice I also allow an optional attachment to be specified via a TV. Obviously some things need to be moved into configuration variables.
function sendNewsletterMail ($emails,$emailFrom,$newsletterId,$subject) {
global $modx;
$html = sendHTML($newsletterId);
require_once('controls/class.phpmailer.php');
$base_path = $modx->config['base_path'];
$tvs = $modx->getTemplateVarOutput('Attachment',$newsletterId);
$attachment = $base_path . $tvs['Attachment'];
foreach ($emails as $id => $datas) {
$email = $datas['val'];
$timestamp = $datas['timestamp'];
$MD5 = ControlMD5($email,$timestamp);
$link = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?&id='.$modx->TXNewsletters['idPageUnsubscribe'].'&action=del&email='.$email.'&control='.$MD5;
$send_html = preg_replace('#(\{link_unsubscribe\})#', $link, $html);
$headers = 'From: ' . $emailFrom . "\n";
$headers .= 'To: ' . $email . "\n";
$headers .= 'Return-Path: ' . $emailFrom . "\n";
$headers .= 'MIME-Version: 1.0' ."\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' ."\n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "\n\n";
$headers .= $send_html . "\n";
// $result=mail('', $subject,'', $headers);
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->From = $emailFrom;
$mail->FromName = '[Website Name Here]';
$mail->Subject = $subject;
$mail->Body = $send_html;
$mail->AddAddress($email);
if($attachment != $base_path) {
$mail->AddAttachment($attachment);
}
if(!$mail->send())
echo $mail->ErrorInfo,'<br>';
$result=true;
}
return $result;
}
Next is some tweaks to get a plain newsletter. I wanted the newletters to appear on the website with the normal site template, only the content (and an unsubscribe link) to be sent in the emails. This code is a little tricky...would be nice to get some input on it from someone more familiar with the parser (perhaps this is a very bad idea!)
function sendHTML ($newsletterId) {
global $modx;
$modxout = new DocumentParser;
$modxout->documentIdentifier=$newsletterId;
$modxout->documentMethod= 'id';
$modxout->documentObject= $modxout->getDocumentObject($modxout->documentMethod, $modxout->documentIdentifier);
$modxout->documentName= $modxout->documentObject['pagetitle'];
$modxout->documentContent= '<h3>[!Title!]</h3>[*content*]<p><a href="{link_unsubscribe}">unsubscribe</a></p>'; // use blank template
$modxout->documentContent= $modxout->parseDocumentSource($modxout->documentContent);
ob_start();
ob_start();
$modxout->outputContent();
$html = ob_get_contents();
ob_end_clean();
/*
ini_set('default_socket_timeout', 120);
$url = 'http://'.$_SERVER['HTTP_HOST'].$modx->makeUrl($newsletterId);
$html = file_get_contents($url);
*/
$httpDom = 'http://'.$_SERVER['HTTP_HOST'];
// $httpDom = $modx->config['base_url'];
$html = preg_replace('#(<img[^>]*src=\")([^\"]*/?assets/)#', '${1}'.$httpDom.'/assets/', $html);
$html = preg_replace('#(<a[^>]*href=\")([^\"]*/?assets/)#', '${1}'.$httpDom.'/assets/', $html);
$html = preg_replace('#(<a[^>]*href=\")([^\"]*/?index\.php)#', '${1}'.$httpDom.'/index.php', $html);
$html = preg_replace('#(onclick=\"window\.open\(\')([^\"]*/?assets/)#', '${1}'.$httpDom.'/assets/', $html);
return $html;
}
Unfortunately too busy to fix up this module myself, so I thought I would share some of my modifications here for whomever has a little more time to put together the 1.0 version of TX Newsletters.