Newsletter modules



  • I wish I could get this newsletter module working in TB. It lets you send newsletters right from your site. It works, right up to the point that you tell it to send out a newsletter, then it white screens…

    mdekker EDIT: attachment removed due to copyright


  • Global Moderator

    I dont know about this module, but have you thought about mailchimp? Thirtybees offers a free module for it.



  • It lets you mail out newsletters right from the admin of the shop?


  • Global Moderator

    No. Mailchimp is a special newsletter service. The module is syncinc your customer data and more stuff.



  • I narrowed it down to this file, it needs to utilize the new smarty, not the really old one ps used to use…

    0_1509198557586_NewsMail.php

    which looks like…

    notice the includes on the top aren’t correct, and I’m sure there must be calls into the file that are incorrect too…

    <?php
    
    include_once(dirname(__FILE__).'/../../../tools/swift/Swift.php');
    include_once(dirname(__FILE__).'/../../../tools/swift/Swift/Connection/SMTP.php');
    include_once(dirname(__FILE__).'/../../../tools/swift/Swift/Connection/NativeMail.php');
    include_once(dirname(__FILE__).'/../../../tools/swift/Swift/Plugin/Decorator.php');
    
    class NewsMail
    {
    	const TYPE_HTML = 1;
    	const TYPE_TEXT = 2;
    	const TYPE_BOTH = 3;
    
    	public static function Send($id_lang, $template, $subject, $template_vars, $to,
    		$to_name = null, $from = null, $from_name = null, $file_attachment = null, $mode_smtp = null,
    		$template_path, $die = false, $id_shop = null, $bcc = null, $configuration = null)
    	{
    	if($configuration == null) {
    		$configuration = Configuration::getMultiple(array(
    			'PS_SHOP_EMAIL',
    			'PS_MAIL_METHOD',
    			'PS_MAIL_SERVER',
    			'PS_MAIL_USER',
    			'PS_MAIL_PASSWD',
    			'PS_SHOP_NAME',
    			'PS_MAIL_SMTP_ENCRYPTION',
    			'PS_MAIL_SMTP_PORT',
    			'PS_MAIL_TYPE',
    			'PS_MAIL_DOMAIN'
    		), null, null, $id_shop);
    	}		
    
    		if (!isset($configuration['PS_MAIL_SMTP_ENCRYPTION']))
    			$configuration['PS_MAIL_SMTP_ENCRYPTION'] = 'off';
    		if (!isset($configuration['PS_MAIL_SMTP_PORT']))
    			$configuration['PS_MAIL_SMTP_PORT'] = 'default';
    
    
    		//Verify the Return-Path	
    		if (!isset($address))
    			$address = null;
    
    		if (!is_array($template_vars))
    			$template_vars = array();
    
    
    		/* Construct multiple recipients list if needed */
    		$to_list = new Swift_RecipientList();
    		$to_plugin = $to;
    		$to_list->addTo($to, $to_name);
    		$to = $to_list;
    		
    		try {
    			/* Connect with the appropriate configuration */
    			if ($configuration['PS_MAIL_METHOD'] == 2)
    			{
    				if (empty($configuration['PS_MAIL_SERVER']) || empty($configuration['PS_MAIL_SMTP_PORT']))
    				{
    					die('Error: invalid SMTP server or SMTP port');
    				}
    				$connection = new Swift_Connection_SMTP($configuration['PS_MAIL_SERVER'], $configuration['PS_MAIL_SMTP_PORT'],
    					($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl') ? Swift_Connection_SMTP::ENC_SSL :
    					(($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls') ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF));
    				$connection->setTimeout(4);
    				if (!$connection)
    					return false;
    				if (!empty($configuration['PS_MAIL_USER']))
    					$connection->setUsername($configuration['PS_MAIL_USER']);
    				if (!empty($configuration['PS_MAIL_PASSWD']))
    					$connection->setPassword($configuration['PS_MAIL_PASSWD']);
    			}
    			else
    				$connection = new Swift_Connection_NativeMail();
    
    			if (!$connection)
    				return false;
    			$swift = new Swift($connection, $configuration['PS_MAIL_DOMAIN']);
    			/* Get templates content */
    			$template_html = file_get_contents(dirname(__FILE__).'/../mails/newsletter.html');
    			$template_txt = strip_tags(html_entity_decode(file_get_contents(dirname(__FILE__).'/../mails/newsletter.txt'), null, 'utf-8'));
    
    			$message = new Swift_Message($subject);
    
    			$message->setCharset('utf-8');
    
    			/* Set Message-ID - getmypid() is blocked on some hosting */
    			$message->setId(NewsMail::generateId());
    /* set a return-path to manage bounces feedbacks*/
    //			if($address != null)
    //				$message->headers->set("Return-Path", $address);
    			$message->headers->setEncoding('Q');
    
    
    
    			$swift->attachPlugin(new Swift_Plugin_Decorator(array($to_plugin => $template_vars)), 'decorator');
    			if ($configuration['PS_MAIL_TYPE'] == NewsMail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == NewsMail::TYPE_TEXT)
    				$message->attach(new Swift_Message_Part($template_txt, 'text/plain', '8bit', 'utf-8'));
    			if ($configuration['PS_MAIL_TYPE'] == NewsMail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == NewsMail::TYPE_HTML)
    				$message->attach(new Swift_Message_Part($template_html, 'text/html', '8bit', 'utf-8'));
    
    			/* Send mail */
    			$send = $swift->send($message, $to, new Swift_Address($from, $from_name));
    			$swift->disconnect();
    			
    
    			return $send;
    		}
    		catch (Swift_Exception $e) {
    			echo $e;
    			return false;
    		}
    	}
    
    	/* Rewrite of Swift_Message::generateId() without getmypid() */
    	protected static function generateId($idstring = null)
    	{
    		$midparams =  array(
    			"utctime" => gmstrftime("%Y%m%d%H%M%S"),
    			"randint" => mt_rand(),
    			"customstr" => (preg_match("/^(?<!\\.)[a-z0-9\\.]+(?!\\.)\$/iD", $idstring) ? $idstring : "swift") ,
    			"hostname" => (isset($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : php_uname("n")),
    		);
    		return vsprintf("<%s.%d.%s@%s>", $midparams);
    	}
    
    }
    


  • I think I have it, except a couple lines…

    what should this be changed to?

    	$to_list = new Swift_RecipientList();


  • i will also recommend mailchimp for newsletters. very easy, nice templates. you can even setup mails for abandoned carts very easy.
    also tracking, opened, unopened, bounced etc is easy to track… … i should give it a try! its much more professional then the most modules ive seen for this purpose


  • Global Moderator

    @are you using the tb module?



  • i have tested this on a thirtybees webshop with the tb module and it works.
    but i also use this on my mainwebshop which is still on prestashop(waiting till all my needs are compatible with thirtybees) and it works fabulous, i use the module of businesstech on that webshop


  • administrators

    I narrowed it down to this file, it needs to utilize the new smarty, not the really old one ps used to use…

    It needs a whole lot more than that. The module was written for PrestaShop 1.5 and with some tricks could be made working on 1.6.0.x. It seems that the free module is no longer being maintained and has turned into a paid one.



  • Right it is now a paid module, the old free one, that I have, works on old PS 1.6.0.5 or so, before they changed the Swift package. Ive got it almost working, right upto the point you actually send the emails. grr…


  • administrators

    Really? :O

    I updated the SwiftMailer library of PrestaShop back then, maybe the exact commit can help you: https://github.com/PrestaShop/PrestaShop/commit/85e602549f8525bf61b28734a71f3ee65a111eb3



  • Then you’re the perfect person to help me replace that one line of code I posted above… :)


  • administrators

    It’s not just one line, I think. It is about every single line that interacts with SwiftMailer. It will be easier to include an older version of SwiftMailer with the module and require that instead, though I’m not sure it that will conflict.



  • I’ve fixed, I think, all other lines. Yes, if you try to include an old version of Swift, it simple says you cant redefine Swift



  • Here’s my new code…except that line, line 68 maybe…

    <?php
    
    // include_once(dirname(__FILE__).'/../../../tools/swift/Swift.php');
    // include_once(dirname(__FILE__).'/../../../tools/swift/Swift/Connection/SMTP.php');
    // include_once(dirname(__FILE__).'/../../../tools/swift/Swift/Connection/NativeMail.php');
    // include_once(dirname(__FILE__).'/../../../tools/swift/Swift/Plugin/Decorator.php');
    // include_once(_PS_SWIFT_DIR_.'swift_required.php');
    
    class NewsMail
    {
    	const TYPE_HTML = 1;
    	const TYPE_TEXT = 2;
    	const TYPE_BOTH = 3;
    
    	public static function Send(
    		$id_lang,
    		$template,
    		$subject,
    		$template_vars,
    		$to,
    		$to_name = null,
    		$from = null,
    		$from_name = null,
    		$file_attachment = null,
    		$mode_smtp = null,
    		$template_path,
    		$die = false,
    		$id_shop = null,
    		$bcc = null,
    		$configuration = null)
    	{
    	if($configuration == null) {
    		$configuration = Configuration::getMultiple(array(
    			'PS_SHOP_EMAIL',
    			'PS_MAIL_METHOD',
    			'PS_MAIL_SERVER',
    			'PS_MAIL_USER',
    			'PS_MAIL_PASSWD',
    			'PS_SHOP_NAME',
    			'PS_MAIL_SMTP_ENCRYPTION',
    			'PS_MAIL_SMTP_PORT',
    			'PS_MAIL_TYPE',
    			'PS_MAIL_DOMAIN'),
    			null,
    			null,
    			$id_shop);
    	}		
    
    	if (!isset($configuration['PS_MAIL_SMTP_ENCRYPTION']) || Tools::strtolower($configuration['PS_MAIL_SMTP_ENCRYPTION']) === 'off') {
    		$configuration['PS_MAIL_SMTP_ENCRYPTION'] = false;
    	}
    	if (!isset($configuration['PS_MAIL_SMTP_PORT'])) {
    		$configuration['PS_MAIL_SMTP_PORT'] = 'default';
    	}
    
    
    		//Verify the Return-Path	
    		if (!isset($address)) {
    			$address = null;
    		}
    
    		if (!is_array($template_vars)) {
    			$template_vars = array();
    		}
    
    
    		/* Construct multiple recipients list if needed */
    		$to_list = new Swift_RecipientList();
    		$to_plugin = $to;
    		$to_list->addTo($to, $to_name);
    		$to = $to_list;
    		
    		try {
    			/* Connect with the appropriate configuration */
    			if ($configuration['PS_MAIL_METHOD'] == 2) {
    				if (empty($configuration['PS_MAIL_SERVER']) || empty($configuration['PS_MAIL_SMTP_PORT'])) {
    					die('Error: invalid SMTP server or SMTP port');
    				}
    				$connection = Swift_SmtpTransport::newInstance($configuration['PS_MAIL_SERVER'], $configuration['PS_MAIL_SMTP_PORT'], $configuration['PS_MAIL_SMTP_ENCRYPTION']);
    				$connection->setTimeout(4);
    				if (!$connection)
    					return false;
    				if (!empty($configuration['PS_MAIL_USER']))
    					$connection->setUsername($configuration['PS_MAIL_USER']);
    				if (!empty($configuration['PS_MAIL_PASSWD']))
    					$connection->setPassword($configuration['PS_MAIL_PASSWD']);
    			} else {
    				$connection = Swift_MailTransport::newInstance();
    			}
    
    			if (!$connection) {
    				return false;
    			}
    
    			$swift = Swift_Mailer::newInstance($connection);
    			/* Get templates content */
    			$template_html = file_get_contents(dirname(__FILE__).'/../mails/newsletter.html');
    			$template_txt = strip_tags(html_entity_decode(file_get_contents(dirname(__FILE__).'/../mails/newsletter.txt'), null, 'utf-8'));
    
    			$message = new Swift_Message($subject);
    
    			$message->setCharset('utf-8');
    
    			/* Set Message-ID - getmypid() is blocked on some hosting */
    			$message->setId(NewsMail::generateId());
    /* set a return-path to manage bounces feedbacks*/
    //			if($address != null)
    //				$message->headers->set("Return-Path", $address);
    			$message->headers->setEncoding('Q');
    
                $template_vars = array_merge($template_vars, $extra_template_vars);
                $swift->registerPlugin(new Swift_Plugins_DecoratorPlugin(array($to_plugin => $template_vars)));
                if ($configuration['PS_MAIL_TYPE'] == NewsMail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == NewsMail::TYPE_TEXT) {
                    $message->addPart($template_txt, 'text/plain', 'utf-8');
                }
                if ($configuration['PS_MAIL_TYPE'] == NewsMail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == NewsMail::TYPE_HTML) {
                    $message->addPart($template_html, 'text/html', 'utf-8');
                }
    
    			/* Send mail */
    			$send = $swift->send($message, $to, new Swift_Address($from, $from_name));
    			$swift->disconnect();
    			
    
    			return $send;
    		}
    		catch (Swift_Exception $e) {
    			echo $e;
    			return false;
    		}
    	}
    
    	/* Rewrite of Swift_Message::generateId() without getmypid() */
    	protected static function generateId($idstring = null)
    	{
    		$midparams =  array(
    			"utctime" => gmstrftime("%Y%m%d%H%M%S"),
    			"randint" => mt_rand(),
    			"customstr" => (preg_match("/^(?<!\\.)[a-z0-9\\.]+(?!\\.)\$/iD", $idstring) ? $idstring : "swift") ,
    			"hostname"  => ((isset($_SERVER["SERVER_NAME"]) && !empty($_SERVER["SERVER_NAME"])) ? $_SERVER["SERVER_NAME"] : php_uname("n")),
    		);
    		return vsprintf("<%s.%d.%s@%s>", $midparams);
    	}
    
    }
    

    0_1509247913427_NewsMail.php


  • administrators

    What is the difference with the 1.6.1.5+ mailer? Doesn’t it just want to inject its own template?

    96. /* Get templates content */
    97. $template_html = file_get_contents(dirname(__FILE__).'/../mails/newsletter.html');
    98. $template_txt = strip_tags(html_entity_decode(file_get_contents(dirname(__FILE__).'/../mails/newsletter.txt'), null, 'utf-8'));
    

  • administrators

    So what if you take the original, then restore the lines shown above?



  • The module does use it’s own templates, or is supposed to anyways. The templates arent the issue, its getting the array of recipients, to send the mails off.



  • Hi @SLICK_303,

    My module adapts its Swift calls according to Prestashop versions (before 1.6.1.15 or after)

    You can rewrite the code your way, but there have been several changes since the last free version ^^


Log in to reply
 

Looks like your connection to thirty bees forum was lost, please wait while we try to reconnect.