Skip to content

encodeUtf8Word() should prefer ASCII when possible #32

@MircoBabin

Description

@MircoBabin

When sending an email only containing ASCII:

    require_once("./class.simple_mail.php");

    $from = 'me@somewhere.org';

    $email = new SimpleMail();
    $email->setFrom($from, 'Me')
        ->setParameters('-f'.$from)
        ->setReplyTo($from, '')
        ->setTo('...@yahoo.com', 'Someone')
        ->setTo('...@gmail.com', 'Another one')
        ->setSubject('Subject')
        ->setMessage('Check your email.');
    $email->send();

I got the following error in my e-mail Inbox:

host mailfilter.hostnet.nl[91.184.19.251] said: 550
    Subject contains invalid characters. (in reply to end of DATA command)

So I changed the encodeUtf8Word() to:

    /**
     * encodeUtf8Word
     *
     * @param string $value The word to encode.
     *
     * @return string
     */
    public function encodeUtf8Word($value)
    {
        $isAscii = true;
        for ($i=0; $i<strlen($value); $i++) {
            $ch = ord(substr($value, $i, 1));
            if ($ch >= 128) {
                $isAscii = false;
                break;
            }
        }
        
        if ($isAscii) {
            return $value;
        } else {
            return sprintf('=?UTF-8?B?%s?=', base64_encode($value));
        }
    }

After this change, the email sends correctly, no error anymore.

So encodeUt8Word() should prefer plain ASCII, without encoding.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions