ReceiveEmail.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\AdditionalUsername;
  4. use App\Alias;
  5. use App\Domain;
  6. use App\EmailData;
  7. use App\Mail\ForwardEmail;
  8. use App\Mail\ReplyToEmail;
  9. use App\Notifications\NearBandwidthLimit;
  10. use App\User;
  11. use Illuminate\Console\Command;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Mail;
  14. use Illuminate\Support\Facades\Redis;
  15. use Illuminate\Support\Str;
  16. use PhpMimeMailParser\Parser;
  17. class ReceiveEmail extends Command
  18. {
  19. /**
  20. * The name and signature of the console command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'anonaddy:receive-email
  25. {file=stream : The file of the email}
  26. {--sender= : The sender of the email}
  27. {--recipient=* : The recipient of the email}
  28. {--local_part=* : The local part of the recipient}
  29. {--extension=* : The extension of the local part of the recipient}
  30. {--domain=* : The domain of the recipient}
  31. {--size= : The size of the email in bytes}';
  32. /**
  33. * The console command description.
  34. *
  35. * @var string
  36. */
  37. protected $description = 'Receive email from postfix pipe';
  38. protected $parser;
  39. protected $size;
  40. /**
  41. * Create a new command instance.
  42. *
  43. * @return void
  44. */
  45. public function __construct()
  46. {
  47. parent::__construct();
  48. }
  49. /**
  50. * Execute the console command.
  51. *
  52. * @return mixed
  53. */
  54. public function handle()
  55. {
  56. try {
  57. $this->exitIfFromSelf();
  58. $file = $this->argument('file');
  59. $this->parser = $this->getParser($file);
  60. $recipients = $this->getRecipients();
  61. // Divide the size of the email by the number of recipients (excluding any unsubscribe recipients) to prevent it being added multiple times.
  62. $recipientCount = $recipients->where('domain', '!=', 'unsubscribe.'.config('anonaddy.domain'))->count();
  63. $this->size = $this->option('size') / ($recipientCount ? $recipientCount : 1);
  64. foreach ($recipients as $key => $recipient) {
  65. $displayTo = $this->parser->getAddresses('to')[$key]['display'] ?? null;
  66. $parentDomain = collect(config('anonaddy.all_domains'))
  67. ->filter(function ($name) use ($recipient) {
  68. return Str::endsWith($recipient['domain'], $name);
  69. })
  70. ->first();
  71. if ($parentDomain) {
  72. $subdomain = substr($recipient['domain'], 0, strrpos($recipient['domain'], '.'.$parentDomain));
  73. if ($subdomain === 'unsubscribe') {
  74. $this->handleUnsubscribe($recipient);
  75. continue;
  76. }
  77. // Check if this is an additional username.
  78. if ($additionalUsername = AdditionalUsername::where('username', $subdomain)->first()) {
  79. $user = $additionalUsername->user;
  80. $aliasable = $additionalUsername;
  81. } else {
  82. $user = User::where('username', $subdomain)->first();
  83. }
  84. }
  85. if (!isset($user)) {
  86. // Check if this is a custom domain.
  87. if ($customDomain = Domain::where('domain', $recipient['domain'])->first()) {
  88. $user = $customDomain->user;
  89. $aliasable = $customDomain;
  90. }
  91. // Check if this is a uuid generated alias.
  92. if ($alias = Alias::find($recipient['local_part'])) {
  93. $user = $alias->user;
  94. } elseif ($recipient['domain'] === $parentDomain && !empty(config('anonaddy.admin_username'))) {
  95. $user = User::where('username', config('anonaddy.admin_username'))->first();
  96. }
  97. }
  98. // If there is still no user or the user has no verified default recipient then continue.
  99. if (!isset($user) || !$user->hasVerifiedDefaultRecipient()) {
  100. continue;
  101. }
  102. $this->checkBandwidthLimit($user);
  103. $this->checkRateLimit($user);
  104. // Check whether this email is a reply or a new email to be forwarded.
  105. if ($recipient['extension'] === sha1(config('anonaddy.secret').$displayTo)) {
  106. $this->handleReply($user, $recipient, $displayTo);
  107. } else {
  108. $this->handleForward($user, $recipient, $aliasable ?? null);
  109. }
  110. }
  111. } catch (\Exception $e) {
  112. report($e);
  113. $this->error('4.3.0 An error has occurred, please try again later.');
  114. exit(1);
  115. }
  116. }
  117. protected function handleUnsubscribe($recipient)
  118. {
  119. $alias = Alias::find($recipient['local_part']);
  120. if (!is_null($alias) && $alias->user->isVerifiedRecipient($this->option('sender'))) {
  121. $alias->deactivate();
  122. }
  123. }
  124. protected function handleReply($user, $recipient, $displayTo)
  125. {
  126. $alias = $user->aliases()->where('email', $recipient['local_part'] . '@' . $recipient['domain'])->first();
  127. if (!is_null($alias) && filter_var($displayTo, FILTER_VALIDATE_EMAIL)) {
  128. $emailData = new EmailData($this->parser);
  129. $message = new ReplyToEmail($user, $alias, $emailData);
  130. Mail::to($displayTo)->queue($message);
  131. if (!Mail::failures()) {
  132. $alias->increment('emails_replied');
  133. $user->bandwidth += $this->size;
  134. $user->save();
  135. }
  136. }
  137. }
  138. protected function handleForward($user, $recipient, $aliasable)
  139. {
  140. $alias = $user->aliases()->firstOrNew([
  141. 'email' => $recipient['local_part'] . '@' . $recipient['domain'],
  142. 'local_part' => $recipient['local_part'],
  143. 'domain' => $recipient['domain'],
  144. 'aliasable_id' => $aliasable->id ?? null,
  145. 'aliasable_type' => $aliasable ? 'App\\'.class_basename($aliasable) : null
  146. ]);
  147. if (!isset($alias->id)) {
  148. // This is a new alias.
  149. if ($user->hasExceededNewAliasLimit()) {
  150. $this->error('4.2.1 New aliases per hour limit exceeded for user.');
  151. exit(1);
  152. }
  153. if ($recipient['extension'] !== '') {
  154. $alias->extension = $recipient['extension'];
  155. $keys = explode('.', $recipient['extension']);
  156. $recipientIds = $user
  157. ->recipients()
  158. ->oldest()
  159. ->get()
  160. ->filter(function ($item, $key) use ($keys) {
  161. return in_array($key+1, $keys) && !is_null($item['email_verified_at']);
  162. })
  163. ->pluck('id')
  164. ->take(10)
  165. ->toArray();
  166. }
  167. }
  168. $alias->save();
  169. $alias->refresh();
  170. if (isset($recipientIds)) {
  171. $alias->recipients()->sync($recipientIds);
  172. }
  173. $emailData = new EmailData($this->parser);
  174. $alias->verifiedRecipientsOrDefault()->each(function ($recipient) use ($alias, $emailData) {
  175. $message = new ForwardEmail($alias, $emailData, $recipient->should_encrypt ? $recipient->fingerprint : null);
  176. Mail::to($recipient->email)->queue($message);
  177. });
  178. if (!Mail::failures()) {
  179. $alias->increment('emails_forwarded');
  180. $user->bandwidth += $this->size;
  181. $user->save();
  182. }
  183. }
  184. protected function checkBandwidthLimit($user)
  185. {
  186. if ($user->hasReachedBandwidthLimit()) {
  187. $this->error('4.2.1 Bandwidth limit exceeded for user. Please try again later.');
  188. exit(1);
  189. }
  190. if ($user->nearBandwidthLimit() && ! Cache::has("user:{$user->username}:near-bandwidth")) {
  191. $user->notify(new NearBandwidthLimit());
  192. Cache::put("user:{$user->username}:near-bandwidth", now()->toDateTimeString(), now()->addDay());
  193. }
  194. }
  195. protected function checkRateLimit($user)
  196. {
  197. Redis::throttle("user:{$user->username}:limit:emails")
  198. ->allow(config('anonaddy.limit'))
  199. ->every(3600)
  200. ->then(
  201. function () {
  202. },
  203. function () {
  204. $this->error('4.2.1 Rate limit exceeded for user. Please try again later.');
  205. exit(1);
  206. }
  207. );
  208. }
  209. protected function getRecipients()
  210. {
  211. return collect($this->option('recipient'))->map(function ($item, $key) {
  212. return [
  213. 'email' => $item,
  214. 'local_part' => strtolower($this->option('local_part')[$key]),
  215. 'extension' => $this->option('extension')[$key],
  216. 'domain' => strtolower($this->option('domain')[$key])
  217. ];
  218. });
  219. }
  220. protected function getParser($file)
  221. {
  222. $parser = new Parser;
  223. if ($file == 'stream') {
  224. $fd = fopen('php://stdin', 'r');
  225. $this->rawEmail = '';
  226. while (!feof($fd)) {
  227. $this->rawEmail .= fread($fd, 1024);
  228. }
  229. fclose($fd);
  230. $parser->setText($this->rawEmail);
  231. } else {
  232. $parser->setPath($file);
  233. }
  234. return $parser;
  235. }
  236. protected function exitIfFromSelf()
  237. {
  238. // To prevent recipient alias infinite nested looping.
  239. if (in_array($this->option('sender'), [config('mail.from.address'), config('anonaddy.return_path')])) {
  240. exit(0);
  241. }
  242. }
  243. }