Page 1 of 2

help with Template Plugin

Posted: Wed Nov 08, 2006 1:03 am
by tkarven
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, thanks to the author for this great piece of library. Brilliant works. 

I am current writing a simple script, which send an email to all existing member asking to update their profile. Each of the email will contain their existing profile information, which I try to accomplish by utilizing template plug in. However it doesn't works, with no error message or no email send, seems it's terminated when the plug in is called. 

Attached is the codes I write, please suggest, thanks.

Code: Select all

<?php 
	require("swift/Swift.php"); 
	require("swift/Swift/Connection/SMTP.php"); 
	
	// send 50 mails per batch, pause 30 seconds in between batches
	$mailer->loadPlugin(new Swift_Plugin_AntiFlood(50,30));  
	// make the script run until it's finished in the background
	set_time_limit(0); ignore_user_abort();  
	
	$mailer = new Swift(new Swift_Connection_SMTP('smtp.gmail.com', SWIFT_SECURE_PORT, SWIFT_TLS)) or die("error :: failed to connect to email server"); 
	$mailer->authenticate('username@gmail.com', 'password') or die("error :: failed to authenticate user account"); 
	$email = $user = array(); 
	
	$query = "select * from member"; 
	$rr = mysql_query($query) or die("Error :: Failed to execute query"); 
	while($rs = mysql_fetch_array($rr)){
		$email[] = array($rs['name'], $rs['email']);
		$user[] = array(id=>$rs['id'], sc=>$rs['sc'], name=>$rs['name']);
	}
	
	$msg = file_get_contents("txt_template/member.php"); 
	$mailer->addPart($msg, 'text/html'); 
	$mailer->loadPlugin(new Swift_Plugin_Template($user)); 
	
	$mailer->send($email, 'username@gmail.com', 'Please update your contact information'); 
	
	echo "<pre>"; 
	print_r($mailer->transactions); 
	echo "</pre>"; 
	$mailer->close(); 		
	
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Nov 08, 2006 1:06 am
by tkarven
Beside the template plug in problem, email lost is another problem i facing. I tried to send the same email to four accounts which is gmail, yahoo mail, hotmail and my hosting account. Everything seems fine except Yahoo mail, it never receive any of the message. Any advice?

I try to solve this from using gmail server instead of my hosting server, but it never solve the problem.

Thanks in advance.

Posted: Wed Nov 08, 2006 1:10 am
by Chris Corbyn
What does the email body look like?

Also, this code needs some quotes around it ;)

Code: Select all

$user[] = array(id=>$rs['id'], sc=>$rs['sc'], name=>$rs['name']);

//should be

$user[] = array('id'=>$rs['id'], 'sc'=>$rs['sc'], 'name'=>$rs['name']);
For the lost emails, it could be for a number of reasons. These big email providers have many controls against what they would term as junk. Even if your email is not junk they may score it that way. Reasons could be:

Ip Blacklisting (Check *all* servers the email will pass through at http://www.robtex.com/rbls.html )
HTML-only messages. Always send a plain part too.
Short messages. Not always possible but try not to make the body too brief.
Bad use of language in the email. Avoid words relating to large amounts of money, sexual terms, drugs etc
Lack of a SPF record on the originating server.


The one many people are surprised by is when they find their IP is blacklisted for one reason or another.

Posted: Wed Nov 08, 2006 1:12 am
by Chris Corbyn
Oh, just noticed you're realying via Gmail. That won't help if you haven't added your email address to your Gmail account via the Gmail settings since Gmail re-writes the email headers and makes it obvious that you've used them as a relay. This would of course be fine if you are sending from a Gmail address which you do seem to be in this snippet.

Posted: Wed Nov 08, 2006 1:13 am
by Chris Corbyn
:oops: Didn't even notice, but you haven't included the Template plugin file. You need to require() that file for the class to be found.

Posted: Wed Nov 08, 2006 1:18 am
by tkarven
Thanks a lot for the fast reply. Currently trying. Will get back to you soon.

Posted: Wed Nov 08, 2006 1:37 am
by tkarven
Everything is working perfectly now. Thanks D11 for your brilliant library, as well as your brilliant support.

Thanks a lot! Keep up the hard works please :D

Posted: Wed Nov 08, 2006 2:12 am
by tkarven
Hmmmh weird, what is the replacement criteria? Is it will just replace the string with same name as array name within {} blacket?

example like

Code: Select all

$member = array(
      'id'=>1, 'name'=>'my name', 'phone'=>'my phone';
);
It will replace the string {id}, {name}, and {phone} accordingly, am I right?

The weird thing happen is this not happen all the times. E.g. now the name is working fine, but when i add the phone into the array, the "name" might mal-function.

Please advise.

Posted: Wed Nov 08, 2006 4:03 am
by Chris Corbyn
What version of Swift do you have? This was a bug with older versions (bug with the plugin, not with Swift). It's an easy fix and I can send corrected code. What PHP version do you have so I can send the correct code?

/me wonders if the old Template plugin somehow slipped back into the new version

Posted: Wed Nov 08, 2006 7:46 am
by tkarven
My PHP version is

- Local Machine - PHP Version 4.3.11
- Hosting Server - PHP Version 4.4.4

My Swift version is Swift Mailer 2.1.17

Posted: Wed Nov 08, 2006 7:48 am
by tkarven
feyd, sorry about the coding quote. knew it now :idea:

Posted: Wed Nov 08, 2006 8:56 am
by Chris Corbyn
Are you using the Template plugin found in "Swift/Plugins/Template.php" ?

Posted: Wed Nov 08, 2006 9:04 am
by tkarven
yes i am

Posted: Wed Nov 08, 2006 10:45 am
by Chris Corbyn
I'll have to have a look at the code in there later sorry. ~Weirdan provided this ammended version a while back which seemed to work well (note the difference in the naming though):

Code: Select all

<?php
class Swift_Plugin_CustomiseEachEmail
{
    var $pluginName = 'CustomiseEachEmail';
    var $templateVars = array();
    var $swiftInstance;
    var $template = '';
    var $count = 0;

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

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

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

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

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

        $mailBody = $this->template;

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

?>
Save it into "Swift/Plugin/CustomeEachEmail.php" and try using that :)

I'm currently working on an entire re-write of the code so the whole plugin API is changing anyway.

Posted: Wed Nov 08, 2006 7:18 pm
by tkarven
Yes the replacement is working fine now , thanks for it....

Anyway I having another problem now, sorry for been so problematic.

I wrote the code to connect to our SMTP server now and it works well when i run it from my local machine, but not when i run it from the hosting server. Here's the code

Code: Select all

require("swift/Swift.php"); 
	require("swift/Swift/Connection/SMTP.php"); 
	require("swift/Swift/Plugin/CustomiseEachEmail.php"); 
	require("swift/Swift/Plugin/AntiFlood.php"); 

	$mailer = new Swift(new Swift_Connection_SMTP('smtp.mailserver.com')) or die("failed to connect to email server"); 	
	$mailer->authenticate("username@mailserver.com", "password") or die("failed to authenticate user");
The output I got is

Code: Select all

failed to authenticate user
Same coding same server same login, works well when run from local machine, but authentication failed when run from server, how come? After i check with print_r($mailer->transactions), confirmed is authentication problem.

Code: Select all

[response] => 535 Incorrect authentication data