www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - E-mail attachment with unwanted characters

reply Vino.B <vino.bheeman hotmail.com> writes:
Hi All,

   Request your help, the below code is working as expected, but 
when I receive the attachment, the attachment contains the 
orginal text plus some unwanted characters like below, can 
someone help me how to remove these unwanted characters.

Unwanted characters
This is a test documentoÞóÎ}ã¿xÛ]Zõ§ûwN¶÷ÆÝy·

Code:
import std.base64: Base64;
import std.container.array;
import std.conv : to;
import std.file: read;
import std.format : format;
import std.net.curl;
import std.path : baseName;
import std.uuid: randomUUID;
pragma(lib, "curl");

string Message(string To, string From, string Body, string 
Subject, string Filename, ubyte[] Content) {
Array!string headers;
string cid, msg, boundary = randomUUID().toString(), Fname = 
baseName(Filename);
const string crlf = "\r\n";
int Size = to!int(getSize(Filename));

     headers.insert("From: " ~ From );
     headers.insert("To: " ~ To );
     headers.insert("Subject: " ~ "Subject" );
     headers.insert("MIME-Version: 1.0");
     headers.insert(format("Content-Type: multipart/mixed; 
boundary=\"%s\"\r\n", boundary));
     headers.insert("--" ~ boundary);
     headers.insert("Content-Type: text/html; charset=utf-8");
     headers.insert(crlf);
     headers.insert(Body);
     headers.insert(crlf);
     headers.insert("--" ~ boundary);
     headers.insert("Content-Type: text/plain");
     headers ~ ((cid !is null) ? "Content-ID: <" ~ cid ~ ">" : "");
     headers.insert("Content-Transfer-Encoding: base64");
     headers.insert("Content-Disposition: attachment; filename=\"" 
~ Fname ~ "\"");
     headers.insert(crlf);
     headers.insert(to!string(Base64.encode(Content)) ~ ".\r\n");
     headers.insert(crlf);
     headers.insert("--" ~ boundary ~ ".");

     msg.reserve(Size + Body.length);
     foreach(header; headers) { msg ~= header ~ "\r\n"; }
     if(msg.length > 0) { msg ~= "\r\n";}
     return(msg);

  }

  void main () {
  auto Filename = "C:\\Script\\New\\new.txt";
  auto Con = cast(ubyte[])read(Filename);
  auto smtp = SMTP("smtp://xxx.com");
  smtp.mailTo = "user ask.com";
  smtp.mailFrom = "admin ask.com";
  smtp.message =  Message("user ask.com", "admin ask.com", "Test", 
"TestMail", Filename, Con);
  smtp.perform();
}

From,
Vino.B
Apr 27 2018
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 27 April 2018 at 17:57:26 UTC, Vino.B wrote:
     headers.insert(to!string(Base64.encode(Content)) ~ ".\r\n");
     headers.insert("--" ~ boundary ~ ".");
what are those random dots for?
Apr 27 2018
parent reply Vino.B <vino.bheeman hotmail.com> writes:
On Friday, 27 April 2018 at 18:20:46 UTC, Adam D. Ruppe wrote:
 On Friday, 27 April 2018 at 17:57:26 UTC, Vino.B wrote:
     headers.insert(to!string(Base64.encode(Content)) ~ 
 ".\r\n");
     headers.insert("--" ~ boundary ~ ".");
what are those random dots for?
Hi Adam, Thank you very much, after removing the dot the unwanted characters disappeared, The earlier program (as function) is working as expected without any issue, but if I change the program from function to Classes, then the programing is executing without any errors, able to get the attachment wihout any unwanted characters, but not able to get the body text, tries passing the body text as Array!sting and normal string, even then the body message is not appearing when I receive the mails. Code: import std.array: join; import std.base64: Base64; import std.container.array; import std.conv : to; import std.file: read, getSize; import std.format : format; import std.net.curl; import std.path : baseName; import std.uuid: randomUUID; pragma(lib, "curl"); class EmailMessage { static string Boundary; static this() { Boundary = randomUUID().toString(); } string From, Subject, cid, Filename, msg, crlf = "\r\n"; Array!string To, Body, headers, attach; int Size; void Attachment (string Filename) { attach ~= Filename; } string BuildMail () { string[] tos; foreach (e; To) { tos ~= e; } headers.insert("From: " ~ From ); headers.insert("To: " ~ join(tos, ",")); headers.insert("Subject: " ~ Subject); headers.insert("MIME-Version: 1.0"); headers.insert(format("Content-Type: multipart/alternative; boundary=\"%s\"\r\n", Boundary)); headers.insert("--" ~ Boundary); headers.insert("Content-Type: text/plain; charset=utf-8"); headers ~ Body; //Array!string does not work headers.insert(Body); //string does not work headers.insert("--" ~ Boundary); headers.insert("Content-Type: text/plain"); headers ~ ((cid !is null) ? "Content-ID: <" ~ cid ~ ">" : ""); headers.insert("Content-Transfer-Encoding: base64"); foreach (File; attach) { string Fname = baseName(File); ubyte[] Content = cast(ubyte[])read(File); Size = to!int(getSize(File) + Body.length); headers.insert("Content-Disposition: attachment; filename=\"" ~ Fname ~ "\""); headers.insert(crlf); headers.insert(to!string(Base64.encode(Content)) ~ ".\r\n"); } headers.insert("--" ~ Boundary); msg.reserve(Size); foreach(header; headers) { msg ~= header ~ "\r\n"; } if(msg.length > 0) { msg ~= "\r\n";} return(msg); } void Send(string server) { const(char)[][] allRecipients; foreach (e; To) { allRecipients ~= e; } auto smtp = SMTP(server); smtp.mailTo(allRecipients); smtp.mailFrom = From; smtp.message = BuildMail(); smtp.perform(); } } void main () { string Filename = "D:\\DScript\\Test.txt"; Array!string To, Body; To.insert("user1 ask.com"); To.insert("user2 ask.com"); Body.insert("This is Test1"); Body.insert("This is Test2"); auto message = new EmailMessage(); message.To = To; message.From = "admin ask.com"; message.Subject = "My Subject"; message.Body ~= Body; //Array!string does not work mesagae.Body = "Test Body"; //string does not work message.Attachment = Filename; message.Send = "smtp://ask.com"; } From, Vino.B
Apr 28 2018
parent Vino.B <vino.bheeman hotmail.com> writes:
On Saturday, 28 April 2018 at 16:37:26 UTC, Vino.B wrote:
 On Friday, 27 April 2018 at 18:20:46 UTC, Adam D. Ruppe wrote:
 [...]
Hi Adam, Thank you very much, after removing the dot the unwanted characters disappeared, The earlier program (as function) is working as expected without any issue, but if I change the program from function to Classes, then the programing is executing without any errors, able to get the attachment wihout any unwanted characters, but not able to get the body text, tries passing the body text as Array!sting and normal string, even then the body message is not appearing when I receive the mails. [...]
Hi Adam, Thank you very much, was able to resolve the issue. From, Vino.B
Apr 29 2018