www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - SMTP Mail

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

   Request your help on sending Mails, I am able to receive mails 
with empty body the line "smtp.message ="Example Message" doesn't 
seem to be working and also please let me know how do i send a 
file as a attachment in a email.

import std.net.curl;
void main ()
{
auto smtp = SMTP("smtp://server.com");
smtp.mailTo = ["xxx xxx.com"];
smtp.mailFrom = "xxx xxx.com";
smtp.message = "Example Message";
smtp.perform();
}

From,
Vino.B
Aug 22 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 22 August 2017 at 12:52:24 UTC, Vino.B wrote:
   Request your help on sending Mails, I am able to receive 
 mails with empty body the line "smtp.message ="Example Message" 
 doesn't seem to be working and also please let me know how do i 
 send a file as a attachment in a email.
The message there needs to be the complete message, including headers. The SMTP struct is pretty low-level. My email.d (plus its dependencies, characterencodings.d, dom.d, and htmltotext.d) has the code to form a full message. It isn't very documented though. https://github.com/adamdruppe/arsd auto message = new EmailMessage(); message.to ~= "some email"; message.subject = "Subject" message.setTextBody("hi"); message.send(RelayInfo("smtp://whatever", "user", "pass"));
Aug 24 2017
parent reply Vino <vino.bheeman hotmail.com> writes:
On Friday, 25 August 2017 at 02:13:42 UTC, Adam D. Ruppe wrote:
 On Tuesday, 22 August 2017 at 12:52:24 UTC, Vino.B wrote:
   Request your help on sending Mails, I am able to receive 
 mails with empty body the line "smtp.message ="Example 
 Message" doesn't seem to be working and also please let me 
 know how do i send a file as a attachment in a email.
The message there needs to be the complete message, including headers. The SMTP struct is pretty low-level. My email.d (plus its dependencies, characterencodings.d, dom.d, and htmltotext.d) has the code to form a full message. It isn't very documented though. https://github.com/adamdruppe/arsd auto message = new EmailMessage(); message.to ~= "some email"; message.subject = "Subject" message.setTextBody("hi"); message.send(RelayInfo("smtp://whatever", "user", "pass"));
Hi Adam, I am trying your email.d programming, and i am getting the below errors, can you please help me, i just used these programs (characterencodings.d, color.d, dom.d, htmltotext.d, email.d) Program import arsd.email; void main() { auto message = new EmailMessage(); message.to ~= "vino xxx.com"; message.subject = "Test"; message.setHtmlBody("<b>1.Line 1</b><b>2.Line 2</b><b>3.Line 3</b>"); message.addAttachment("text/txt", "C:\\Temp\\test.log", "Text"); message.send(RelayInfo("smtp://smtp.xxxx.com")); } Error OPTLINK (R) for Win32 Release 8.00.17 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html C:\Users\-TSK-X~1\AppData\Local\Temp\6\.rdmd\rdmd-dmail.d-4C543ACF04BF46D1398435D9500B3B70\objs\dmai l.exe.obj(dmail.exe) Error 42: Symbol Undefined __D6object__T14__switch_errorZQrFNaNbNiNfAyakZv C:\Users\-TSK-X~1\AppData\Local\Temp\6\.rdmd\rdmd-dmail.d-4C543ACF04BF46D1398435D9500B3B70\objs\dmai l.exe.obj(dmail.exe) Error 42: Symbol Undefined __D4arsd10htmltotext10htmlToTextFAyabiZQg C:\Users\-TSK-X~1\AppData\Local\Temp\6\.rdmd\rdmd-dmail.d-4C543ACF04BF46D1398435D9500B3B70\objs\dmai l.exe.obj(dmail.exe) Error 42: Symbol Undefined __D4arsd10htmltotext12__ModuleInfoZ Error: linker exited with status 3 From, Vino.B
Apr 08
next sibling parent Vino <vino.bheeman hotmail.com> writes:
On Sunday, 8 April 2018 at 15:45:48 UTC, Vino wrote:
 On Friday, 25 August 2017 at 02:13:42 UTC, Adam D. Ruppe wrote:
 [...]
Hi Adam, I am trying your email.d programming, and i am getting the below errors, can you please help me, i just used these programs (characterencodings.d, color.d, dom.d, htmltotext.d, email.d) [...]
Hi All, Any help is much appreciated. From, Vino.B
Apr 09
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 8 April 2018 at 15:45:48 UTC, Vino wrote:
   I am trying your email.d programming, and i am getting the 
 below errors, can you please help me, i just used these 
 programs (characterencodings.d, color.d, dom.d, htmltotext.d, 
 email.d)
