weird behavior with blank/null content type

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
gmorehoudh
Forum Commoner
Posts: 50
Joined: Tue Mar 04, 2008 1:49 pm

weird behavior with blank/null content type

Post by gmorehoudh »

A couple observations:

1. Sending in a blank string for content type passes it through into the header, producing for example "Content-Type: ; charset=iso-8859-1". It might be nice to have a check for this.

2. Setting a null content type appears to make the constructor blow up. I wouldn't bet my life on it but I think that's what's going on here:

My offending code, written in hopes of squashing the blank charset in header behavior above by replacing any blank strings with null:

Code: Select all

$message =& new Swift_Message($subject, $body, ($content_type ? $content_type : null), ($content_transfer_encoding ? $content_transfer_encoding : null));
The result:

Code: Select all

Swift_Message_MimeException: Cannot set attribute 'charset' for header 'Content-Type' as the header does not exist. Consider using Swift_Message_Headers->has() to check.
Log Information
 
++ Log level changed to 3
 
in /Library/WebServer/snip/inc/swift/Swift/Message/Headers.php on line 279
Call Stack
#   Time    Memory  Function    Location
1   0.0064  149232  {main}( )   ../imap_slurp.php:0
2   11.1301 1706080 Swift_Message->__construct( )   ../imap_slurp.php:331
3   11.1379 1972792 Swift_Message->setCharset( )    ../Message.php:92
4   11.1379 1973088 Swift_Message_Headers->setAttribute( )  ../Message.php:385
Variables in local scope (#4)
 
$name = string 'charset' (length=7)
 
$value = null
 
$lheader = string 'content-type' (length=12)
 
$header = string 'Content-Type' (length=12)
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: weird behavior with blank/null content type

Post by Chris Corbyn »

Hmm... that is odd. It appears the content type header has been set to NULL (which is effectively an unset of it). Therefore, the charset cannot be set on it since it doesn't exist.

I have to admit, the code in version 3 is a bit fiddly to figure out when Swift will apply a default value and when it will just use the value which has been set (effectively if a value is NULL swift applies it's own default where possible). Roll on v4..... (seems to be taking a lifetime to get it out of the door!).

Looking at the code in my constructor, Swift first sets the content-type, then sets the charset. If content-type is NULL then it will fail (rightly so.... there's no such thing as a NULL content-type).

Is this issue only happening when the content type is null?
gmorehoudh
Forum Commoner
Posts: 50
Joined: Tue Mar 04, 2008 1:49 pm

Re: weird behavior with blank/null content type

Post by gmorehoudh »

You know, I'm not sure. I replaced my lovely ternary operator construct with

Code: Select all

       if(!$content_type && !$content_transfer_encoding)
            $message =& new Swift_Message($subject, $body);
        elseif(!$content_type && $content_transfer_encoding)
            $message =& new Swift_Message($subject, $body, null, $content_transfer_encoding);
        elseif($content_type && !$content_transfer_encoding)
            $message =& new Swift_Message($subject, $body, $content_type);
        else
            $message =& new Swift_Message($subject, $body, $content_type, $content_transfer_encoding);
This works in my tests, however I'm not sure every case has been tested.

BTW, I think the default behavior for a null content type should definitely be "treat it as if it was never set and perform accordingly" and in my OPINION, a blank string should do the same. What's happening here is the constructor is trying to set the default charset and failing (I never told it a charset).
Post Reply