[SOLVED] Storing a compiled message in a database for later

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
majiga
Forum Newbie
Posts: 7
Joined: Mon Jun 11, 2007 9:42 am

[SOLVED] Storing a compiled message in a database for later

Post by majiga »

Hi everyone,

I'm building a system that will eventually send lots of emails, so I've split the transmission stuff in to two parts; the first part stores the email (so that it doesn't have to be regularly rebuilt) and a list of the people to whom it should be sent, and then the second part is a function that'll get called from cron regularly, which goes through and retrieves the email and pulls out batches of people and sends it to them.

however, I'm having trouble storing and retrieving the actual "compiled" email to a mysql database.

in the first function, I:

Create the new message:

Code: Select all

$message =& new Swift_Message($emailtitle);
build the html and plain text versions of the email and attach them as parts to $message

and then to get the $message object in to the database, i've tried serializing/unserializing whilst adding/stripping slashes, I've also tried base64 encoding/decoding the serialised object. the second function has thus far always failed to properly load it (more on that later)

I've noticed (with phpmyadmin) that the field that this gets stored to in the database never ends up with any slashes in it, even when i use addslashes to make it safe to go in to the database...

Then, in the second function which loops and actually transmits the emails, I initialise swiftmailer:

Code: Select all

Swift_Errors::expect($e, "Swift_Exception");
Open a new SMTP connection and authenticate and then base64 decode or stripslashes (depending on which ever i've been using to debug this thing with! ;) ), unserialize and then

Code: Select all

if ($swift->send($message, new Swift_Address($email,"$firstname $lastname"), new Swift_Address($this->smtp_from,$this->smtp_from_name)))
send it


I've tried all different sorts of combinations of adding and stripping slashes, serializing/unserializing, base64 enc/dec etc; different orders (ie serializing before adding slashes; or adding slashes before serializing) and nothing works

I either get

Code: Select all

Notice: Swift::send expects parameter 1 to be instance of Swift_Message. in C:\Program Files\AppServ\www\snip\inc\swiftmailer\lib\Swift.php on line 381
in which case var_dump tells me "bool(false)"

or I get

Code: Select all

Fatal error: Unknown(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition swift_message_headers of the object you are trying to operate on was loaded _before_ the session was started in C:\Program Files\AppServ\www\snip\inc\swiftmailer\lib\Swift\Message\Mime.php on line 242
in which case var_dump shows what seems to be the stored email

(on that point; can anyone please tell me why when get this particular error message and then var_dump the retreived $message, it actually seems to output the email about 28 times? the stored version of it in the database doesn't have it stored like that, and there's no loop causing it to come out 28 times)

i've stored arrays to a database with serialize() before; what am I missing here?

thankyou so much!!
Last edited by majiga on Sat Jun 30, 2007 10:34 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

PHP4 is not very good at serializing objects. I'd see it better to store the body and information in the headers then build the email at runtime.

FYI, emails in Swift are never rebuilt once they've been sent for the first time. Swift caches the result itself.
majiga
Forum Newbie
Posts: 7
Joined: Mon Jun 11, 2007 9:42 am

Post by majiga »

ah damn. thanks for the speedy reply!

it'd make this thing heaps easier (because i'm attaching images to the message object via other functions..) if I could just manage to serialize the object.. is there any work around I can try that might get this to work?

i've tried moving and removing session_start(); with no luck (as the error message indicates), ...is there any way to "Ensure that the class definition swift_message_headers of the object we are trying to operate on is loaded _before_ the session is started ...." ;)

thankyou!
majiga
Forum Newbie
Posts: 7
Joined: Mon Jun 11, 2007 9:42 am

Post by majiga »

hey, i just figured it out

i found

http://lists.horde.org/archives/nag/Wee ... 00407.html

which said "Did you clear the session first?"

i had already tried not creating the session, so i just tried putting in

Code: Select all

session_unset();
session_destroy();
before i require_once'd the swiftmailer stuff and it worked!

:)

thanks for your help!
Post Reply