开发者

Using variables in imap_search() criteria

Sorry, I'm probably missing something simple here, but I feel like I've tried everything. I'm a PHP beginner, so that likely explains it.

Okay, so I am having emails sent to an inbox with a key appended to the mailbox. For example:

jerry+examplekey@gmail.com

I'm trying to create a loop that does something to all emails with that key attached to it.

When I do

foreach ($valid_keys as $key){ // granted this loop does nothing for now
if ($emails = imap_search($mbox, 'To "examplekey"')){echo "Found emails.";};
}

It works absolutely fine. That is, when I specify the "To" criteria string开发者_开发知识库 manually. But when I try to put a variable in to the To criteria instead of a string, it doesn't find it--even when I've verified that $key is equal to a key that's in the inbox (I was echoing it in the loop to make sure).

foreach ($valid_keys as $key){
if ($emails = imap_search($mbox, 'To "$key"')){echo "found one";}; // this doesn't work
}

I've tried wrapping it in curly braces, fiddling with the quotes, putting the whole 'To "$key"' criteria in a $string and then doing it like imap_search($mbox, $string) and it doesn't work.

What am I missing here?


Change 'To "$key"' to "To \"$key\""

PHP only does variable substitution in double quotes ""

Single quotes are faster because of this, PHP doesn't have to search through the string to find variables.

So you could also do it using the concatenation operator like this 'To "'.$key.'"'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