Download MetaTrader 5

SendMail() not working in EA

To add comments, please log in or register
fabriziosartori
43
fabriziosartori  

Hi, everybody!

I've been developing an EA and one of the new requisites is to send an e-mail informing that a withdrawal must be carried out.

I configured my MT4 platform to work with a gmail account (sender), by using hMailServer application, and the manually configured test e-mail is successfully sent to the receiver e-mail.

The EA has been tested on my DEMO account, and another "interesting" thing is about the piece of code below:

   ...

   SendMail(subject, message);

   log_DebugViewer("SendMail(" + subject + ", " + message + ")");

   if ( GetLastError() == ERR_NO_ERROR )

      log_DebugViewer("e-mail successfully sent");

   else

      log_DebugViewer("error [" + GetLastError() + "] while sending e-mail");

   ...

Considerations:

- As you may realize, the log lines are printed in Debug Viewer (Microsoft application)

- When I check the log lines, I see that both subject and message parameters seem to be formatted correctly, otherwise other errors should occur, correct?

- However, the error printed when the supposed error occurs is equal to 0, that means ERR_NO_ERROR.


I appreciate a lot your help.


Best regards,

Fabrizio.

Simon Gniadkowski
Moderator
18025
Simon Gniadkowski  
fabriziosartori:

Hi, everybody!

I've been developing an EA and one of the new requisites is to send an e-mail informing that a withdrawal must be carried out.

I configured my MT4 platform to work with a gmail account (sender), by using hMailServer application, and the manually configured test e-mail is successfully sent to the receiver e-mail.

The EA has been tested on my DEMO account, and another "interesting" thing is about the piece of code below:

Considerations:

- As you may realize, the log lines are printed in Debug Viewer (Microsoft application)

- When I check the log lines, I see that both subject and message parameters seem to be formatted correctly, otherwise other errors should occur, correct?

- However, the error printed when the supposed error occurs is equal to 0, that means ERR_NO_ERROR.

This code will not do what you think . . .

   if ( GetLastError() == ERR_NO_ERROR )   //  reads and resets the error number stored . . .

      log_DebugViewer("e-mail successfully sent");

   else

      log_DebugViewer("error [" + GetLastError() + "] while sending e-mail");  //  the error number has already been reset,  see above

if you must do this, do it like this . . .

   int LastError = GetLastError();     // reads, stores and resets error number held by GetLastError

   if ( LastError == ERR_NO_ERROR )

      log_DebugViewer("e-mail successfully sent");

   else

      log_DebugViewer("error [" + LastError + "] while sending e-mail");
fabriziosartori
43
fabriziosartori  
RaptorUK:

This code will not do what you think . . .

if you must do this, do it like this . . .

Hi, RaptorUK!

Thanks a lot for the tip about checking GetLastError()!

And about the issue about SendMail()? Any idea?

Regards,

Fabrizio

Simon Gniadkowski
Moderator
18025
Simon Gniadkowski  
fabriziosartori:

Hi, RaptorUK!

Thanks a lot for the tip about checking GetLastError()!

And about the issue about SendMail()? Any idea?

I have never had a problem with SendMail(), how frequently are you sending messages ? what type are your subject and message variables ?

Try the test message again to make certain it is still working . . .

fabriziosartori
43
fabriziosartori  
RaptorUK:

I have never had a problem with SendMail(), how frequently are you sending messages ? what type are your subject and message variables ?

Try the test message again to make certain it is still working . . .

Do you use SendMail either in DEMO accounts or in LIVE accounts?

Both parametes are strings. Even when formatting values to be part of them I use DoubleToStr(), independent of being double values. The log lines (Microsoft Debug Viewer), as previously explained, are viewed correctly, that is, without any strange characters.

As this new requirement is still under testing, I also explained when posted the topic that I've been using my DEMO account, not a LIVE one. I have also seen this topic about sending e-mails: https://www.mql5.com/en/articles/1512

The test message (through MT4) is working perfectly. I just run hMailServer before even opening MT4 to perform anything.

Regards,

Fabrizio.

Simon Gniadkowski
Moderator
18025
Simon Gniadkowski  
fabriziosartori:

Do you use SendMail either in DEMO accounts or in LIVE accounts?

Both parametes are strings. Even when formatting values to be part of them I use DoubleToStr(), independent of being double values. The log lines (Microsoft Debug Viewer), as previously explained, are viewed correctly, that is, without any strange characters.

As this new requirement is still under testing, I also explained when posted the topic that I've been using my DEMO account, not a LIVE one. I have also seen this topic about sending e-mails: https://www.mql5.com/en/articles/1512

The test message (through MT4) is working perfectly. I just run hMailServer before even opening MT4 to perform anything.

There is no problem send mails from Demo or Live . . . you can't from the Strategy Tester though . . . did you try the test message again ?
fabriziosartori
43
fabriziosartori  
RaptorUK:
There is no problem send mails from Demo or Live . . . you can't from the Strategy Tester though . . . did you try the test message again ?

Ah, ok! So it's not possible to send e-mails by testing through Strategy Tester. Sorry for the lack of knowledge and kind of silly question: What is (are) other way(s) to perform this test?

Yes, the test message works.


Regards and thanks again,

Fabrizio.

Simon Gniadkowski
Moderator
18025
Simon Gniadkowski  
fabriziosartori:

Ah, ok! So it's not possible to send e-mails by testing through Strategy Tester. Sorry for the lack of knowledge and kind of silly question: What is (are) other way(s) to perform this test?

Yes, the test message works.

If you look at the article here: Testing Features and Limits in MetaTrader 4

. . . it says

"Some functions are processed/passed without output

These are Sleep(), Alert(), SendMail(), PlaySound(), MessageBox(), WindowFind(), WindowHandle(), WindowIsVisible()"


if you want to test SendMail() you need to do it with a Demo/Live account in real time on a live chart not in the Strategy Tester.

fabriziosartori
43
fabriziosartori  
RaptorUK:

If you look at the article here: Testing Features and Limits in MetaTrader 4

. . . it says

"Some functions are processed/passed without output

These are Sleep(), Alert(), SendMail(), PlaySound(), MessageBox(), WindowFind(), WindowHandle(), WindowIsVisible()"


if you want to test SendMail() you need to do it with a Demo/Live account in real time on a live chart not in the Strategy Tester.


Hi, RaptorUK.

Thanks again for your comprehension and quite helpful tips!

Best regards,

Fabrizio.

To add comments, please log in or register