Atozed Forums

Full Version: Setting ContentType for bidi language
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello
 
I try to send an email using IdSMTP and IdMessage components with Hebrew on body text.
I use those settings:

Code:
IdMessage := 'UTF_8';
If HaveAttachments then
  IdMessage.ContentType := 'multipart/mixed';
Else
  IdMessage.ContentType := 'text/plain';

When I add attachments the emails arrive ok,
Without attachments the body text is replaced with questions marks.
 
What I'm doing wrong?
(02-20-2019, 11:20 AM)sorinh Wrote: [ -> ]
Code:
IdMessage := 'UTF_8';

That is not a valid code statement and should not even compile.  I assume you meant this instead:

Code:
IdMessage.CharSet := 'UTF_8';

Note that 'UTF_8' is not a valid charset name, you need to use 'UTF-8' instead (hyphen instead of underscore).

Setting the ContentType property to a 'text/...' media type without including a charset attribute will set the CharSet property to 'us-ascii', which is why you end up getting question marks for non-ASCII characters.  So, you need to set the CharSet property after setting the ContentType property:

Code:
if HaveAttachments then
  IdMessage.ContentType := 'multipart/mixed'
else begin
  IdMessage.ContentType := 'text/plain';
  IdMessage.CharSet := 'UTF-8';
end;

Or, include the charset as part of the ContentType property value:

Code:
if HaveAttachments then
  IdMessage.ContentType := 'multipart/mixed'
else
  IdMessage.ContentType := 'text/plain; charset=UTF-8';

That being said, you might consider using TIdMessageBuilderPlain instead and let it handle the details of populating the TIdMessage for you:

Code:
uses
 ..., IdMessage, IdMessageBuilder;

Bldr := TIdMessageBuiderPlain.Create;
try
  Bldr.PlainTextCharSet := 'UTF-8';
  // populate Bldr.PlainText as needed...
  // add items to Bldr.Attachments as needed...
  Bldr.FillMessage(IdMessage);
finally
  Bldr.Free;
end;