<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Imap on dotnet.work</title>
        <link>https://dotnet.work/tags/imap/</link>
        <description>Recent content in Imap on dotnet.work</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-US</language>
        <managingEditor>dotnet@oles.cloud (Ole Albers)</managingEditor>
        <webMaster>dotnet@oles.cloud (Ole Albers)</webMaster>
        <copyright>Ole Albers</copyright>
        <lastBuildDate>Fri, 13 Feb 2015 12:00:00 +0100</lastBuildDate><atom:link href="https://dotnet.work/tags/imap/index.xml" rel="self" type="application/rss+xml" /><item>
            <title>IMAP-HTML-Autoresponder with PHP</title>
            <link>https://dotnet.work/blog/imap-html-php-autoresponder-autoreply/</link>
            <pubDate>Fri, 13 Feb 2015 12:00:00 +0100</pubDate><author>dotnet@oles.cloud (Ole Albers)</author>
            <guid>https://dotnet.work/blog/imap-html-php-autoresponder-autoreply/</guid>
            <description>&lt;p&gt;Everyone knows spam. Everyone &lt;strong&gt;hates&lt;/strong&gt; spam. And everyone who has his email-address written on the net (for example in the NIC-records for a domain) gets &lt;strong&gt;a lot of&lt;/strong&gt; spam.&lt;/p&gt;&#xA;&lt;p&gt;I am one of those guys who gets a lot of spam. Of course there are anti-spam solutions out there, but the best would be to not even receive spam. And spam filters need a lot of work and / or do not filter 100% perfect. That is why I decided to use a public email that has to go through a &amp;ldquo;gatekeeper&amp;rdquo;. A PHP-script that tells everyone that this is not my real mail address, but that there is another one where I could be contacted.&lt;/p&gt;&#xA;&lt;p&gt;That email-address is not displayed in plain text but as an image to get rid of all those bots. SOME bots however should be able to get through: For example my hoster who needs to validate my email periodically. So these messages just are being forwarded.&lt;/p&gt;&#xA;&lt;p&gt;I wanted to be as simple as possible. That&amp;rsquo;s why this script does not need much more than plain php. Only a replacement for phps &amp;ldquo;mail&amp;rdquo; was needed.&lt;/p&gt;&#xA;&lt;p&gt;First step: Download swiftmailer. Just put the contents of the &amp;ldquo;lib&amp;rdquo; - directory onto your web space. That&amp;rsquo;s it. Ready to go.&lt;/p&gt;&#xA;&lt;p&gt;Simple include the following line to access swiftmailer:&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;require\_once &#39;./swift/lib/swift\_required.php&#39;;&lt;/code&gt;&#xA;To access your mail you need to enter the IMAP-Settings. POP would work, too, but IMAP would be much more useful because we do not want to download the email-body of the messages processed:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;// IMAP-Settings:&#xA; $hostname = &amp;#34;{imap.hostname.com:993/imap/ssl}INBOX&amp;#34;;&#xA; $username = &amp;#34;username&amp;#34;;&#xA; $password = &amp;#34;password&amp;#34;;&#xA;`Now enter your email-addresses. $targetMail is your real (&amp;#34;hidden&amp;#34;) email-address where valid emails should be forwarded to. $thisMail is the public address that receives the spam.`&#xA;$targetMail=&amp;#34;private@mydomain.com&amp;#34;; &#xA;$thisMail=&amp;#34;webmaster@mydomain.com&amp;#34;;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next you need to create an image with your favourite picture editing tool (GiMP, Paint.NET, PaintShopPro, etc.) This image should only contain your private email. Take a font you like. You might also use a &amp;ldquo;Captcha&amp;rdquo;-font. I - personally - hate that&amp;hellip; :)&lt;/p&gt;&#xA;&lt;p&gt;Upload that image to your web server and enter the url of that image. PNG would be a good choice.&lt;/p&gt;&#xA;&lt;p&gt;$image=&amp;ldquo;&lt;a class=&#34;link&#34; href=&#34;http://www.example.com/images/mail.png%22;&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;&#xA;    &gt;http://www.example.com/images/mail.png&#34;;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;Next, enter the text the sender should receive. By default everyone receives $templateHtml. For those who do not have the capabilities to display html (or don&amp;rsquo;t want to) a simple text-mail ($templatePlain) will be displayed. Users with a html-browser will see your email-image directly in the mail. Plain-text mail-clienst will display a link to the image instead.&lt;/p&gt;&#xA;&lt;p&gt;$templateForward defines the mail content YOU will see if a mail is forwarded to you from a valid sender&lt;/p&gt;&#xA;&lt;p&gt;So it is time to be creative :)&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$templateHtml=&amp;#34;&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;Thanks for your mail. Please note that this mail is no more. It has ceased to be! It is expired and gone to meet its maker! This is an Ex-Mail!&amp;lt;br/&amp;gt;But don&amp;#39;t be afraid, there is a replacement for that. Just send your mail to: &amp;lt;br/&amp;gt;&amp;lt;img src=\\&amp;#34;\_\_EMBED\_\_\\&amp;#34;/&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;If you cannot see that image: Click onto the following link to see my: &amp;lt;a href=\\&amp;#34;\_\_URL\_\_\\&amp;#34;&amp;gt;new mail-address&amp;lt;/a&amp;gt;&amp;lt;br/&amp;gt;And no: This mail will not be forwarded. No one will see this until you forward it to my new mail-address. Thanks!&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;#34;;&#xA;&#xA;$templatePlain=&amp;#34;Thanks for your mail. Please note that this mail is no more. It has ceased to be! It is expired and gone to meet its maker! This is an Ex-Mail!\\r\\nBut don&amp;#39;t be afraid, there is a replacement for that. Just click onto the following link to see my: new mail-address: \_\_URL\_\_&amp;#34;;&#xA;&#xA;$templateForward=&amp;#34;new message redirected&amp;#34;;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;As you can see there are two special tags in those templates. &amp;ldquo;__EMBED__&amp;rdquo; will be replaced with the embedded image (html-template only) and &amp;ldquo;__URL__&amp;rdquo; will contain a link to that image.&lt;/p&gt;&#xA;&lt;p&gt;The last variable you can define is the white-list. Everything entered there is interpreted as a valid sender. Enter multiple values seperated by commas.&#xA;&lt;code&gt;$whitelist = &amp;quot;uberspace,denic.de,domainbox.net,romrobot.com,inwx.de,millerntor.hamburg,dotnet.work&amp;quot;;&lt;/code&gt;&#xA;that&amp;rsquo;s all for configuration. Now just some explanation what the script does:&lt;/p&gt;&#xA;&lt;p&gt;First of all the script checks your IMAP-Server for unread messages. It collects the header of each of those messages and compares the sender with the white-list. If the sender is recognized, the mail body is downloaded and sent as an attachment to the mail address you defined as $targetMail.&lt;/p&gt;&#xA;&lt;p&gt;If the sender is not in the white-list, an automatic reply is generated using your html-template. It contains the image you downloaded as an attachment and embedded image of the mail. The last step is to mark the mail as read so it won&amp;rsquo;t be processed again.&lt;/p&gt;&#xA;&lt;p&gt;In a perfect world&amp;hellip;&lt;/p&gt;&#xA;&lt;p&gt;Everything works now. But you can (and will) get problems sending the mail to the sender if the sender&amp;rsquo;s address does not comply with RFC 2822. While most spammers DO comply, mom DOESN&amp;rsquo;T&amp;hellip; :) That&amp;rsquo;s because there is a mail client from a tiny little software company in Redmond, where they do not know much about standards. But this is no big problem at all. Just open &lt;em&gt;lib\classes\swift\Mime\Headers\MailboxHeader.php&lt;/em&gt; from swiftmailer and put &lt;em&gt;/* */&lt;/em&gt; around the throw-part of the last function:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;private function \_assertValidAddress($address)&#xA;{&#xA;   if (!preg\_match(&amp;#39;/^&amp;#39;.$this-&amp;gt;getGrammar()-&amp;gt;getDefinition(&amp;#39;addr-spec&amp;#39;).&amp;#39;$/D&amp;#39;, $address)) {&#xA;     /\* throw new Swift\_RfcComplianceException(&amp;#39;Address in mailbox given \[&amp;#39;.$address.&amp;#39;\] does not comply with RFC 2822, 3.6.2.&amp;#39;);\*/&#xA;   }&#xA;}&#xA;`So finally here is the complete script.`&#xA;&amp;lt;?php&#xA;   require\_once &amp;#39;./swift/lib/swift\_required.php&amp;#39;;// IMAP-Settings:&#xA;   $hostname = &amp;#34;{imap.example.com:993/imap/ssl}INBOX&amp;#34;;&#xA;   $username = &amp;#34;username&amp;#34;;&#xA;   $password = &amp;#34;password&amp;#34;; // Script-Settings:&#xA;   $targetMail=&amp;#34;private@example.com&amp;#34;; // &amp;#34;hidden&amp;#34; target mail-address&#xA;   $thisMail=&amp;#34;webmaster@example.com&amp;#34;; // &amp;#34;official&amp;#34; mail-address (which receives the spam) &#xA;   $image=&amp;#34;http://www.dotnet.work/reply/mail.png&amp;#34;; // Image to be shown&#xA;   $templateHtml=&amp;#34;&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;Thanks for your mail. Please note that this mail is no more. It has ceased to be! It is expired and gone to meet its maker! This is an Ex-Mail!&amp;lt;br/&amp;gt;But don&amp;#39;t be afraid, there is a replacement for that. Just send your mail to: &amp;lt;br/&amp;gt;&amp;lt;img src=\\&amp;#34;\_\_EMBED\_\_\\&amp;#34;/&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;If you cannot see that image: Click onto the following link to see my: &amp;lt;a href=\\&amp;#34;\_\_URL\_\_\\&amp;#34;&amp;gt;new mail-address&amp;lt;/a&amp;gt;&amp;lt;br/&amp;gt;And no: This mail will not be forwarded. No one will see this until you forward it to my new mail-address. Thanks!&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;#34;;&#xA;   $templatePlain=&amp;#34;Thanks for your mail. Please note that this mail is no more. It has ceased to be! It is expired and gone to meet its maker! This is an Ex-Mail!\\r\\nBut don&amp;#39;t be afraid, there is a replacement for that. Just click onto the following link to see my: new mail-address: \_\_URL\_\_&amp;#34;;&#xA;   $templateForward=&amp;#34;new message redirected&amp;#34;;&#xA;&#xA;   $whitelist = &amp;#34;uberspace,denic.de,domainbox.net,romrobot.com,inwx.de,millerntor.hamburg,dotnet.work&amp;#34;;&#xA; &#xA;   $allowedSenders=explode(&amp;#34;,&amp;#34;,$whitelist);&#xA;   $inbox = imap\_open($hostname,$username,$password) or die(&amp;#34;Sorry, connection failed&amp;#34;);&#xA;   $emails = imap\_search($inbox,&amp;#34;UNSEEN&amp;#34;);&#xA;   $count=0; &#xA;   echo &amp;#34;&amp;lt;pre&amp;gt;&amp;#34;; &#xA;   if($emails) {&#xA;     $transport = Swift\_MailTransport::newInstance();&#xA;     foreach($emails as $email) {&#xA;        $count++;&#xA;        $header = imap\_fetch\_overview($inbox,$email,0); &#xA;        $subject=$header\[0\]-&amp;gt;subject;&#xA;        $sender=$header\[0\]-&amp;gt;from;&#xA;&#xA;        $messageHeader =  imap\_fetchheader($inbox, 1)&#xA;        if (strpos($messageHeader,&amp;#34;X-RESPONDERDATE&amp;#34;)!==false) {&#xA;          continue; // no wild bouncing, please&#xA;        }&#xA;&#xA;        if (strpos($sender,$targetMail!==false || strpos($sender,$thisMail!==false) {&#xA;           // We want no loops... :)&#xA;           continue;&#xA;        }&#xA;        echo &amp;#34;retrieving &amp;#34;.$subject.&amp;#34; from &amp;#34;.$sender.&amp;#34;\\r\\n&amp;#34;;&#xA;        // Whitelist: &#xA;        $isWhiteListed=false;&#xA;      &#xA;        foreach($allowedSenders as $allowedSender) {&#xA;           if (stripos($sender,$allowedSender)!==false) {&#xA;              $isWhiteListed=true;&#xA;              break;&#xA;           }&#xA;        }&#xA; &#xA;        if ($isWhiteListed) {&#xA;           $email\_body = imap\_body($inbox, $email);&#xA;           $mailer = Swift\_Mailer::newInstance($transport);&#xA;           $message = Swift\_Message::newInstance()&#xA;              -&amp;gt;setSubject(&amp;#34;Fw:&amp;#34;.$subject)&#xA;              -&amp;gt;setFrom($sender)&#xA;              -&amp;gt;setTo($targetMail)&#xA;              -&amp;gt;setBody($templateForward)&#xA;           ;&#xA; &#xA;           $attachment = Swift\_Attachment::newInstance($email\_body, &amp;#39;origin.eml&amp;#39;, &amp;#39;message/rfc822&amp;#39;); &#xA;           $message-&amp;gt;attach($attachment);&#xA; &#xA;           $result = $mailer-&amp;gt;send($message);&#xA;           echo &amp;#34;whitelist redirect\\r\\n&amp;#34;;&#xA;        } else { &#xA;           $mailer = Swift\_Mailer::newInstance($transport);&#xA;           $message = Swift\_Message::newInstance();&#xA;           $swiftheaders = $message-&amp;gt;getHeaders();&#xA;           $swiftheaders-&amp;gt;addTextHeader(&amp;#39;X-RESPONDERDATE&amp;#39;, date(&amp;#39;Y-m-d H:i:s&amp;#39;));&#xA;           $cid = $message-&amp;gt;embed(Swift\_Image::fromPath($image));&#xA;           $html=str\_replace(&amp;#34;\_\_URL\_\_&amp;#34;,$image,$templateHtml);&#xA;           $html=str\_replace(&amp;#34;\_\_EMBED\_\_&amp;#34;,$cid,$html);&#xA;           $plain=str\_replace(&amp;#34;\_\_URL\_\_&amp;#34;,$image,$templatePlain);&#xA;           $message&#xA;              -&amp;gt;setSubject(&amp;#34;Re:&amp;#34;.$subject)&#xA;              -&amp;gt;setFrom($thisMail)&#xA;              -&amp;gt;setTo($sender) &#xA;              -&amp;gt;setBody($html, &amp;#39;text/html&amp;#39;)&#xA;              -&amp;gt;addPart($plain, &amp;#39;text/plain&amp;#39;)&#xA;           ;&#xA;           $result = $mailer-&amp;gt;send($message);&#xA;           echo &amp;#34;answered\\r\\n&amp;#34;;&#xA;        } &#xA;        imap\_setflag\_full($inbox, imap\_uid($inbox,$email), &amp;#34;\\\\Seen \\\\Flagged&amp;#34;, ST\_UID);&#xA;     } &#xA;  }&#xA;  imap\_close($inbox);&#xA;  echo &amp;#34;done redirecting &amp;#34;.$count.&amp;#34; mails\\r\\n&amp;#34;;&#xA;  echo &amp;#34;&amp;lt;/pre&amp;gt;&amp;#34;;&#xA;?&amp;gt;&#xA;`Just save this program as &amp;#34;reply.php&amp;#34;, send test-mails and open &amp;#34;reply.php&amp;#34; in your browser to test the settings. When everything works fine you can simply add a cronjob so this script is run automaticly. The following example runs the script every 5 minutes:`&#xA;\*/5 \* \* \* \* php /var/www/reply.php&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt; &lt;/p&gt;&#xA;&lt;p&gt;Just use the comments if you need any help&lt;/p&gt;&#xA;</description>
        </item></channel>
</rss>
