{"id":339569,"date":"2021-09-15T15:00:01","date_gmt":"2021-09-15T12:00:01","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it\/"},"modified":"2021-09-15T15:00:01","modified_gmt":"2021-09-15T12:00:01","slug":"how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it\/","title":{"rendered":"#How to Route Incoming Email to Your Application With Exim \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a368bd362bae\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a368bd362bae\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it\/#Managing_Exim_Configuration\" >Managing Exim Configuration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it\/#Creating_a_Router\" >Creating a Router<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it\/#Creating_a_Transport\" >Creating a Transport<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it\/#Using_Environment_Variables\" >Using Environment Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it\/#Return_Values\" >Return Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it\/#Command_Invocation\" >Command Invocation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-route-incoming-email-to-your-application-with-exim-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Route Incoming Email to Your <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">App<\/a>lication With Exim \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure style=\"width: 1200px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-14187\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/6d08103b.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/6d08103b.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/6d08103b.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Illustration showing email symbols moving out from a light source\" width=\"1200\" height=\"675\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-illustration\/email-158159369\">dwori\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>Exim is a popular message transfer agent (MTA) for Unix systems. It offers a wide variety of routing and transportation options. In this article, we\u2019ll show how to use Exim to pipe incoming emails into your own script.<\/p>\n<p>We\u2019ll assume you\u2019ve already got a functioning Exim server that\u2019s able to receive email. The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/Exim\/exim\/wiki\/ObtainingExim\">official wiki<\/a> provides informative guidance if you\u2019re starting from scratch with a fresh installation.<\/p>\n<h2 id=\"managing-exim-configuration\"><span class=\"ez-toc-section\" id=\"Managing_Exim_Configuration\"><\/span>Managing Exim Configuration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Available configuration approaches vary by operating system distribution. An Exim installation built from source <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.exim.org\/exim-html-current\/doc\/html\/spec_html\/ch-the_default_configuration_file.html\">will use <code>src\/configure.default<\/code><\/a> as its config file.<\/p>\n<p>The file for Exim installed from a package manager is typically <code>\/etc\/exim\/config<\/code> or <code>\/etc\/exim.conf<\/code>. You can find the path that\u2019s currently used by running <code>exim -bP configure_file<\/code>.<\/p>\n<p>Debian-based operating systems have a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/wiki.debian.org\/Exim\">slightly more complicated<\/a> system. There are two possible configuration methods: a single file, <code>\/etc\/exim4\/exim4.conf.template<\/code>, or split config files within <code>\/etc\/exim4\/conf.d<\/code>. You need to run <code>update-exim4.conf<\/code> after you make changes. This creates a single merged file that\u2019s actually read by Exim.<\/p>\n<p>Exim should be restarted each time you change its configuration. Run <code>service exim4 restart<\/code> to apply your changes.<\/p>\n<h2 id=\"creating-a-router\"><span class=\"ez-toc-section\" id=\"Creating_a_Router\"><\/span>Creating a Router<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The first step in getting email to your application is defining a custom router. Routers match incoming emails against a set of conditions to determine the delivery mechanism to use.<\/p>\n<p>Routers are processed in the sequential order they\u2019re found in the configuration file. Any routers above yours in the file could match incoming emails first.<\/p>\n<p>Add your router to your configuration file within the <code>ROUTERS CONFIGURATION<\/code> section. If you\u2019re using the split Debian configuration, you can create a new file in <code>\/etc\/exim4\/conf.d\/routers<\/code> instead. Routers will be combined in alphabetical order when you use this approach.<\/p>\n<p>Here\u2019s an example router:<\/p>\n<pre>example_router:&#13;\n  driver = accept&#13;\n  domains = example.com&#13;\n  transport = example_transport<\/pre>\n<p>This is a basic router which matches any email sent to <code>example.com<\/code>. When a match is processed, Exim will <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.exim.org\/exim-html-current\/doc\/html\/spec_html\/ch-how_exim_receives_and_delivers_mail.html#SECTrunindrou\"><code>accept<\/code> the message<\/a> and deliver it using the <code>example_transport<\/code> transport. We\u2019ll create this next.<\/p>\n<h2 id=\"creating-a-transport\"><span class=\"ez-toc-section\" id=\"Creating_a_Transport\"><\/span>Creating a Transport<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once an email has been accepted by a router, it\u2019s delivered by a transport. Transports are responsible for implementing the message delivery routine. Different drivers can send over the internet using SMTP, transfer to a local Unix user, or write to a file.<\/p>\n<p>Creating a custom transport based on a built-in driver lets you deliver matching emails to your own application. You can then deal with each email individually, outside of Exim.<\/p>\n<p>Unlike routers the order of transports does not matter. Each incoming email will match to the single transport specified by the router that accepts it. Add your transport anywhere in the <code>TRANSPORTS CONFIGURATION<\/code> section of your Exim file. Debian users with split configuration enabled can create a file in <code>\/etc\/exim4\/conf.d\/transports<\/code>.<\/p>\n<p>Here\u2019s a transport that invokes a PHP script:<\/p>\n<pre>example_transport:&#13;\n  driver = pipe&#13;\n  command = \/usr\/bin\/php \/var\/www\/html\/handle_incoming_email.php&#13;\n  user = www-data&#13;\n  group = www-data<\/pre>\n<p>When combined with the example router from above, any email sent to <code>@example.com<\/code> will be delivered to <code>handle_incoming_email.php<\/code>. Exim supplies the entire email in RFC-compliant format as the command\u2019s standard input for you to read in your programming language.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co1\">\/\/ Get email content from standard input in PHP<\/span>\n<span class=\"re0\">$email<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw3\">fopen<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"php:\/\/stdin\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"r\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"co1\">\/\/ To: user@example.com<\/span>\n<span class=\"co1\">\/\/ Subject: Demo Email<\/span>\n<span class=\"co1\">\/\/ <\/span>\n<span class=\"co1\">\/\/ This is an email.<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The transport is configured with the <code>pipe<\/code> driver. This <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.exim.org\/exim-html-current\/doc\/html\/spec_html\/ch-the_pipe_transport.html\">uses a Unix pipe<\/a> to supply the incoming email to your <code>command<\/code>. The command will be executed as the user and group you specify.<\/p>\n<h2 id=\"using-environment-variables\"><span class=\"ez-toc-section\" id=\"Using_Environment_Variables\"><\/span>Using Environment Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Exim will set several <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.exim.org\/exim-html-current\/doc\/html\/spec_html\/ch-the_pipe_transport.html\">environment variables<\/a> before executing your command. These can be used to access information about the message without having to parse the full email format.<\/p>\n<p>Available variables include:<\/p>\n<ul>\n<li><code>DOMAIN<\/code> \u2013 The domain the incoming email was sent to.<\/li>\n<li><code>MESSAGE_ID<\/code> \u2013 Exim\u2019s internal ID representing the email.<\/li>\n<li><code>RECIPIENT<\/code> \u2013 The email address which the message was sent to.<\/li>\n<li><code>SENDER<\/code> \u2013 The email address which the message originated from.<\/li>\n<\/ul>\n<p>You can add additional variables to the command\u2019s environment by setting the <code>environment<\/code> option in your transport. This accepts a comma-separated list of key-value pairs:<\/p>\n<pre>example_transport:&#13;\n  environment = foo=bar,demo=example<\/pre>\n<h2 id=\"return-values\"><span class=\"ez-toc-section\" id=\"Return_Values\"><\/span>Return Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Your command should exit with a <code>0<\/code> status code to confirm a successful delivery. A non-zero exit code will be interpreted as a failed delivery. The sender will receive a bounce message.<\/p>\n<p>This behavior is disabled by setting the <code>ignore_status<\/code> option in your transport. Exim will then treat non-zero status codes as successful.<\/p>\n<p>In addition, you can use the <code>temp_errors<\/code> option to define script exit codes which indicate an error was encountered but is expected to be temporary. When Exim sees one of these status codes, message delivery will be deferred and retried later on.<\/p>\n<p>The content of bounce messages can be customized with the <code>return_fail_output<\/code> option. When this is set to <code>true<\/code>, Exim will include the contents of your script\u2019s standard output in its bounce email. The mechanism facilitates use of your application code to deliver personalized bounce responses to senders.<\/p>\n<h2 id=\"command-invocation\"><span class=\"ez-toc-section\" id=\"Command_Invocation\"><\/span>Command Invocation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Exim usually invokes your command directly from the transport. If you set the <code>use_shell<\/code> option, it will pass the command to <code>\/bin\/sh<\/code> instead. This can be useful in scenarios where your script requires a full shell environment to be available.<\/p>\n<p>Command execution times out after one hour with the default configuration. A timeout is treated as a delivery error. The command will be terminated and a bounce message returned to the sender. The timeout can be customized with the <code>timeout<\/code> setting. Additionally setting the <code>timeout_defer<\/code> option will cause timeouts to be treated as temporary errors, allowing another delivery attempt after a delay.<\/p>\n<pre>example_transport:&#13;\n  timeout = 5m&#13;\n  timeout_defer<\/pre>\n<p>You should also consider that your script might be executed concurrently if multiple emails arrive at the same time. Your application should be able to withstand this possibility. Locking systems that allow only one instance to run simultaneously should be disabled for your email handling script.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Exim is easily configured to route incoming email to your own application. A simple setup that directs all received messages can be achieved with a basic router coupled to a transport using the <code>pipe<\/code> driver.<\/p>\n<p>You need to provide a binary that accepts an email on standard input as part of your application. The binary will be invoked by Exim whenever a new message is received. You should then parse the email in a standards-compliant manner to pull out the body content and message headers relevant to your system.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14186\/how-to-route-incoming-email-to-your-application-with-exim\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Route Incoming Email to Your Application With Exim \u2013 CloudSavvy IT&#8221; dwori\/Shutterstock.com Exim is a popular message transfer agent (MTA) for Unix systems. It offers a wide variety of routing and transportation options. In this article, we\u2019ll show how to use Exim to pipe incoming emails into your own script. We\u2019ll assume you\u2019ve&#8230;<\/p>\n","protected":false},"author":1,"featured_media":339570,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/6d08103b.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-339569","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/339569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=339569"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/339569\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/339570"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=339569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=339569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=339569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}