Page 1 of 1

[SOLVED] Clear body

Posted: Wed Aug 01, 2007 4:07 am
by Kaitlyn2004
I send out a personalized message to each person and have my own method for personalizing the data...

for text e-mails I use:

Code: Select all

$message->setBody();
for HTML I use:

Code: Select all

$message->setBody();
$message->setContentType('text/html');
for text+HTML I use:

Code: Select all

$message->attach('text content');
$message->attach('html content', 'text/html'):

will just re-calling setBody each time reset the body of the e-mail? What about the multipart, where you attach? I don't want to keep attaching sections...

Thanks!

Posted: Wed Aug 01, 2007 5:49 am
by Chris Corbyn
setBody() has been deliberately designed to act a little unexpectedly when you've got parts attached. It won't remove your parts, all it will do is set some text that appears before the MIME data (where you'd usually have a message saying "Sorry your mail client doesn't support MIME 1.0, please upgrade your mail client.". Effectively it becomes the fallback for really ancient mail clients.

If you need to remove the parts you've attached you'll have to use the detach() method:

Code: Select all

$part1 =& new Swift_Message_Part("xyz");
$id1 = $message->attach($part1);
$part2 =& new Swift_Message_Part("abc");
$id2 = $message->attach($part2);

$message->detach($id1);
//Now $message only contains $part2
After typing that I thing I might also change it to allow simply this, more like a DOM.

Code: Select all

$message->detach($part1);

Posted: Wed Aug 01, 2007 8:36 am
by Kaitlyn2004
Ugh!

Now I'm getting:
Fatal error: Call to a member function getEncoding() on a non-object in Swift\Message\Mime.php on line 232

Code: Select all

$mail =& new Mailer();
$message =& $mail->message;
$message->setSubject($subject);
$message->setFrom(new Swift_Address($fromEmail, $fromName));
$message->setReplyTo(new Swift_Address($fromEmail, $fromName));
$message->setCharset($charSet);
$message->setPriority($priority);

$message->headers->set('X-Message-Id', 'my message id'));

if ($format == 0 || $format == 2) {
	$text =& new Swift_Message_Part(parseUniqueMsgCodes($textContent, $subInfo, $sendId, MSG_TYPE_NORMAL));
	$text = $message->attach($text);
}
else if ($format == 1 || $format == 2) {
	$html =& new Swift_Message_Part(parseUniqueMsgCodes($tmp_htmlContent, $subInfo, $sendId, MSG_TYPE_NORMAL), 'text/html');
	$html = $message->attach($html);
}

if (!$mail->mailer->send($message, $email, [from])) { echo 'failed' }

if (isset($text)) $message->detach($text);
if (isset($html)) $message->detach($html);
(in the Mailer class)

Code: Select all

$this->mailer =& new Swift(new Swift_Connection_SMTP([private], [private]));
$this->message =& new Swift_Message();
I don't see any problems with reference there?

Posted: Wed Aug 01, 2007 8:40 am
by Kaitlyn2004
The problem is with the attaching part, because replacing the $format checks with:

Code: Select all

$message->setBody('this is a test, alright?');
works

Posted: Wed Aug 01, 2007 8:40 am
by Chris Corbyn
This:

Code: Select all

$text = $message->attach($text);
attach() returns a string, but $text is a reference to an object before that happens.

Posted: Wed Aug 01, 2007 8:49 am
by Kaitlyn2004
d11wtq wrote:This:

Code: Select all

$text = $message->attach($text);
attach() returns a string, but $text is a reference to an object before that happens.
Yay! Worked!

butt... it should use the $text in attach() before assigning it to the new $text...?

If I have JUST text to send, then it sends it as a MIME message, as makes sense. So in this case would I use the setBody() method on a plain text message, or what?

and then I guess above that, I would need to check if I am attaching anything... if not, use setBody() or whatever u say, but if attach, then I need to use the _Mesage_Part() method, right?

Posted: Thu Aug 02, 2007 3:01 am
by Chris Corbyn
Kaitlyn2004 wrote:butt... it should use the $text in attach() before assigning it to the new $text...?
That's what does happen, but the error comes later because Swift then does some other things to the object when it comes to building the entire message structure. In PHP5 I think your code would have worked (sans the explicit reference operator). In PHP4, because you explictly create a reference it's treated a little differently ;)

Posted: Thu Aug 02, 2007 3:06 am
by Chris Corbyn
Oh, forgot to mention, you can keep the parts in variables and re-use the same parts repeatedly with different bodies if you want. You can still attach/detach them but it's probably good to keep them handy from an optimization point of view.

Code: Select all

$part =& new Swift_Message_Part("XYZ");
$id = $message->attach($part); //Message is multipart
$message->detach($id);
$message->setBody("XYZ"); //Message is just plain
$message->setBody("");
$part->setBody("ABC");
$message->attach($part); //Message is multipart again