array() problem sending mass email with swiftmailing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post by kigoobe »

Hi Chris

Well, two things, if you could help. In one of the examples given in the doc of your swiftmailer website, I have found you to suggest >

Code: Select all

$connection = new Swift_Sendmail_Connection;
In this example, you are suggesting

Code: Select all

$connection = new Swift_SMTP_Connection;
I would have liked to know, which one I should follow. I guess one of these two is a typo error, right?

Besides, the code of that part is now

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
$recipients[] = array($row['name'].' '.$row['surname'], $row['email']);
$subj1 = '<p>Hi '.$row['name'].'</p>'.$themessage;
}
Could you please tell how should I incorporate the the following in my code?

Code: Select all

$tpl = array ( 
 array ('foo' => 'First email'), 
 array ('foo' => 'second email'), 
 array ('foo' => 'third email') 
);
Thanks a million times, once again.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I guess one of these two is a typo error, right?
No, they are just different transport subsystems used by Swift.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$tpl = array();
while ($row = mysql_fetch_assoc($result)) {
  $recipients[] = array($row['name'].' '.$row['surname'], $row['email']);
  $tpl[] = array('name' => $row['name'] . ' ' . $row['surname']);
}
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post by kigoobe »

Thanks Weirdan, Well after following you guys' suggestions, I am finding the following:

Code: Select all

Fatal error: Argument 1 passed to Swift::loadPlugin() must implement interface Swift_IPlugin, called in /basic_sendmail.php on line 49 and defined in /Swift-1.3.1/Swift.php on line 538
... :roll:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

kigoobe wrote:Thanks Weirdan, Well after following you guys' suggestions, I am finding the following:

Code: Select all

Fatal error: Argument 1 passed to Swift::loadPlugin() must implement interface Swift_IPlugin, called in /basic_sendmail.php on line 49 and defined in /Swift-1.3.1/Swift.php on line 538
... :roll:
PHP5 ?

The plugin I posted assumed you're using PHP4 sorry. Just change.

Code: Select all

class Swift_Template_Plugin
{
To

Code: Select all

class Swift_Template_Plugin implements Swift_IPlugin
{
It should get you going unless you have E_STRICT on, in which case you'll want to swap all the "var" keywords to "public".... Like I say, it was knocked up very quickly just to get a small task done for someone else.... I'll write an official one to bundle with future versions of Swift but I have 101 other things to do tonight :(
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post by kigoobe »

Nah, it's still not working ... may be I should give the full code ...

Code: Select all

require('Swift-1.3.1/Swift.php');
require('Swift-1.3.1/Swift/Swift_Sendmail_Connection.php');
require('Swift_Template_Plugin.php'); 

$connection = new Swift_Sendmail_Connection;

$mailer = new Swift($connection, $_SERVER['SERVER_NAME']);

$subject = 'My subject';
$sender = '"My me" <me@gmail.com>';
	$themessage=stripslashes($_POST['themessage']);
	$themessage = preg_replace('#<(.+?)>#ims','<\\1>',$themessage);
	$themessage = eregi_replace('&','&',$themessage); 
	$themessage = eregi_replace('"','"',$themessage); 	
$recipients = array();
$tpl = array();  
$thisnum = 'Cathala';
$result = mysql_query("SELECT name, surname, email from $member_base WHERE groupe = '$groupe' AND surname !='$thisnum'");
while ($row = mysql_fetch_assoc($result)) {
$recipients[] = array($row['name'].' '.$row['surname'], $row['email']);
$tpl[] = array('name' => $row['name']); 
}

$subj1 = '<p>Bonjour {name}</p>'.$themessage;

$mailer->addPart($subj1, 'text/html');

$mailer->loadPlugin(new Swift_Template_Plugin($tpl));  

//If anything goes wrong you can see what happened in the logs
if ($mailer->isConnected())
{
	//Sends a simple email
	$mailer->send(
		$recipients,
		$sender,
		$subject
	);
	//Closes cleanly... works without this but it's not as polite.
	$mailer->close();
	header('Location: mailsend.php'); 
} 
else echo "The mailer failed to connect. Errors: <pre>".print_r($mailer->errors, 1)."</pre><br />
	Log: <pre>".print_r($mailer->transactions, 1)."</pre>";
The error that I'm getting is that now I am having the top most name from my mysql db for all the mails ... :(
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Could be something in Swift by the sound of it.... I did have to tweak a few things to get it working.

No promises but if I get some time later (I *probably* will do) I'll write something a bit more final and make sure it works with the latest release in both PHP4 and 5. I'll just take it to PM in that case to make sure you get going ok ;)
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post by kigoobe »

Thanks a lot Chris. Thats very nice of you. I'll wait for your message.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

OK, I've just tested (without changing) the original one I wrote for PHP5 with the latest version of Swift 1.3.1 right from the website and it works fine:

Code: Select all

<?php

class Swift_Template_Plugin implements Swift_IPlugin
{
        public $pluginName = 'Template';
        private $templateVars = array();
        private $swiftInstance;
        private $template = '';
        private $count = 0;

