فهرست منبع

Replies to UUID aliases now come from the alias

Will Browning 5 سال پیش
والد
کامیت
f2ff02e155
4فایلهای تغییر یافته به همراه59 افزوده شده و 6 حذف شده
  1. 1 1
      README.md
  2. 4 1
      app/Console/Commands/ReceiveEmail.php
  3. 9 4
      app/Mail/ReplyToEmail.php
  4. 45 0
      tests/Feature/ReceiveEmailTest.php

+ 1 - 1
README.md

@@ -192,7 +192,7 @@ I am very passionite about this project. I use it myself everyday and will be ke
 
 #### **Is the application tested?**
 
-Yes it has over 100 automated PHPUnit tests written.
+Yes it has over 130 automated PHPUnit tests written.
 
 #### **How do I host this myself?**
 

+ 4 - 1
app/Console/Commands/ReceiveEmail.php

@@ -11,6 +11,7 @@ use App\Mail\ReplyToEmail;
 use App\Notifications\NearBandwidthLimit;
 use App\User;
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Mail;
 use Illuminate\Support\Facades\Redis;
 use Illuminate\Support\Str;
@@ -231,8 +232,10 @@ class ReceiveEmail extends Command
             exit(1);
         }
 
-        if ($user->nearBandwidthLimit()) {
+        if ($user->nearBandwidthLimit() && ! Cache::has("user:{$user->username}:near-bandwidth")) {
             $user->notify(new NearBandwidthLimit());
+
+            Cache::put("user:{$user->username}:near-bandwidth", now()->toDateTimeString(), now()->addDay());
         }
     }
 

+ 9 - 4
app/Mail/ReplyToEmail.php

@@ -44,19 +44,24 @@ class ReplyToEmail extends Mailable implements ShouldQueue
     public function build()
     {
         $fromName = $this->user->from_name ? $this->user->from_name : $this->alias->email.' via '.config('app.name');
+        $fromAddress = $this->alias->isUuid() ? $this->alias->email : config('mail.from.address');
+        $returnPath = $this->alias->isUuid() ? $this->alias->email : config('anonaddy.return_path');
 
         $email =  $this
-            ->from(config('mail.from.address'), $fromName)
-            ->replyTo($this->alias->email, $fromName)
+            ->from($fromAddress, $fromName)
             ->subject(base64_decode($this->emailSubject))
             ->text('emails.reply.text')->with([
                 'text' => base64_decode($this->emailText)
             ])
-            ->withSwiftMessage(function ($message) {
+            ->withSwiftMessage(function ($message) use ($returnPath) {
                 $message->getHeaders()
-                        ->addTextHeader('Return-Path', config('anonaddy.return_path'));
+                        ->addTextHeader('Return-Path', $returnPath);
             });
 
+        if (! $this->alias->isUuid()) {
+            $email->replyTo($this->alias->email, $fromName);
+        }
+
         if ($this->emailHtml) {
             $email->view('emails.reply.html')->with([
                 'html' => base64_decode($this->emailHtml)

+ 45 - 0
tests/Feature/ReceiveEmailTest.php

@@ -11,6 +11,7 @@ use App\Notifications\NearBandwidthLimit;
 use App\Recipient;
 use App\User;
 use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Mail;
 use Illuminate\Support\Facades\Notification;
 use Tests\TestCase;
@@ -896,6 +897,50 @@ class ReceiveEmailTest extends TestCase
         );
     }
 
+    /** @test */
+    public function it_does_not_send_near_bandwidth_limit_notification_more_than_once_per_day()
+    {
+        Notification::fake();
+
+        Notification::assertNothingSent();
+
+        Cache::put("user:{$this->user->username}:near-bandwidth", now()->toDateTimeString(), now()->addDay());
+
+        $this->user->update(['bandwidth' => 9485760]);
+
+        $this->artisan(
+            'anonaddy:receive-email',
+            [
+                'file' => base_path('tests/emails/email.eml'),
+                '--sender' => 'will@anonaddy.com',
+                '--recipient' => ['ebay@johndoe.anonaddy.com'],
+                '--local_part' => ['ebay'],
+                '--extension' => [''],
+                '--domain' => ['johndoe.anonaddy.com'],
+                '--size' => '1000'
+            ]
+        )->assertExitCode(0);
+
+        $this->assertDatabaseHas('aliases', [
+            'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
+            'local_part' => 'ebay',
+            'domain' => 'johndoe.'.config('anonaddy.domain'),
+            'emails_forwarded' => 1,
+            'emails_blocked' => 0
+        ]);
+        $this->assertEquals(1, $this->user->aliases()->count());
+        $this->assertDatabaseHas('users', [
+            'id' => $this->user->id,
+            'username' => 'johndoe',
+            'bandwidth' => '9486760'
+        ]);
+
+        Notification::assertNotSentTo(
+            $this->user,
+            NearBandwidthLimit::class
+        );
+    }
+
     /** @test */
     public function it_can_forward_email_from_file_for_all_domains()
     {