E-mail attachment with unwanted characters
Vino.B
vino.bheeman at hotmail.com
Sat Apr 28 16:37:26 UTC 2018
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 at ask.com");
To.insert("user2 at ask.com");
Body.insert("This is Test1");
Body.insert("This is Test2");
auto message = new EmailMessage();
message.To = To;
message.From = "admin at 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
More information about the Digitalmars-d-learn
mailing list