Jump to content

Mail::send to array or batch send


connemb

Recommended Posts

I know prestashop uses swift mailer for their e-mails. We have a reminder script that runs every week and generates an array of e-mail addresses. The array is a normal array that looks like this

Array
0 => string '[email protected]'
1 => string '[email protected]'
2 => string '[email protected]'

We are trying to figure out how to create the mailer to use our reminder template to send to each e-mail address. We have already tried inserting the mail:send function into a foreach loop, but apparently it doesn't like this and kicks a Fatal Error: Using $this when not in object context. Any ideas on how to bulk send e-mails from prestashop?

require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');


$emails = Db::getInstance()->executeS('SELECT email FROM '._DB_PREFIX_.'wnewsletter WHERE active = \'0\''); 

$emailArray = array();
foreach ($emails as $email)
{
	foreach ($email as $emails)
	{
		$emailArray[] = $emails;	
	}
}

foreach ($emailArray as $emailSend)
{
	Mail::Send('1','newsletter_remin',Mail::l('Email Reminder','1'),array(),$emailSend,null,null,null,null,null,dirname(__FILE__).'/mails/',false,'1');	
}
Link to comment
Share on other sites

Is this for older Prestashop version ?

If you are use PS v.1.4.x or Ps v.1.5, better create and use controller file.

 

look into your script above, where this file placed ?
If your file is placed like this : /root/modules/modulename/your_file.php

The required/included files should be written like this

include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');

And why you are creating a new array for emails data ?
You can use the result from your previous DB query, var $emails to get email data

/* REMEMBER in Prestashop < v.1.4 the default params for static public function Send() are:
 * $toName = NULL
 * $from = NULL
 * $fromName = NULL
 * $fileAttachment = NULL
 * $modeSMTP = NULL
 * $templatePath = _PS_MAIL_DIR_
 * AND param $templateVars is an array for email content
 */

foreach ($emails as $row)
   Mail::Send($id_lang, $template, $subject, $templateVars, $row['email'], $toName, $from, $fromName, $fileAttachment, $modeSMTP, $templatePath);
  • Like 1
Link to comment
Share on other sites

I do not know why I created a new array, it was a long day haha thanks for pointing that out! I tried the code above and it seemed to work. I don't know how I overlooked this, I must of just been putting the Mail Send function into an area it didn't like.

 

Thanks  :)

Edited by connemb (see edit history)
Link to comment
Share on other sites

  • 3 years later...

Good afternoon, I am developing a module in prestashop where I send emails to my clients with attachments, but I can not attach more than one file to an array.pdf file. I am exposing my doubts here because I noticed that they are also having problems with the array in the function Mail :: Send. And someone can give me some tip thanks.

Thanks.

 

Code:

 

if(!isset($_FILES) or !($_FILES > 0))

{

$this->_error .= $this->displayError($this->l('Invalid file'));

}

else {

for($i=0; $i < count($_FILES['FILE']['name']); $i++)

{

$explode = explode('.', $_FILES['FILE']['name'][$i]);

$ext = end($explode);

if(!in_array($ext, array('pdf','doc', 'docx', 'rar', 'zip')) )

{

$this->_error .= $this->displayError($this->l('Invalid file extension'));

} else {

$ftype[] = $_FILES['FILE']['type'][$i];

$fname[] = $_FILES['FILE']['name'][$i];

$ftmp[] = $_FILES['FILE']['tmp_name'][$i];

$fsize[] = $_FILES['FILE']['size'][$i];

}

}

}

$files = $fname;

for($x=0;$x<count($files);$x++)

{

$tmpFilePath[$x] = $ftmp[$x];

$newFilePath[$x] = dirname(__FILE__ ).'/import/' . $files[$x];

move_uploaded_file($tmpFilePath[$x], $newFilePath[$x]);

$file = fopen($newFilePath[$x],"r");

$data = fread($file,$fsize[$x]);

fclose($file);

$data = $data;

}

$file_attachment = array('content' => $data, 'mime' => 'application/pdf', 'name' => $files);

$res = Mail::Send(

$this->context->language->id, 'recibos',

$subject, array('{message}' => $msg_html, $template_vars = false,),

$to, $to_name, $from, $from_name,

$file_attachment,

null,

$this->local_path.'mails/', null, null, $bcc, null

);

 

I am developing a module in prestashop where I send emails to my clients with attachments, but I can not attach more than one file to an array.pdf file. I am exposing my doubts here because I noticed that they are also having problems with the array in the function Mail :: Send. And someone can give me some tip thanks. Thanks.

Edited by SilviaPimenta (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...