Hi guys,
I'm new to phpbuilder and PHP coding, and sorry that my first post is a request. But I have looked everywhere for help. I've been trying to solve this problem for days, still no luck,
I'm trying to use PHPmailer to sent out bulk emails, i have no problems on sending out emails, but on my recipients side the body message will build up.
for example; if I sent a email to David, John and Cathy with the follow body
Hi {username}, testing.
David will receive the body message as
Hi David, testing.
John will receive the body message as
Hi David, testing. Hi John, testing.
Cathy will receive the body message as
Hi David, testing. Hi John, testing. Hi Cathy testing.
I did not wrote these code, but i've been trying to edit it. and still no use.
function _sendBulkEmail($data, $delay = 100){
if(!empty($data)){
// If the to is array then loop through recipient
if(is_array($data['to'])){
$recipients = $data['to'];
// Loop through recipient
foreach($recipients as $recipient){
// Trim the data, remove the whitespace
$data['to'] = trim($recipient);
// Send the email
$this->_sendEmail($data);
// Reseting email before sending again
$this->Mailer->ClearAllRecipients();
$this->Mailer->ClearAddresses();
$this->Mailer->ClearAttachments();
// Delay the email sending
usleep($delay);
}
}else{
// Put in to temporary variable
$recipients = $data['to'];
// Split up the recipient in case user enter
// the recipient as comma separated value
$recipients = preg_split('/[\s,]/', $recipients);
// Loop through recipient after split
foreach($recipients as $key => $recipient){
// Remove the whitespace from recipient address, if any
$recipients[$key] = trim($recipient);
}
// Put back the recipient as an array
$data['to'] = $recipients;
// Recursive call
$this->_sendBulkEmail($data);
}
}else{
return false;
}
}
/**
* Function to send email
*
* @param array $data An array containing smtp parameter including body
* @return boolean Return true if success, false otherwise
*/
function _sendEmail($data) {
if(!empty($data)) {
if($this->Mailer){
$emailConfigurations = Configure::read('Email');
if(!empty($emailConfigurations['Host'])){
$this->Mailer->Host = $emailConfigurations['Host'];
}else{
$this->log('_sendMail(), Host parameter required');
return false;
}
if(!empty($emailConfigurations['Port'])){
$this->Mailer->Port = $emailConfigurations['Port'];
}else{
$this->log('_sendMail(), Port parameter required');
return false;
}
// Is this mailer using smtp
if(!empty($emailConfigurations['IsSMTP'])){
$this->Mailer->IsSMTP();
if(!empty($emailConfigurations['Username'])){
$this->Mailer->Username = $emailConfigurations['Username'];
}else{
$this->log('_sendMail(), Username parameter required if using SMTP');
return false;
}
if(!empty($emailConfigurations['Password'])){
$this->Mailer->Password = $emailConfigurations['Password'];
}else{
$this->log('_sendMail(), Password parameter required if using SMTP');
return false;
}
}
if(!empty($emailConfigurations['SMTPAuth'])){
$this->Mailer->SMTPAuth = $emailConfigurations['SMTPAuth'];
}
if(!empty($emailConfigurations['SMTPSecure'])){
$this->Mailer->SMTPSecure = $emailConfigurations['SMTPSecure'];
}
if(!empty($emailConfigurations['WordWrap'])){
$this->Mailer->WordWrap = $emailConfigurations['WordWrap'];
}
if(!empty($emailConfigurations['From'])){
$this->Mailer->From = $emailConfigurations['From'];
}else{
$this->log('_sendMail(), From parameter required');
return false;
}
if(!empty($emailConfigurations['FromName'])){
$this->Mailer->FromName = $emailConfigurations['FromName'];
}else{
$this->log('_sendMail(), From name parameter required');
return false;
}
if(!empty($data['subject'])){
$this->Mailer->Subject = $data['subject'];
}else{
$this->log('_sendMail(), Subject parameter required');
return false;
}
if(!empty($data['to'])){
if(is_array($data['to'])){
foreach($data['to'] as $recipient){
if(!empty($recipient['name'])){
$this->Mailer->AddAddress($recipient['to'], $recipient['name']);
}else{
$this->Mailer->AddAddress($recipient['to']);
}
}
}else{
if(!empty($data['name'])){
$this->Mailer->AddAddress($data['to'], $data['name']);
}else{
$this->Mailer->AddAddress($data['to']);
}
}
}else{
$this->log('_sendMail(), To parameter required');
return false;
}
$this->set('data', $data);
$this->autoRender = false;
$this->autoLayout = false;
if(empty($data['layout'])){
$data['layout'] = 'default';
}
if(empty($data['template'])){
$data['template'] = $this->action;
}
if(!empty($emailConfigurations['IsHTML'])){
$this->Mailer->IsHTML($emailConfigurations['IsHTML']);
$this->layout = 'email'.DS.'html'.DS.$data['layout'];
$this->viewPath = 'elements'.DS.'email'.DS.'html';
$bodyHtml = $this->render($data['template']);
$this->Mailer->MsgHTML($bodyHtml);
}else{
$this->layout = 'email'.DS.'text'.DS.$data['layout'];
$this->viewPath = 'elements'.DS.'email'.DS.'text';
$bodyText = $this->render($data['template']);
$this->Mailer->AltBody = $bodyText;
}
if($return = $this->Mailer->Send()) {
return $return;
}else{
$this->log($this->Mailer->ErrorInfo);
return false;
}
}else{
$this->log('_sendMail(), PhpMailer component required.');
return false;
}
}else{
$this->log('_sendMail(), data parameter required.');
return false;
}
}
}
?>
Thanks for your time