What is your build command? It looks like a module is just missing in the build. If you are using rdmd, what is your directory layout too. (I don't work Sundays btw so that's why I am so slow to respond.)
Apr 09
parent reply Vino.B <vino.bheeman htomail.com> writes:
On Monday, 9 April 2018 at 13:02:06 UTC, Adam D. Ruppe wrote:
 On Sunday, 8 April 2018 at 15:45:48 UTC, Vino wrote:
   I am trying your email.d programming, and i am getting the 
 below errors, can you please help me, i just used these 
 programs (characterencodings.d, color.d, dom.d, htmltotext.d, 
 email.d)
What is your build command? It looks like a module is just missing in the build. If you are using rdmd, what is your directory layout too. (I don't work Sundays btw so that's why I am so slow to respond.)
Hi Adam, Thank you very much, I copied your folder arsd under the phobes folder in c:\D\... and the program was placed on my desktop and tried to execute it from the desktop via rdmd. From, Vino.B
Apr 09
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 9 April 2018 at 15:38:55 UTC, Vino.B wrote:
 Thank you very much, I copied your folder arsd under the phobes 
 folder in c:\D\... and the program was placed on my desktop and 
 tried to execute it from the desktop via rdmd.
I don't think rdmd is seeing the dependency on htmltotext.d. Try importing it explicitly from your main: import arsd.email; import arsd.htmltotext; /* the rest of your code */ Just that way rdmd will find it easier.
Apr 09
parent reply Vino <vino.bheeman hotmail.com> writes:
On Monday, 9 April 2018 at 19:19:53 UTC, Adam D. Ruppe wrote:
 On Monday, 9 April 2018 at 15:38:55 UTC, Vino.B wrote:
 Thank you very much, I copied your folder arsd under the 
 phobes folder in c:\D\... and the program was placed on my 
 desktop and tried to execute it from the desktop via rdmd.
I don't think rdmd is seeing the dependency on htmltotext.d. Try importing it explicitly from your main: import arsd.email; import arsd.htmltotext; /* the rest of your code */ Just that way rdmd will find it easier.
Hi Adam, Now the program works, but the attachment does not work as expected, message.addAttachment("text/plain", "C:\\Temp\\Test\Test1.txt", "Test"); It takes the full path as file name eg: If the attachment file resides on the path C:\Temp\Test\Test1.txt" the attachment name file name that we receive is as "CTempTestTest1.txt" and also it does not attached the real file, instead the content of the file is "Test" We Tried the below message.addAttachment("text/plain", "Test1.txt", "C:\\Temp\\Test\Test1.txt"); After the above change the attachment file name is "Test1.txt" with the content "C:\Temp\Test\Test1.txt" The orignal content of the file is "Hi This is Test Attachment". From, Vino.B
Apr 10
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 10 April 2018 at 11:09:56 UTC, Vino wrote:
  Now the program works, but the attachment does not work as 
 expected,

 message.addAttachment("text/plain", "C:\\Temp\\Test\Test1.txt", 
 "Test");
What did you expect that "Test" argument to do if it was going to read the file as the content? I guess I should change the documents on this. The way it works is the filename argument is just what is seen in the email as a suggestion name for the user to download the attachment, and the content argument is what is what's inside that attachment. Since the attachment name just suggests a name, and the user decides where to put it, it should NOT have a path name. So try this instead: addAttachment("text/plain", "Test1.txt", std.file.read("C:\Temp\Test\Test1.txt")); so the name argument is JUST the name to suggest to the user, then the content argument gives the content of your file to attach - which here is read from the file on disk via the phobos std.file read function.
Apr 10
parent reply Vino <vino.bheeman hotmail.com> writes:
On Tuesday, 10 April 2018 at 13:51:02 UTC, Adam D. Ruppe wrote:
 On Tuesday, 10 April 2018 at 11:09:56 UTC, Vino wrote:
  Now the program works, but the attachment does not work as 
 expected,

 message.addAttachment("text/plain", 
 "C:\\Temp\\Test\Test1.txt", "Test");
What did you expect that "Test" argument to do if it was going to read the file as the content? I guess I should change the documents on this. The way it works is the filename argument is just what is seen in the email as a suggestion name for the user to download the attachment, and the content argument is what is what's inside that attachment. Since the attachment name just suggests a name, and the user decides where to put it, it should NOT have a path name. So try this instead: addAttachment("text/plain", "Test1.txt", std.file.read("C:\Temp\Test\Test1.txt")); so the name argument is JUST the name to suggest to the user, then the content argument gives the content of your file to attach - which here is read from the file on disk via the phobos std.file read function.
Hi Adam, Thank you very much, the program now works, but i need to make some adjustment, if possible can you please help me on this. The variable "to" is of type string[] but we need it as Array!string The variable "Subject" but we need it as Array!string. From, Vino.B
Apr 10
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 10 April 2018 at 15:10:44 UTC, Vino wrote:
 The variable "to" is of type string[] but we need it as 
 Array!string
 The variable "Subject" but we need it as Array!string.
