EmailValidation.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * Copyright (C) 2013 Mailgun
  5. *
  6. * This software may be modified and distributed under the terms
  7. * of the MIT license. See the LICENSE file for details.
  8. */
  9. namespace Mailgun\Api;
  10. use Mailgun\Assert;
  11. use Mailgun\Model\EmailValidation\ParseResponse;
  12. use Mailgun\Model\EmailValidation\ValidateResponse;
  13. use Psr\Http\Client\ClientExceptionInterface;
  14. use Psr\Http\Message\ResponseInterface;
  15. /**
  16. * @see https://documentation.mailgun.com/en/latest/api-email-validation.html
  17. *
  18. * @author David Garcia <me@davidgarcia.cat>
  19. */
  20. class EmailValidation extends HttpApi
  21. {
  22. /**
  23. * Addresses are validated based off defined checks.
  24. * This operation is only accessible with the private API key and not subject to the daily usage limits.
  25. * @param string $address An email address to validate. Maximum: 512 characters.
  26. * @param bool $mailboxVerification If set to true, a mailbox verification check will be performed
  27. * against the address. The default is False.
  28. * @param array $requestHeaders
  29. * @return ValidateResponse|ResponseInterface
  30. * @throws ClientExceptionInterface Thrown when we don't catch a Client or Server side Exception
  31. */
  32. public function validate(string $address, bool $mailboxVerification = false, array $requestHeaders = [])
  33. {
  34. Assert::stringNotEmpty($address);
  35. $params = [
  36. 'address' => $address,
  37. 'mailbox_verification' => $mailboxVerification,
  38. ];
  39. $response = $this->httpGet('/v3/address/private/validate', $params, $requestHeaders);
  40. return $this->hydrateResponse($response, ValidateResponse::class);
  41. }
  42. /**
  43. * Parses a delimiter-separated list of email addresses into two lists: parsed addresses and unparsable portions.
  44. * The parsed addresses are a list of addresses that are syntactically valid
  45. * (and optionally pass DNS and ESP specific grammar checks).
  46. * The unparsable list is a list of character sequences that could not be parsed
  47. * (or optionally failed DNS or ESP specific grammar checks).
  48. * Delimiter characters are comma (,) and semicolon (;).
  49. * This operation is only accessible with the private API key and not subject to the daily usage limits.
  50. * @param string $addresses A delimiter separated list of addresses. Maximum: 8000 characters.
  51. * @param bool $syntaxOnly Perform only syntax checks or DNS and ESP specific validation as well.
  52. * The default is True.
  53. * @param array $requestHeaders
  54. * @return ParseResponse|ResponseInterface
  55. * @throws ClientExceptionInterface Thrown when we don't catch a Client or Server side Exception
  56. */
  57. public function parse(string $addresses, bool $syntaxOnly = true, array $requestHeaders = [])
  58. {
  59. Assert::stringNotEmpty($addresses);
  60. Assert::maxLength($addresses, 8000);
  61. $params = [
  62. 'addresses' => $addresses,
  63. 'syntax_only' => $syntaxOnly,
  64. ];
  65. $response = $this->httpGet('/v3/address/private/parse', $params, $requestHeaders);
  66. return $this->hydrateResponse($response, ParseResponse::class);
  67. }
  68. }