ReceiveEmail.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Mail\ForwardEmail;
  4. use App\Mail\ReplyToEmail;
  5. use App\Mail\SendFromEmail;
  6. use App\Models\AdditionalUsername;
  7. use App\Models\Alias;
  8. use App\Models\Domain;
  9. use App\Models\EmailData;
  10. use App\Models\PostfixQueueId;
  11. use App\Models\Recipient;
  12. use App\Models\User;
  13. use App\Notifications\DisallowedReplySendAttempt;
  14. use App\Notifications\FailedDeliveryNotification;
  15. use App\Notifications\NearBandwidthLimit;
  16. use App\Notifications\SpamReplySendAttempt;
  17. use Illuminate\Console\Command;
  18. use Illuminate\Support\Facades\Cache;
  19. use Illuminate\Support\Facades\Log;
  20. use Illuminate\Support\Facades\Mail;
  21. use Illuminate\Support\Str;
  22. use PhpMimeMailParser\Parser;
  23. class ReceiveEmail extends Command
  24. {
  25. /**
  26. * The name and signature of the console command.
  27. *
  28. * @var string
  29. */
  30. protected $signature = 'anonaddy:receive-email
  31. {file=stream : The file of the email}
  32. {--sender= : The sender of the email}
  33. {--recipient=* : The recipient of the email}
  34. {--local_part=* : The local part of the recipient}
  35. {--extension=* : The extension of the local part of the recipient}
  36. {--domain=* : The domain of the recipient}
  37. {--size= : The size of the email in bytes}';
  38. /**
  39. * The console command description.
  40. *
  41. * @var string
  42. */
  43. protected $description = 'Receive email from postfix pipe';
  44. protected $parser;
  45. protected $senderFrom;
  46. protected $size;
  47. /**
  48. * Create a new command instance.
  49. *
  50. * @return void
  51. */
  52. public function __construct()
  53. {
  54. parent::__construct();
  55. }
  56. /**
  57. * Execute the console command.
  58. *
  59. * @return mixed
  60. */
  61. public function handle()
  62. {
  63. try {
  64. $this->exitIfFromSelf();
  65. $file = $this->argument('file');
  66. $this->parser = $this->getParser($file);
  67. $this->senderFrom = $this->getSenderFrom();
  68. $recipients = $this->getRecipients();
  69. // Divide the size of the email by the number of recipients (excluding any unsubscribe recipients) to prevent it being added multiple times.
  70. $recipientCount = $recipients->where('domain', '!=', 'unsubscribe.'.config('anonaddy.domain'))->count();
  71. $this->size = $this->option('size') / ($recipientCount ? $recipientCount : 1);
  72. foreach ($recipients as $recipient) {
  73. // Handle bounces
  74. if ($this->option('sender') === 'MAILER-DAEMON') {
  75. $this->handleBounce($recipient['email']);
  76. }
  77. // First determine if the alias already exists in the database
  78. if ($alias = Alias::firstWhere('email', $recipient['local_part'] . '@' . $recipient['domain'])) {
  79. $user = $alias->user;
  80. if ($alias->aliasable_id) {
  81. $aliasable = $alias->aliasable;
  82. }
  83. } else {
  84. // Does not exist, must be a standard, additional username or custom domain alias
  85. $parentDomain = collect(config('anonaddy.all_domains'))
  86. ->filter(function ($name) use ($recipient) {
  87. return Str::endsWith($recipient['domain'], $name);
  88. })
  89. ->first();
  90. if (!empty($parentDomain)) {
  91. // It is standard or additional username alias
  92. $subdomain = substr($recipient['domain'], 0, strrpos($recipient['domain'], '.' . $parentDomain)); // e.g. johndoe
  93. if ($subdomain === 'unsubscribe') {
  94. $this->handleUnsubscribe($recipient);
  95. continue;
  96. }
  97. // Check if this is an additional username or standard alias
  98. if (!empty($subdomain)) {
  99. $user = User::where('username', $subdomain)->first();
  100. if (!isset($user)) {
  101. $additionalUsername = AdditionalUsername::where('username', $subdomain)->first();
  102. $user = $additionalUsername->user;
  103. $aliasable = $additionalUsername;
  104. }
  105. }
  106. } else {
  107. // It is a custom domain
  108. if ($customDomain = Domain::where('domain', $recipient['domain'])->first()) {
  109. $user = $customDomain->user;
  110. $aliasable = $customDomain;
  111. }
  112. }
  113. if (!isset($user) && !empty(config('anonaddy.admin_username'))) {
  114. $user = User::where('username', config('anonaddy.admin_username'))->first();
  115. }
  116. }
  117. // If there is still no user or the user has no verified default recipient then continue.
  118. if (!isset($user) || !$user->hasVerifiedDefaultRecipient()) {
  119. continue;
  120. }
  121. $this->checkBandwidthLimit($user);
  122. $this->checkRateLimit($user);
  123. // Check whether this email is a reply/send from or a new email to be forwarded.
  124. $destination = Str::replaceLast('=', '@', $recipient['extension']);
  125. $validEmailDestination = filter_var($destination, FILTER_VALIDATE_EMAIL);
  126. $verifiedRecipient = $user->getVerifiedRecipientByEmail($this->senderFrom);
  127. if ($validEmailDestination && $verifiedRecipient?->can_reply_send) {
  128. // Check if the Dmarc allow or spam headers are present from Rspamd
  129. if (! $this->parser->getHeader('X-AnonAddy-Dmarc-Allow') || $this->parser->getHeader('X-AnonAddy-Spam')) {
  130. // Notify user and exit
  131. $verifiedRecipient->notify(new SpamReplySendAttempt($recipient, $this->senderFrom, $this->parser->getHeader('X-AnonAddy-Authentication-Results')));
  132. exit(0);
  133. }
  134. if ($this->parser->getHeader('In-Reply-To')) {
  135. $this->handleReply($user, $recipient);
  136. } else {
  137. $this->handleSendFrom($user, $recipient, $aliasable ?? null);
  138. }
  139. } elseif ($validEmailDestination && $verifiedRecipient?->can_reply_send === false) {
  140. // Notify user that they have not allowed this recipient to reply and send from aliases
  141. $verifiedRecipient->notify(new DisallowedReplySendAttempt($recipient, $this->senderFrom, $this->parser->getHeader('X-AnonAddy-Authentication-Results')));
  142. exit(0);
  143. } else {
  144. $this->handleForward($user, $recipient, $aliasable ?? null);
  145. }
  146. }
  147. } catch (\Exception $e) {
  148. report($e);
  149. $this->error('4.3.0 An error has occurred, please try again later.');
  150. exit(1);
  151. }
  152. }
  153. protected function handleUnsubscribe($recipient)
  154. {
  155. $alias = Alias::find($recipient['local_part']);
  156. if ($alias && $alias->user->isVerifiedRecipient($this->senderFrom) && $this->parser->getHeader('X-AnonAddy-Dmarc-Allow')) {
  157. $alias->deactivate();
  158. }
  159. }
  160. protected function handleReply($user, $recipient)
  161. {
  162. $alias = $user->aliases()->where('email', $recipient['local_part'] . '@' . $recipient['domain'])->first();
  163. if ($alias) {
  164. $sendTo = Str::replaceLast('=', '@', $recipient['extension']);
  165. $emailData = new EmailData($this->parser, $this->size);
  166. $message = new ReplyToEmail($user, $alias, $emailData);
  167. Mail::to($sendTo)->queue($message);
  168. }
  169. }
  170. protected function handleSendFrom($user, $recipient, $aliasable)
  171. {
  172. $alias = $user->aliases()->withTrashed()->firstOrNew([
  173. 'email' => $recipient['local_part'] . '@' . $recipient['domain'],
  174. 'local_part' => $recipient['local_part'],
  175. 'domain' => $recipient['domain'],
  176. 'aliasable_id' => $aliasable->id ?? null,
  177. 'aliasable_type' => $aliasable ? 'App\\Models\\' . class_basename($aliasable) : null
  178. ]);
  179. // This is a new alias but at a shared domain or the sender is not a verified recipient.
  180. if (!isset($alias->id) && in_array($recipient['domain'], config('anonaddy.all_domains'))) {
  181. exit(0);
  182. }
  183. $alias->save();
  184. $alias->refresh();
  185. $sendTo = Str::replaceLast('=', '@', $recipient['extension']);
  186. $emailData = new EmailData($this->parser, $this->size);
  187. $message = new SendFromEmail($user, $alias, $emailData);
  188. Mail::to($sendTo)->queue($message);
  189. }
  190. protected function handleForward($user, $recipient, $aliasable)
  191. {
  192. $alias = $user->aliases()->withTrashed()->firstOrNew([
  193. 'email' => $recipient['local_part'] . '@' . $recipient['domain'],
  194. 'local_part' => $recipient['local_part'],
  195. 'domain' => $recipient['domain'],
  196. 'aliasable_id' => $aliasable->id ?? null,
  197. 'aliasable_type' => $aliasable ? 'App\\Models\\' . class_basename($aliasable) : null
  198. ]);
  199. if (!isset($alias->id)) {
  200. // This is a new alias.
  201. if ($user->hasExceededNewAliasLimit()) {
  202. $this->error('4.2.1 New aliases per hour limit exceeded for user.');
  203. exit(1);
  204. }
  205. if ($recipient['extension'] !== '') {
  206. $alias->extension = $recipient['extension'];
  207. $keys = explode('.', $recipient['extension']);
  208. $recipientIds = $user
  209. ->recipients()
  210. ->oldest()
  211. ->get()
  212. ->filter(function ($item, $key) use ($keys) {
  213. return in_array($key+1, $keys) && !is_null($item['email_verified_at']);
  214. })
  215. ->pluck('id')
  216. ->take(10)
  217. ->toArray();
  218. }
  219. }
  220. $alias->save();
  221. $alias->refresh();
  222. if (isset($recipientIds)) {
  223. $alias->recipients()->sync($recipientIds);
  224. }
  225. $emailData = new EmailData($this->parser, $this->size);
  226. $alias->verifiedRecipientsOrDefault()->each(function ($recipient) use ($alias, $emailData) {
  227. $message = new ForwardEmail($alias, $emailData, $recipient);
  228. Mail::to($recipient->email)->queue($message);
  229. });
  230. }
  231. protected function handleBounce($returnPath)
  232. {
  233. // Collect the attachments
  234. $attachments = collect($this->parser->getAttachments());
  235. // Find the delivery report
  236. $deliveryReport = $attachments->filter(function ($attachment) {
  237. return $attachment->getContentType() === 'message/delivery-status';
  238. })->first();
  239. if ($deliveryReport) {
  240. $dsn = $this->parseDeliveryStatus($deliveryReport->getMimePartStr());
  241. // Verify queue ID
  242. if (isset($dsn['X-postfix-queue-id'])) {
  243. // First check in DB
  244. $postfixQueueId = PostfixQueueId::firstWhere('queue_id', strtoupper($dsn['X-postfix-queue-id']));
  245. if (!$postfixQueueId) {
  246. exit(0);
  247. }
  248. // If found then delete from DB
  249. $postfixQueueId->delete();
  250. } else {
  251. exit(0);
  252. }
  253. // Get the bounced email address
  254. $bouncedEmailAddress = isset($dsn['Final-recipient']) ? trim(Str::after($dsn['Final-recipient'], ';')) : '';
  255. $remoteMta = isset($dsn['Remote-mta']) ? trim(Str::after($dsn['Remote-mta'], ';')) : '';
  256. if (isset($dsn['Diagnostic-code']) && isset($dsn['Status'])) {
  257. // Try to determine the bounce type, HARD, SPAM, SOFT
  258. $bounceType = $this->getBounceType($dsn['Diagnostic-code'], $dsn['Status']);
  259. $diagnosticCode = Str::limit($dsn['Diagnostic-code'], 497);
  260. } else {
  261. $bounceType = null;
  262. $diagnosticCode = null;
  263. }
  264. // The return path is the alias except when it is from an unverified custom domain
  265. if ($returnPath !== config('anonaddy.return_path')) {
  266. $alias = Alias::withTrashed()->firstWhere('email', $returnPath);
  267. if (isset($alias)) {
  268. $user = $alias->user;
  269. }
  270. }
  271. // Try to find a user from the bounced email address
  272. if ($recipient = Recipient::select(['id', 'user_id', 'email', 'email_verified_at'])->get()->firstWhere('email', $bouncedEmailAddress)) {
  273. if (!isset($user)) {
  274. $user = $recipient->user;
  275. }
  276. }
  277. // Get the undelivered message
  278. $undeliveredMessage = $attachments->filter(function ($attachment) {
  279. return in_array($attachment->getContentType(), ['text/rfc822-headers', 'message/rfc822']);
  280. })->first();
  281. $undeliveredMessageHeaders = [];
  282. if ($undeliveredMessage) {
  283. $undeliveredMessageHeaders = $this->parseDeliveryStatus($undeliveredMessage->getMimePartStr());
  284. if (isset($undeliveredMessageHeaders['Feedback-id'])) {
  285. $parts = explode(':', $undeliveredMessageHeaders['Feedback-id']);
  286. if (in_array($parts[0], ['F', 'R', 'S']) && !isset($alias)) {
  287. $alias = Alias::find($parts[1]);
  288. // Find the user from the alias if we don't have it from the recipient
  289. if (!isset($user) && isset($alias)) {
  290. $user = $alias->user;
  291. }
  292. }
  293. // Check if failed delivery notification or Alias deactivated notification and if so do not notify the user again
  294. if (! in_array($parts[0], ['FDN'])) {
  295. if (isset($recipient)) {
  296. // Notify recipient of failed delivery, check that $recipient address is verified
  297. if ($recipient->email_verified_at) {
  298. $recipient->notify(new FailedDeliveryNotification($alias->email ?? null, $undeliveredMessageHeaders['X-anonaddy-original-sender'] ?? null, $undeliveredMessageHeaders['Subject'] ?? null));
  299. }
  300. } elseif (in_array($parts[0], ['R', 'S']) && isset($user)) {
  301. if ($user->email_verified_at) {
  302. $user->defaultRecipient->notify(new FailedDeliveryNotification($alias->email ?? null, $undeliveredMessageHeaders['X-anonaddy-original-sender'] ?? null, $undeliveredMessageHeaders['Subject'] ?? null));
  303. }
  304. }
  305. }
  306. }
  307. }
  308. if (isset($user)) {
  309. $failedDelivery = $user->failedDeliveries()->create([
  310. 'recipient_id' => $recipient->id ?? null,
  311. 'alias_id' => $alias->id ?? null,
  312. 'bounce_type' => $bounceType,
  313. 'remote_mta' => $remoteMta ?? null,
  314. 'sender' => $undeliveredMessageHeaders['X-anonaddy-original-sender'] ?? null,
  315. 'email_type' => $parts[0] ?? null,
  316. 'status' => $dsn['Status'] ?? null,
  317. 'code' => $diagnosticCode,
  318. 'attempted_at' => $postfixQueueId->created_at
  319. ]);
  320. if (isset($alias)) {
  321. // Decrement the alias forward count due to failed delivery
  322. if ($failedDelivery->email_type === 'F' && $alias->emails_forwarded > 0) {
  323. $alias->decrement('emails_forwarded');
  324. }
  325. if ($failedDelivery->email_type === 'R' && $alias->emails_replied > 0) {
  326. $alias->decrement('emails_replied');
  327. }
  328. if ($failedDelivery->email_type === 'S' && $alias->emails_sent > 0) {
  329. $alias->decrement('emails_sent');
  330. }
  331. }
  332. } else {
  333. Log::info([
  334. 'info' => 'user not found from bounce report',
  335. 'deliveryReport' => $deliveryReport,
  336. 'undeliveredMessage' => $undeliveredMessage,
  337. ]);
  338. }
  339. }
  340. exit(0);
  341. }
  342. protected function checkBandwidthLimit($user)
  343. {
  344. if ($user->hasReachedBandwidthLimit()) {
  345. $this->error('4.2.1 Bandwidth limit exceeded for user. Please try again later.');
  346. exit(1);
  347. }
  348. if ($user->nearBandwidthLimit() && ! Cache::has("user:{$user->username}:near-bandwidth")) {
  349. $user->notify(new NearBandwidthLimit());
  350. Cache::put("user:{$user->username}:near-bandwidth", now()->toDateTimeString(), now()->addDay());
  351. }
  352. }
  353. protected function checkRateLimit($user)
  354. {
  355. \Illuminate\Support\Facades\Redis::throttle("user:{$user->username}:limit:emails")
  356. ->allow(config('anonaddy.limit'))
  357. ->every(3600)
  358. ->then(
  359. function () {
  360. },
  361. function () {
  362. $this->error('4.2.1 Rate limit exceeded for user. Please try again later.');
  363. exit(1);
  364. }
  365. );
  366. }
  367. protected function getRecipients()
  368. {
  369. return collect($this->option('recipient'))->map(function ($item, $key) {
  370. return [
  371. 'email' => $item,
  372. 'local_part' => strtolower($this->option('local_part')[$key]),
  373. 'extension' => $this->option('extension')[$key],
  374. 'domain' => strtolower($this->option('domain')[$key])
  375. ];
  376. });
  377. }
  378. protected function getParser($file)
  379. {
  380. $parser = new Parser;
  381. // Fix some edge cases in from name e.g. "\" John Doe \"" <johndoe@example.com>
  382. $parser->addMiddleware(function ($mimePart, $next) {
  383. $part = $mimePart->getPart();
  384. if (isset($part['headers']['from'])) {
  385. $value = $part['headers']['from'];
  386. $value = (is_array($value)) ? $value[0] : $value;
  387. try {
  388. mailparse_rfc822_parse_addresses($value);
  389. } catch (\Exception $e) {
  390. $part['headers']['from'] = str_replace("\\", "", $part['headers']['from']);
  391. $mimePart->setPart($part);
  392. }
  393. }
  394. return $next($mimePart);
  395. });
  396. if ($file == 'stream') {
  397. $fd = fopen('php://stdin', 'r');
  398. $this->rawEmail = '';
  399. while (!feof($fd)) {
  400. $this->rawEmail .= fread($fd, 1024);
  401. }
  402. fclose($fd);
  403. $parser->setText($this->rawEmail);
  404. } else {
  405. $parser->setPath($file);
  406. }
  407. return $parser;
  408. }
  409. protected function parseDeliveryStatus($deliveryStatus)
  410. {
  411. $lines = explode(PHP_EOL, $deliveryStatus);
  412. $result = [];
  413. foreach ($lines as $line) {
  414. if (preg_match('#^([^\s.]*):\s*(.*)\s*#', $line, $matches)) {
  415. $key = ucfirst(strtolower($matches[1]));
  416. if (empty($result[$key])) {
  417. $result[$key] = trim($matches[2]);
  418. }
  419. } elseif (preg_match('/^\s+(.+)\s*/', $line) && isset($key)) {
  420. $result[$key] .= ' ' . $line;
  421. }
  422. }
  423. return $result;
  424. }
  425. protected function getBounceType($code, $status)
  426. {
  427. if (preg_match("/(:?mailbox|address|user|account|recipient|@).*(:?rejected|unknown|disabled|unavailable|invalid|inactive|not exist|does(n't| not) exist)|(:?rejected|unknown|unavailable|no|illegal|invalid|no such).*(:?mailbox|address|user|account|recipient|alias)|(:?address|user|recipient) does(n't| not) have .*(:?mailbox|account)|returned to sender|(:?auth).*(:?required)/i", $code)) {
  428. return 'hard';
  429. }
  430. if (preg_match("/(:?spam|unsolicited|blacklisting|blacklisted|blacklist|554|mail content denied|reject for policy reason|mail rejected by destination domain|security issue)/i", $code)) {
  431. return 'spam';
  432. }
  433. // No match for code but status starts with 5 e.g. 5.2.2
  434. if (Str::startsWith($status, '5')) {
  435. return 'hard';
  436. }
  437. return 'soft';
  438. }
  439. protected function getSenderFrom()
  440. {
  441. try {
  442. return $this->parser->getAddresses('from')[0]['address'];
  443. } catch (\Exception $e) {
  444. return $this->option('sender');
  445. }
  446. }
  447. protected function exitIfFromSelf()
  448. {
  449. // To prevent recipient alias infinite nested looping.
  450. if (in_array($this->option('sender'), [config('mail.from.address'), config('anonaddy.return_path')])) {
  451. exit(0);
  452. }
  453. }
  454. }