ReceiveEmail.php 9.2 KB

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