4D Internet Commands v16

Appendix E, Mocking network to file

Inicio

 
4D Internet Commands v16
Appendix E, Mocking network to file

Appendix E, Mocking network to file  


 

 

Testing and debugging code that sends emails could be a pain; when an email is not received correctly, the source of the problem could be the network configuration, the provider, the client software, and so on.

To help you in this case, we added the ability to dump email in a local file instead of sending it. With this feature, you just need to modify the resulting file slighlty to create an EML file that can display the results in MS Outlook. You can also include email files in unit-testing procedures.

You can run the local code:

 $err:=SMTP_SetPrefs(0;15;0) // Body: UTF-8  &  QuotedPrintable, Header: UTF-8  &  Base64
 $err:=SMTP_Charset(1;1) // Apply setting to header and body (Body: UTF-8  &  QuotedPrintable  &  Header: UTF-8  &  Base64)
 
  //$hostName:="smtp.gmail.com"  // This line sends it over the network using smtp.gmail.com
 $hostName:="file:C:\\Users\\MyWinUser\\Desktop\\test.txt" // This line doesn't send the email, instead it saves the bytes in a file you can monitor
 $msgTo:="mail.to@gmail.com"
 $msgFrom:="mail.sender@gmail.com"
 
 $mailSubject:="テストメール(v17 4372) " //test using extended characters
 $mailBody:="日本語で終わる"
 $err:=SMTP_QuickSend($hostName;$msgFrom;$msgTo;$mailSubject;$mailBody;0;0;$msgFrom;"password")

This produces the following .txt file:

<mail.sender@gmail.com>
<mail.to@gmail.com>

Mime-Version: 1.0
Content-Type: text/plain;charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Date: Fri, 08 Jul 2016 16:45:24 +0200
To: mail.to@gmail.com
From: mail.sender@gmail.com
Subject: =?utf-8?B?44OG44K544OI44Oh44O844Or77yIdjE3IDQzNzIpIA==?=

=E6=97=A5=E6=9C=AC=E8=AA=9E=E3=81=A7=E7=B5=82=E3=82=8F=E3=82=8B

If you want to open this file as a standard MS Outlook email:

  1. Delete all lines before "Mime-Version: 1.0":
    Mime-Version: 1.0
    Content-Type: text/plain;charset="utf-8"
    Content-Transfer-Encoding: quoted-printable
    Date: Fri, 08 Jul 2016 16:45:24 +0200
    To: mail.to@gmail.com
    From: mail.sender@gmail.com
    Subject: =?utf-8?B?44OG44K544OI44Oh44O844Or77yIdjE3IDQzNzIpIA==?=

    =E6=97=A5=E6=9C=AC=E8=AA=9E=E3=81=A7=E7=B5=82=E3=82=8F=E3=82=8B
  2. Save this file with the ".eml" extension, for example "test.eml".
  3. Double-click the file and you will see the email in MS Outlook just as if you received it from your mail server.

To comply with unit-testing mechanisms, when using SMTP_QuickSend with an output file, the Date header is always the following string:

Date: Fri, 08 Jul 2016 16:45:24 +0200

In this case, date comparisons will not fail in unit tests.

Note: When you use a real host (such as smtp.gmail.com), the date header gets replaced by an actual timestamp.

If you want to have an actual timestamp in your test file, you can use this feature with SMTP_Send. In this case, you can use the SMTP_Date command and then provide an actual Date header with SMTP_Send.

 
PROPIEDADES 

Producto: 4D Internet Commands
Tema: Anexos

 
HISTORIA 

Creado por: 4D v16

 
ARTICLE USAGE

4D Internet Commands ( 4D Internet Commands v16)