[SOLVED] adding headers and missing autoFlush()

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
audrey
Forum Newbie
Posts: 12
Joined: Sat Mar 03, 2007 2:19 pm

[SOLVED] adding headers and missing autoFlush()

Post by audrey »

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: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've been looking through the docs and am unsure of how to add some extra headers. I see that there's a Swift_Message_Headers class, but how do i use it? I mean, i see the add() method, but how should i go about instantiating the class? And, once i do, how does it attach to the message? The docs for the Message class say this about the attach() method:

"Any descendant of Swift_Message_Mime can be added safely"

but Swift_Message_Headers isn't a descendant.

Also, in bringing my code up to date with ver. 3 i can't find anything analogous to the autoFlush(). Is this no longer necessary? Or is there some other mechanism available? I'm using the AntiFlood plugin. This is how i've refactored so far:

...

Code: Select all

try
{
	$swift = new Swift(new Swift_Connection_SMTP('localhost'));
	$swift->attachPlugin(new Swift_Plugin_AntiFlood(100, 10), 'anti-flood');
	
	set_time_limit(0);
	ignore_user_abort();
	flush();
	
	/* Old code. How does this change? */
	$swift->autoFlush(FALSE);
	
	$message = new Swift_Message(EMAIL_SUBJECT_NEWSLETTER);
	
	$message->setCharset('utf-8');
	$message->setReplyTo(EMAIL_NEWSLETTER);
	$message->setReturnPath(EMAIL_WEBMASTER);

	/* i'd like to add some other other headers here somewhere */
		
	$message->attach(new Swift_Message_Part($plain_content));
	$message->attach(new Swift_Message_Part($html_content, 'text/html'));
	
	...
}
catch etc.

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: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The page you're probably looking for is this:

http://www.swiftmailer.org/wikidocs/v3/ ... ge_headers

Basically, for any MIME part, message, attachment etc you can call:

Code: Select all

$object->headers->set("Foo", "bar");
//Foo: bar

//and

$object->headers->setAttribute("Foo", "whatever", "stressed-out");
//Foo: bar; whatever=stressed-out
EDIT | Oh, and for auto-flush, no it's not necessary. Just make sure to re-use ONE message object... don't keep creating one in a loop. The message object is mutable, you can change its values on each loop.
audrey
Forum Newbie
Posts: 12
Joined: Sat Mar 03, 2007 2:19 pm

Post by audrey »

That's embarrassing. I'd been all over the docs and the source and i missed it (headers) in both places.

Thanks for the quick response.
Post Reply