Processing incoming e-mail with PHP Script

☆樱花仙子☆ 提交于 2019-11-30 07:43:25

Here's some code you could use to get you going:

// set user to check
$strUser     = "username";
$strPassword = "password";

// open
$hMail = imap_open ("{mail.yourdomain.com:143/notls}INBOX", "$strUser", "$strPassword");

// get headers
$aHeaders = imap_headers( $hMail );

// get message count
$objMail = imap_mailboxmsginfo( $hMail );

// process messages
for( $idxMsg = 1; $idxMsg <= $objMail->Nmsgs; $idxMsg++  )
{
    // get header info
    $objHeader = imap_headerinfo( $hMail, $idxMsg );

    // get from object array
    $aFrom = $objHeader->from;

    // process headers
    for( $idx = 0; $idx < count($aFrom); $idx++ )
    {
        // get object
        $objData = $aFrom[ $idx ];

        // get email from
        $strEmailFrom = $objData->mailbox . "@" . $objData->host;

        // do some stuff here
    }

    // delete message
    imap_delete( $hMail, $idxMsg );
}

// expunge deleted messages
imap_expunge( $hMail );

// close
imap_close( $hMail );

Two options:

1) If you have access to the shell on the recipient email server, you can set up a rule to fire a script (like via procmail or the like) whenever a new message is received.

2) You can allow the email to drop into the mailbox as normal, then write your PHP script to access the mailbox via IMAP, pull the new messages, process them, then delete them. And then run the thing by cron every few minutes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!