title: Mailchimp_List_AlreadySubscribed Fatal Error using Mailchimp API 2 and PHP
body: <p>I'm trying to use the Mailchimp API on my site to allow people to subscribe to my Mailchimp newsletter via a custom form.</p>
  
  <p>The working code is as follows:</p>
  
  <pre><code>$message="";
  if(isset($_POST) &amp; !empty($_POST)){ 
      $emailsanit=filter_var($_POST['email'],FILTER_SANITIZE_EMAIL);
      $email=filter_var($emailsanit,FILTER_VALIDATE_EMAIL);
  
      $api_key = "my api key";
      $list_id = "my list id";
  
      require("Mailchimp.php");
      $Mailchimp = new Mailchimp( $api_key );
      $Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
      $subscriber = $Mailchimp_Lists-&gt;subscribe( $list_id, array( 'email' =&gt; $email ) );
  
      if ( ! empty( $subscriber['leid'] ) ) {
          $message="&lt;div id='message' class='text-success'&gt;Please check your email inbox to         complete your subscription&lt;/div&gt;";
      }
      else
      {
          $message="&lt;div id='message' class='text-danger'&gt;Failed to add email to database.   Please try again or email&lt;/div&gt;";
      }
  }
  ?&gt;
  </code></pre>
  
  <p>As I say, this works fine, the problem I'm having is if somebody enters an email address that's already registered I get the following error and obviously nothing else loads:</p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'Mailchimp_List_AlreadySubscribed' with message 'example@example.com is already subscribed to list XX.</p>
  </blockquote>
  
  <p>Any suggestions as to how I could go about making a rule so that if an email is already registered the message variable is updated (the form is processed on the same page)?</p>
  
  <p>This post <a href="https://stackoverflow.com/questions/21395410/mailchimp-api-2-0-and-php-subscribe-form">MailChimp API 2.0 and PHP Subscribe Form</a> had someone with the same problem but I didn't really understand the solution.</p>