-4

I want to unset an array value if it contains a certain word in it. What I am doing is that, I am pulling all the emails from gmail inbox and only displaying the emails from the the message body so i can update it in the database. Message body includes few emails as it is for undelivered emails.

When all the emails are displayed, it also displays where the email was sent from which i dont want.

The code is below:

/*===================================   GMAIL   ======================================*/
/* connect to gmail */
    //$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'username@gmail.com';
    $password = 'password';

    //$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

    //Which folders or label do you want to access? - Example: INBOX, All Mail, Trash, labelname 
    //Note: It is case sensitive
    $imapmainbox = "INBOX";

    //Select messagestatus as ALL or UNSEEN which is the unread email
    $messagestatus = "ALL";

    //-------------------------------------------------------------------

    //Gmail Connection String
    $imapaddress = "{imap.gmail.com:993/imap/ssl}";

    //Gmail host with folder
    $hostname = $imapaddress . $imapmainbox;

    //Open the connection
    $connection = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

    //Grab all the emails inside the inbox
    $emails = imap_search($connection,$messagestatus);

    //number of emails in the inbox
    $totalemails = imap_num_msg($connection);

    echo "Total Emails: " . $totalemails . "<br>";

    if($emails) {

      //sort emails by newest first
      rsort($emails);

      //loop through every email int he inbox
      foreach($emails as $email_number) {

        //grab the overview and message
        $header = imap_fetch_overview($connection,$email_number,0);

        //Because attachments can be problematic this logic will default to skipping the attachments    
        $message = imap_fetchbody($connection,$email_number,1.1);
             if ($message == "") { // no attachments is the usual cause of this
              $message = imap_fetchbody($connection, $email_number, 1);
        }

        //split the header array into variables
        $status = ($header[0]->seen ? 'read' : 'unread');
        $subject = $header[0]->subject;
        $from = $header[0]->from;
        $date = $header[0]->date;

        preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $message, $matches);
        //$matches = array_unique($matches);

        // remove some of the emails which i dont want to include
        $matches[0] = remove(remove(remove(array_unique($matches[0]) ,  'info@example.co.uk') ,  'no-reply@example.co.uk'),  '131669395@infong353.kundenserver.de') ;

        foreach($matches[0] as $email) {                                                        
          //echo $email . "<br>";
            echo '
                <div class="alert alert-primary" role="alert">
                    '.$email.' in <b>'.category_name(cat_id_from_email($email)).'</b> group <br><br>
                    <a href="index.php?p=gmail&undelivered='.$email.'" class="btn btn-info nounderline" >Undelivered</a>
                    <a href="index.php?p=gmail&unsubscribers='.$email.'" class="btn btn-warning nounderline" >Unsubscribers</a>
                    <a href="index.php?p=gmail&delete='.$email.'" class="btn btn-danger nounderline" >Delete</a>
                </div>
        ';
        }
      }  
    } 

    // close the connection
    imap_close($connection);

I also want to remove any email address with certain domain names. For example:

if any of the email is xxxxx@example2.com, i want to unset it. So basically any email address with example2.com.

Could anyone help me with this.

Malkoc
  • 79
  • 8
  • We are always glad to help and support new coders but ***you need to help yourself first. :-)*** After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Feb 12 '18 at 20:18
  • Can you please give me a link of the other question which is exactly the same? I couldnt find anything which may help me – Malkoc Feb 12 '18 at 20:22
  • It's not the question, it's the answer of the link in the duplicate. Look at the top of the page. – Jay Blanchard Feb 12 '18 at 20:23
  • Nobody knows what `remove()` is but look at http://php.net/manual/en/function.preg-grep.php with `PREG_GREP_INVERT`. – AbraCadaver Feb 12 '18 at 20:26
  • @JayBlanchard: Sorry, I didn't see `unset($matches['some magically derived index']);` as helpful or duplicate. – AbraCadaver Feb 12 '18 at 20:29
  • It's all good. I didn't get the chance to add the other 4 dupes @AbraCadaver – Jay Blanchard Feb 12 '18 at 20:29
  • @AbraCadaver you're saying that this https://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key isn't worthy as a dupe? – Funk Forty Niner Feb 12 '18 at 20:30
  • @FunkFortyNiner: That wasn't the dupe this was closed under, unless I reopened just as Jay was saving the new ones. – AbraCadaver Feb 12 '18 at 20:30
  • @AbraCadaver that wasn't my question – Funk Forty Niner Feb 12 '18 at 20:30
  • remove is a function i created to unset some of the known values. I need to unset the values inside the foreach loop with checking certain word. – Malkoc Feb 12 '18 at 20:31
  • @Malkoc: http://php.net/manual/en/function.strpos.php – AbraCadaver Feb 12 '18 at 20:32
  • @FunkFortyNiner: That matched the original, maybe, but not with the edit _I also want to remove any email address with certain domain names. For example: if any of the email is xxxxx@example2.com, i want to unset it._ – AbraCadaver Feb 12 '18 at 20:33
  • @AbraCadaver 10-4. – Funk Forty Niner Feb 12 '18 at 21:31
  • Ok i can now find the emails that i want to unset. But i am still struggling with unsetting them inside the foreach loop. Code $pos = strpos($email,'example.co.uk'); if($pos === true) {/*do something*/} – Malkoc Feb 12 '18 at 21:31
  • Which `foreach`? The outer one or the inner one displaying the data? – AbraCadaver Feb 12 '18 at 21:32
  • inner one displaying the data – Malkoc Feb 12 '18 at 21:34

1 Answers1

1

I can see two ways; remove from the array before the display loop:

$matches[0] = preg_grep('/example2\.com/', $matches[0], PREG_GREP_INVERT);

Or check in the display loop and just don't display:

foreach($matches[0] as $email) {
    if(strpos($email, 'example2.com') !== false) { continue; }
    //echo your stuff
}

Depends on if you want to keep the array intact but just not display or modify the array for later use.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87