You'll have to convert them yourself if that is a must, but why would you need that?
Apr 10
parent reply Vino <vino.bheeman hotmail.com> writes:
On Tuesday, 10 April 2018 at 15:18:19 UTC, Adam D. Ruppe wrote:
 On Tuesday, 10 April 2018 at 15:10:44 UTC, Vino wrote:
 The variable "to" is of type string[] but we need it as 
 Array!string
 The variable "Subject" but we need it as Array!string.
You'll have to convert them yourself if that is a must, but why would you need that?
Hi Adam, I tried to replicate your code with what is required for me, and was able to successed 90% and stuck, can you please help me. Using the below code are are able to receive the mail with the attachment, but the content of the attachment contains the Body text of the mail(Body1") rather than the original text and the mail body is empty, not sure where is the issue. Code: import std.net.curl; pragma(lib, "curl"); import std.format; import std.container.array; import std.stdio; import std.path; import std.file; struct RelayInfo { string server; } struct MimeAttachment { string type; string filename; const(void)[] content; string id; } class EmailMessage { Array!string To, Body, headers; string From, Subject, msg; override string toString () { string T = "%-(%s, %)".format(To[]); string B = "%-(%s, %)".format(Body[]); headers.insertBack("To: " ~ T); headers.insertBack("From: " ~ From); headers.insertBack("Subject: " ~ Subject); headers.insertBack("MIME-Version: 1.0"); msg.reserve(Body.length + 1024); foreach(header; headers) { msg ~= header ~ "\r\n"; } if(msg.length > 0) { msg ~= "\r\n"; msg ~= B; } return(msg); } const(MimeAttachment)[] attachments; void addAttachment(string Fname, ) { string mimeType = "text/plain"; string filename = baseName(Fname); void[] content = read(Fname); string id = null; headers.insertBack("Content-Disposition: attachment; filename=\""~ filename ~"\""); headers.insertBack("Content-ID: <" ~ id ~ ">"); headers.insertBack("Content-Type: text/plain; charset=UTF-8; file=" ~ filename); attachments ~= MimeAttachment(mimeType, filename, content, id); } void send (RelayInfo mailServer = RelayInfo("smtp://localhost")) { auto smtp = SMTP(mailServer.server); string T = "%-(%s, %)".format(To[]); smtp.mailTo = T; smtp.mailFrom = From; smtp.message = toString; smtp.perform(); } } void main () { string Filename = "D:\\DScript\\Tmailconfig.txt"; auto message = new EmailMessage(); message.To ~= "vino xxx.com"; message.From = "Server hosting.com"; message.Subject = "Test"; message.Body ~= "Body1"; message.addAttachment(Filename)); message.send(RelayInfo("smtp://smtp.awk.sed.com")); } From, Vino.B
Apr 12
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 12 April 2018 at 15:58:33 UTC, Vino wrote:
  I tried to replicate your code with what is required for me
I still wanna know: why use Array instead of regular arrays?
 Using the below code are are able to receive the mail with the 
 attachment, but the content of the attachment contains the Body 
 text of the mail(Body1") rather than the original text and the 
 mail body is empty, not sure where is the issue.
You didn't add the attachment text. You see how my toString http://dpldocs.info/experimental-docs/source/arsd.email.d.html#L108 includes a loop over the attachments? http://dpldocs.info/experimental-docs/source/arsd.email.d.html#L183 Attachments need to be serialized in MIME format: http://dpldocs.info/experimental-docs/source/arsd.email.d.html#L521 I didn't even do it perfectly there lol but it is good enough.
 override string toString ()
 {
 	string T = "%-(%s,  %)".format(To[]); string B = "%-(%s,  
 %)".format(Body[]);
 	headers.insertBack("To: " ~ T);
 	headers.insertBack("From: " ~ From);
 	headers.insertBack("Subject: " ~ Subject);
 	headers.insertBack("MIME-Version: 1.0");
     	msg.reserve(Body.length + 1024);
 	foreach(header; headers) { msg ~= header ~ "\r\n"; }
 	if(msg.length > 0) { msg ~= "\r\n"; msg ~= B; }
 	
 	return(msg);
 }
But you didn't even put the attachments in the string at all.
Apr 12