        //2-dimensional
        // First level MUST be numerically indexed starting at zero
        // Second level contains the replacements
        public function __construct($template_vars=array())
        {
                $this->templateVars = $template_vars;
        }

        public function loadBaseObject(&$object)
        {
                $this->swiftInstance =& $object;
        }

        //Split the headers from the mail body
        private function getTemplate()
        {
                return substr($this->swiftInstance->currentMail[3], strpos($this->swiftInstance->currentMail[3], "\r\n\r\n"));
        }

        private function getHeaders()
        {
                return substr($this->swiftInstance->currentMail[3], 0, strpos($this->swiftInstance->currentMail[3], "\r\n\r\n"));
        }

        public function onBeforeSend()
        {
                if (empty($this->template)) $this->template = $this->getTemplate();

                foreach ($this->templateVars[$this->count] as $key => $replacement)
                {
                        $this->swiftInstance->currentMail[3] = $this->getHeaders().str_replace('{'.$key.'}', $replacement, $this->template);
                        //echo $replacement;
                }
                //echo $this->count++;
        }
}

?>
I'm not going to have time to make the changes I want to make tonight because it's already 10:30pm here but one major limitation with the plugin as stands is that you can't replace anything in the header, only the email body. That will be changed before it's released properly. Try the code above, making sure you're using the version released 28th june (1.3.1) since it's got the correct access control etc for PHP5, not that it should affect anything. Have you echoed out $row['name'] in the loop to double check it's what you expect? :)
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post by kigoobe »

Yeah, I'm using the version 1.3.1 .. i don't understand if it worked for you and and your friend for whom it was initially developped, why it's not working for me? Or because it has something to do with my sendmail.php code ... or may be as I am trying to send an html mail ... very weird !!!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

kigoobe wrote:Yeah, I'm using the version 1.3.1 .. i don't understand if it worked for you and and your friend for whom it was initially developped, why it's not working for me? Or because it has something to do with my sendmail.php code ... or may be as I am trying to send an html mail ... very weird !!!
I tested it with a HTML email too :? Odd. Out of morbid curiosity did you download the .zip or the tarball?

What happens if you send yourself this?

Code: Select all

<?php

require('Swift.php');
require('Swift/Swift_SMTP_Connection.php');

require('Swift_Template_Plugin.php');

//The mailer will now establish a connection with the server
$mailer = new Swift(new Swift_SMTP_Connection('smtp.host.tld'));

$addresses = array(
    '"Chris Corbyn" <YOUR@ADDRESS>',
    '"Chris Corbyn2" <YOUR@ADDRESS>',
    '"Chris Corbyn3" <YOUR@ADDRESS>'
);

$tpl = array(
    array('foo' => 'First email'),
    array('foo' => 'second email'),
    array('foo' => 'third email')
);

$mailer->loadPlugin(new Swift_Template_Plugin($tpl));

$mailer->addPart("Hello Joe it's only me! and this is my {foo}!!!", 'text/html');

//Sends a simple email
$mailer->send(
          $addresses,
          '"Your name" <you@yourdomain.com>',
          'Some Subject'
);

$mailer->close();

?>
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post by kigoobe »

Well, I've downloaded the zip version. I am trying to try the newest code to send this to myself, but for some reason it's not working. I'm testing this since yesterday and have send thousands of emails ... may be there's some cap from my host (dreamhost) ... i think i'll have to try after sometime ... but if we have the same code, and if it's working with you, it must work with me as well .. logically !!! Then what's wrong ... :(

I'll have to wait for sometime I guess as now i am not able to send any mail using my server ...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Chris, where the $this->count variable is incremented? ;)
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post by kigoobe »

Well, my server is working back again, but I could not use Swift_SMTP_Connection as I am getting an error

Code: Select all

Fatal error: Class 'Swift_SMTP_Connection' not found in /test.php on line 9
I then tried this using Swift_Sendmail_Connection. This is working, the emails are going, but the initial problem is not yet solved. I am getting 'this is my First email' for all the mails ... means, the same problem as before ...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

kigoobe wrote:Well, my server is working back again, but I could not use Swift_SMTP_Connection as I am getting an error

Code: Select all

Fatal error: Class 'Swift_SMTP_Connection' not found in /test.php on line 9
I then tried this using Swift_Sendmail_Connection. This is working, the emails are going, but the initial problem is not yet solved. I am getting 'this is my First email' for all the mails ... means, the same problem as before ...
Sounds as though you havn't included the Swift_SMTP_Connection.php file.

NOTE: Swift_SMTP_Connection requires at least one constructor parameter to point to the smtp server. http://www.swiftmailer.org/documentation/48#view
Post Reply