It would never fail if the string to encode is `unicode` and if we're using `replace`.
See:
```python
In [0]: u'Ḩé£ļö ṫĥè®ə!'.encode('ascii', 'replace')
Out[0]: '????? ?????!'
```

If using `replace` fails then will fail with any other encoding, because the raised error is `UnicodeDecodeError`, not the encoding one. This happens when `body` is a `str`.
So, in your code, it will fall under the `pass` in the second `try/except`, and we'll `write_fd` an `str` `body`... the same result as using the `isinstance` that I proposed above.

Instead of having:
```python
try:
    if isinstance(body, unicode):
        body = body.encode(charset)
except UnicodeError as exc:
    logger.error(
        "Unicode error, using 'replace'. {0!r}".format(exc))
    logger.debug("Attempted to encode with: %s" % charset)
    body = body.encode(charset, 'replace')
```
we could have just:
```python
if isinstance(body, unicode):
    body = body.encode(charset, 'replace')
```
but the `try/except` is there to log an encoding detection error... since after that we'll end up with some `?` in the message.

Maybe if chardet is failing we should fix that instead of doing a fallback here.