smtp mail library
Info
The smtp mail library is a Library for MetaTrader 5 that this library is designed to send mail messages. function set expands possibilities of traditional sendmail.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Library works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Libraries folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use smtp mail library
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
What this tool does
this library is designed to send mail messages.
Typical Use Case
This Library excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Library works on MetaTrader 5. Place the file in the MQL5/Libraries folder and restart the terminal.
Description & Settings
Related: regular expressions in mq l5 for working with regular expressions - another powerful library for MetaTrader 5 traders.
Also recommended: grid, martin gale include file - similar library with strong performance on MetaTrader 5.
this library is designed to send mail messages.
function set expands possibilities of traditional
sendmail
. now you can send messages both in text and html formats. messaged can be sent to several addressees. you can attach one or several files to your letter. ssl is supported.
dll source code is written on delphi xe4 using
indy
library.
besides project files you should install
openssl
library.
list of library functions:
function
description
mailconnect
connects with mail server
mailsendtext
sends texts
mailsendhtml
sends html-messages
mailsendinlinetextfile
sends messages with text file content inserted
mailsendinlinehtmlfile
sends messages with html file content inserted
mailerrordescription
brings error description back
mailclose
closes connection with mail server
error messages:
constant
value
description
mail_no_connection
-2
no connection
mail_no_initialization
-1
no object for smtp-server
mail_no_error
0
no error
mail_timeout
1
connection is closed due to time out
mail_wrong_login
2
wrong login/password
mail_wrong_host_name
3
wrong host name
mail_host_not_found
60
host is not found
script example:
//+------------------------------------------------------------------+
//| mailsend.mq5 |
//| avoitenko |
//| |
//+------------------------------------------------------------------+
#property
copyright
"avoitenko"
#property
link
""
#property
version
"1.00"
#property
script_show_inputs
#include
<trade\trade.mqh>
#include
<arrays\arraystring.mqh>
#include
<smtpmaillibrary.mqh>
//+------------------------------------------------------------------+
//| enum_message |
//+------------------------------------------------------------------+
enum
enum_message { message_text,
// text
message_html,
// html
message_inline_text,
// inline text
message_inline_html
// inline html
};
//+------------------------------------------------------------------+
//| input parameers |
//+------------------------------------------------------------------+
input
string
mail_options=
"=== mail options ==="
;
// mail options
input
string
inpmailhost=
"smtp.gmail.com"
;
// host
input
int
inpmailport=
465
;
// port
input
string
inpmailuser=
"[email protected]"
;
// user
input
string
inpmailpassword=
"password"
;
// password
input
string
inpmailfrom=
""
;
// from (text)
input
string
inpmailsubject=
"smtp mail library"
;
// subject (text)
input
string
inpmailto=
"[email protected]"
;
// mail to
input
uint
inpmailconnectiontimeout=
5000
;
// connection timeout, msec
input
string
msg_options=
"=== message options ==="
;
// message options
input
enum_message inpmessagetype=message_html;
// type
input
string
inpmessageattachmentfiles=
"d:\\temp\\dollar.bmp;d:\\temp\\euro.bmp"
;
// attachment files
input
string
inpmessageinlinefiles=
"d:\\temp\\reporttester-20066082.html"
;
// inline files
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void
onstart
() {
long
smtp=
0
;
//initialize
//--- connection
int
err=mailconnect(smtp,inpmailhost,inpmailport,inpmailuser,inpmailpassword,inpmailconnectiontimeout);
if
(err!=
0
) {
(
"mailconnect error: "
,mailerrordescription(err));
return
; }
switch
(inpmessagetype) {
//---
case
message_text: {
//--- plain text
string
text=
stringformat
(
"account: %d\r\nbalance: %.2f %s"
,
accountinfointeger
(
account_login
),
accountinfodouble
(
account_balance
),
accountinfostring
(
account_currency
));
//--- send mail to yourself
err=mailsendtext(smtp,inpmailuser,inpmailfrom,inpmailsubject,text,inpmessageattachmentfiles);
if
(err!=
0
)
(
"mailsendtext error: "
,mailerrordescription(err));
else
printformat
(
"program '%s' has sent mail to '%s'"
,
mqlinfostring
(
mql_program_name
),inpmailuser);
//--- send mail to mailto
err=mailsendtext(smtp,inpmailto,inpmailfrom,inpmailsubject,text,inpmessageattachmentfiles);
if
(err!=
0
)
(
"mailsendtext error: "
,mailerrordescription(err));
else
printformat
(
"program '%s' has sent mail to '%s'"
,
mqlinfostring
(
mql_program_name
),inpmailto); }
break
;
//---
case
message_html: {
//--- build html
string
html=buildreport();
//--- send html
err=mailsendhtml(smtp,inpmailto,inpmailfrom,inpmailsubject,html,
""
);
if
(err!=
0
)
(
"mailsendtext error: "
,mailerrordescription(err));
else
printformat
(
"program '%s' has sent mail to '%s'"
,
mqlinfostring
(
mql_program_name
),inpmailto); }
break
;
//---
case
message_inline_text: { err=mailsendinlinetextfile(smtp,inpmailto,inpmailfrom,inpmailsubject,inpmessageinlinefiles);
if
(err!=
0
)
(
"mailsendtext error: "
,mailerrordescription(err));
else
printformat
(
"program '%s' has sent mail to '%s'"
,
mqlinfostring
(
mql_program_name
),inpmailto); }
break
;
//---
case
message_inline_html: { err=mailsendinlinehtmlfile(smtp,inpmailto,inpmailfrom,inpmailsubject,inpmessageinlinefiles);
if
(err!=
0
)
(
"mailsendtext error: "
,mailerrordescription(err));
else
printformat
(
"program '%s' has sent mail to '%s'"
,
mqlinfostring
(
mql_program_name
),inpmailto); }
break
; }
//--- close connection
mailclose(smtp); }
//+------------------------------------------------------------------+
//| buildreport |
//+------------------------------------------------------------------+
string
buildreport() { carraystring html;
//---
html.add(
"<html><head><title>report</title>"
); html.add(
"<meta name=\"format-detection\" content=\"telephone=no\">"
); html.add(
"<style type=\"text/css\" media=\"screen\">"
); html.add(
"<!--"
); html.add(
"td{font: 8pt tahoma,arial;}"
); html.add(
"//-->"
); html.add(
"</style>"
); html.add(
"<style type=\"text/css\" media=\"print\">"
); html.add(
"<!--"
); html.add(
"td{font: 7pt tahoma,arial;}"
); html.add(
"//-->"
); html.add(
"</style>"
); html.add(
"</head>"
); html.add(
"<body topmargin=1 marginheight=1> <font face=\"tahoma,arial\" size=1>"
); html.add(
"<div align=center>"
); html.add(
stringformat
(
"<div style=\"font: 18pt times new roman\"><b>%s</b></div>"
,
accountinfostring
(
account_server
))); html.add(
"<table cellspacing=1 cellpadding=3 border=0>"
);
//---
html.add(
"<tr>"
); html.add(
stringformat
(
"<td colspan=2>a/c no: <b>%d</b></td>"
,
accountinfointeger
(
account_login
))); html.add(
stringformat
(
"<td colspan=6>name: <b>%s</b></td>"
,
accountinfostring
(
account_name
))); html.add(
"<td colspan=2> </td>"
); html.add(
stringformat
(
"<td colspan=2 align=right>%s</td>"
,
timetostring
(
timecurrent
()))); html.add(
"</tr>"
);
//---
html.add(
"<tr>"
); html.add(
"<td colspan=13><b>open trades:</b></td>"
); html.add(
"</tr>"
);
//---
html.add(
"<tr align=center bgcolor=\"#c0c0c0\">"
); html.add(
"<td>ticket</td>"
); html.add(
"<td nowrap>open time</td>"
); html.add(
"<td>type</td>"
); html.add(
"<td>size</td>"
); html.add(
"<td>symbol</td>"
); html.add(
"<td>price</td>"
); html.add(
"<td>s/l</td>"
); html.add(
"<td>t/p</td>"
); html.add(
"<td>price</td>"
); html.add(
"<td nowrap>commission</td>"
); html.add(
"<td>swap</td>"
); html.add(
"<td>profit</td>"
); html.add(
"</tr>"
);
//---
double
profit=
0.0
;
int
total=
positionstotal
();
if
(total ==
0
) { html.add(
"<tr align=right><td colspan=13 nowrap align=center>no transactions</td></tr>"
); }
else
{ cpositioninfo m_position;
for
(
int
i=
0
; i<total; i++) { m_position.selectbyindex(i);
//--- color
if
((i&
1
)==
0
)html.add(
"<tr align=right>"
);
else
html.add(
"<tr bgcolor=#e0e0e0 align=right>"
);
//--- data
html.add(
stringformat
(
"<td>%d</td>"
,m_position.identifier())); html.add(
stringformat
(
"<td> %s</td>"
,
timetostring
(m_position.time())));
if
(m_position.positiontype()==
position_type_buy
) html.add(
"<td>buy</td>"
);
else
html.add(
"<td>sell</td>"
); html.add(
stringformat
(
"<td>%.2f</td>"
,m_position.volume())); html.add(
stringformat
(
"<td>%s</td>"
,m_position.
symbol
())); html.add(
stringformat
(
"<td>%.5f</td>"
,m_position.priceopen())); html.add(
stringformat
(
"<td>%.5f</td>"
,m_position.stoploss())); html.add(
stringformat
(
"<td>%.5f</td>"
,m_position.takeprofit())); html.add(
stringformat
(
"<td>%.5f</td>"
,m_position.pricecurrent())); html.add(
stringformat
(
"<td>%.2f</td>"
,m_position.commission())); html.add(
stringformat
(
"<td>%.2f</td>"
,m_position.swap())); html.add(
stringformat
(
"<td>%.2f</td>"
,m_position.profit())); html.add(
"</tr>"
);
//---
profit+=m_position.profit(); } }
//---
html.add(
"<tr>"
); html.add(
stringformat
(
"<td colspan=2><b>balance: %.2f</b></td>"
,
accountinfodouble
(
account_balance
))); html.add(
stringformat
(
"<td colspan=5><b>equity: %.2f</b></td>"
,
accountinfodouble
(
account_equity
))); html.add(
"<td colspan=3><b>floating p/l:</b></td>"
); html.add(
stringformat
(
"<td colspan=4 align=right><b>%.2f</b></td>"
,profit)); html.add(
"</tr>"
);
//---
html.add(
"</table></div></font></body></html>"
);
//--- save to single string
string
result=
""
; total=html.total();
for
(
int
i=
0
;i<total;i++) result+=html.at(i);
//--- done
return
(result); }
//+------------------------------------------------------------------+
results:
You may also like: a charts and mw5 class - excellent alternative for library users on MetaTrader 5.
Source Code
#property copyright "robotfx"
#property link "https://robotfx.org"
#property version "1.00"
#property script_show_inputs
#include <trade\trade.mqh>
#include <arrays\arraystring.mqh>
#include <smtpmaillibrary.mqh>
enum enum_message
{
message_text, // text
.......
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.