I have an upcoming freelance project coming up that includes checking an IMAP email account as well as working with XML in PHP. Let’s talk about connecting to an IMAP account in PHP, don’t worry, I’ll discuss parsing XML next week ;)
For the sake of example, we’re going to connect to a Gmail account:
Not much to it, set up the variables, connect and utilize the
imap_fetch* methods to get what you want. Keep in mind that
imap_fetch_overview will return an array of objects. If you wanted to grab just the subject line, you would call
Also be mindful of the third argument being passed to
imap_fetchbody. The 2 specified here should return the message in RFC822 format but is more so contingent on what type of email you received (text-only or multipart). You may have to play around with that value to get the results you desire.