E-mail¶
E-mail¶
Webové rozhraní API obsahuje prostředek pro odesílání e-mailů. Pro odesílání e-mailů je nutné, aby byla správně nastavena konfigurace SMTP a byla definována e-mailová adresa pro upozornění systému pro instanci DHIS2. Nastavení SMTP můžete provést na obrazovce nastavení e-mailu a e-mailová adresa upozornění systému z obrazovky obecného nastavení v DHIS2.
/api/33/email
Oznámení systému¶
Prostředek oznámení vám umožňuje odesílat systémová e-mailová oznámení s daným předmětem a textem v JSON nebo XML. E-mail bude odeslán na e-mailovou adresu s oznámením, jak je definováno v obecném nastavení systému DHIS2:
{
"subject": "Integrity check summary",
"text": "All checks ran successfully"
}
Systémové e-mailové oznámení můžete odeslat zveřejněním příspěvku do zdroje oznámení takto:
curl -d @email.json "localhost/api/33/email/notification" -X POST
-H "Content-Type:application/json" -u admin:district
Odchozí e-maily¶
Můžete také poslat obecné e-mailové oznámení odesláním do zdroje oznámení, jak je uvedeno níže. Aby bylo možné použít tento API, musí být v systému oprávnění F_SEND_EMAIL nebo ALL. Parametr subjektu je volitelný. Řetězec „DHIS 2“ bude odeslán jako výchozí předmět, pokud není uveden v adrese URL. Adresa URL by měla být kódována, aby bylo možné používat toto API.
curl "localhost/api/33/email/notification?recipients=xyz%40abc.com&message=sample%20email&subject=Test%20Email"
-X POST -u admin:district
The previous example is convenient for short messages. However, since the message is passed as a query string, it can quickly hit the URL length limit of the server. For longer messages, it is better to send the data in the POST request body. The following example shows how to send an email with a longer message in the request body:
curl -u admin:district -X POST 'localhost/api/33/email/notification' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'recipients=recipient1@example.com,recipient2@example.com' \
--data-urlencode 'subject=Important System Update' \
--data-urlencode 'message=Dear user, we are writing to inform you about an important system update that will take place this weekend. The system will be unavailable for a few hours. We apologize for any inconvenience this may cause. Please plan your work accordingly. Best regards, The DHIS2 Team.'
To send an email with an HTML body, you can simply provide the HTML content in the message parameter. The email client should interpret the HTML and render it accordingly. Note that the Content-Type header in the curl command should be application/x-www-form-urlencoded, as the data is sent as URL-encoded form data. The following example shows how to send an email with a simple HTML table:
curl -u admin:district -X POST 'localhost/api/33/email/notification' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'recipients=recipient1@example.com,recipient2@example.com' \
--data-urlencode 'subject=System Maintenance Schedule' \
--data-urlencode 'message=<html><body><h2>System Maintenance</h2><p>Dear user,</p><p>We are writing to inform you about scheduled system maintenance. The following table shows the maintenance schedule for the upcoming week:</p><table border="1"><tr><th>Day</th><th>Time</th></tr><tr><td>Monday</td><td>10 PM - 11 PM</td></tr><tr><td>Wednesday</td><td>10 PM - 11 PM</td></tr></table><p>We apologize for any inconvenience this may cause. Please plan your work accordingly.</p><p>Best regards,<br>The DHIS2 Team</p></body></html>'
Zkušební zpráva¶
Chcete-li otestovat, zda je nastavení SMTP správné, zasláním testovacího e-mailu sami sobě, můžete komunikovat s prostředkem test. K odesílání testovacích e-mailů je nutné, aby měl váš uživatelský účet DHIS2 přidruženou platnou e-mailovou adresu. Můžete poslat zkušební e-mail takto:
curl "localhost/api/33/email/test" -X POST -H "Content-Type:application/json" -u admin:district