使用 PHP 处理退回电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1011714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:38:01  来源:igfitidea点击:

Bounce Email handling with PHP?

phpemailbounce

提问by

Here is my scenario:

这是我的场景:

I have 2 email accounts: [email protected] and [email protected].

我有 2 个电子邮件帐户:[email protected][email protected]

I want to send email to all my users with [email protected] but then "reply to" [email protected] (until here, my PHP script can handle it).

我想用 [email protected] 向我的所有用户发送电子邮件,然后“回复”[email protected](直到这里,我的 PHP 脚本可以处理它)。

When, the email can't be sent, it's sent to [email protected], the error message could be 553 (non existent email ...) etc.

当无法发送电子邮件时,将其发送到[email protected],错误消息可能是553(不存在的电子邮件...)等。

My question is: How do I direct all those bounce emails (couldn't-sent emails) to [email protected] through a handling script to check for the bounce error codes?

我的问题是:如何通过处理脚本将所有这些退回的电子邮件(无法发送的电子邮件)定向到[email protected] 以检查退回错误代码?

What programming language should I be using for the "handling script"?

我应该为“处理脚本”使用什么编程语言?

What would the "handling script" look like? Can you give a sample?

“处理脚本”会是什么样子?你能给一个样品吗?

in other words:

换句话说:

What are the procedures I should follow to handle the bounce email ?

我应该遵循哪些程序来处理退回电子邮件?

回答by Gabriel Sosa

The best scenario is be able to classify the type of bounce: soft, hard...

最好的场景是能够对弹跳类型进行分类:软、硬……

what we use is BounceStudio. You need to compile it and add the php libraries... not hard at all. You have the free and paid version of that product

我们使用的是BounceStudio。您需要编译它并添加 php 库...一点也不难。您拥有该产品的免费和付费版本

once we detect the kind of bouncewe use PEAR::MAIL::MIME to search for custom headers that we added previously to the email, lets say:

一旦我们检测到退回类型,我们就使用 PEAR::MAIL::MIME 来搜索我们之前添加到电子邮件中的自定义标头,让我们说:

X-user-id: XXXXX
X-campaign-id: YYYYYY 
X-recipient-id: SSSSSSSSS

in this way we can know the real recipient that we sent the email to.

通过这种方式,我们可以知道我们发送电子邮件的真正收件人。

hope this help you! so you can help me to get to the 500 points :P

希望这对你有帮助!所以你可以帮助我达到 500 分:P

回答by Shoban

Why not create a [email protected] and use php to read those emailsand do what ever you want?

为什么不创建一个[email protected] 并使用php阅读这些电子邮件并做任何你想做的事情?

Edit After your comment : Please chec my link whcih has a php script which will teach you how to open and email box using php and read the emails. You can use this scrip to check the error messages.

在您发表评论后编辑:请检查我的链接,其中有一个 php 脚本,它将教您如何使用 php 打开和发送电子邮件框并阅读电子邮件。您可以使用此脚本来检查错误消息。

回答by Peter Smit

Let the emails bounce to an address that is really an emailadress (with login details etc.).

让电子邮件退回到真正是电子邮件地址的地址(带有登录详细信息等)。

Make a php script which runs ever x minutes (for example with a cron job). This php script must do the following. - Retrieve all email from the box (use for example Zend Mail) - Check for the error in the message (e.g. by searching it with regular expressions) - Do what ever is necessary.

制作一个每隔 x 分钟运行一次的 php 脚本(例如使用 cron 作业)。这个 php 脚本必须执行以下操作。- 从框中检索所有电子邮件(例如使用Zend Mail) - 检查消息中的错误(例如通过使用正则表达式搜索) - 做任何必要的事情。

If you want to know specifically who has bounced back you can use user specific bounce addresses. (See for example this site)

如果您想具体知道谁退回了,您可以使用用户特定的退回地址。(例如参见本网站

回答by balazscsaba2006

Maybe it's a little late for the answer, but you can always try something new. I had the last week a task like this, and used BOUNCE HANDLER Class, by Chris Fortune, which chops up the bounce into associative arrays - http://www.phpclasses.org/browse/file/11665.html

也许答案有点晚了,但你总是可以尝试新的东西。上周我有一个这样的任务,并使用了 Chris Fortune 的 BOUNCE HANDLER Class,它将弹跳分解为关联数组 - http://www.phpclasses.org/browse/file/11665.html

This will be used after you connect to the POP3 with some mailer to get the bounces from it, then parse it into pieces with this, and if has the status you searched for, do the necessary actions.

这将在您使用某些邮件程序连接到 POP3 以从中获取退回邮件后使用,然后将其解析为多个部分,如果有您搜索的状态,则执行必要的操作。

Cheers.

干杯。

回答by Kev

If you've got a POP3 mailbox set up for [email protected], you could use a POP3 client script written in PHP to retrieve the messages and check for undeliverable messages.

如果您为 设置了 POP3 邮箱[email protected],则可以使用用 PHP 编写的 POP3 客户端脚本来检索邮件并检查无法送达的邮件。

回答by Steve Smith

You could always use something like http://cloudmailin.comto forward the bounced emails on to your php server via http however you may be better with a service dedicated to sending emails and using their api to retrieve the bounce details.

您始终可以使用诸如http://cloudmailin.com 之类的东西通过 http 将退回的电子邮件转发到您的 php 服务器,但是您可能会更好地使用专门用于发送电子邮件并使用其 api 来检索退回详细信息的服务。

回答by Jazzy

i have had pretty bad luck looking for a PHP solution for this, but i ran across this product that does just what i needed.

我为此寻找 PHP 解决方案的运气非常糟糕,但我遇到了这个产品,它可以满足我的需求。

it runs as a native app mac/win but it does the job.

它作为本机应用程序 mac/win 运行,但它可以完成这项工作。

http://www.maxprog.com/site/software/internet-marketing/email-bounce-handler_sheet_us.php

http://www.maxprog.com/site/software/internet-marketing/email-bounce-handler_sheet_us.php

回答by Tamás Barta

I was searching for the answer to the same question. There are more parts of the question, and more options.

我正在寻找同一问题的答案。问题的部分更多,选项也更多。

For handling the bounced e-mail, I found a PHP class, purely in PHP, no compile or additional software installation needed if you have a PHP powered site. It is very easy to use.

为了处理退回的电子邮件,我找到了一个PHP 类,完全用 PHP 编写,如果您有一个 PHP 驱动的站点,则不需要编译或安装额外的软件。这是非常容易使用。

If you are using cPanel, or InterWorx/SiteWorx, you can configure some of the addresses to handle the received e-mails with a script, for example a PHP script, so you can write your own handling with the aid of the mentioned class. Or of course still you can create ordinary e-mail accounts and retrieve the mails via POP3 or IMAP, and then interpret them. I think the first one is better, because it's direct, you don't have to use additional channels, like IMAP. Of course if you can't configure your mail server, or don't know how to do it, then the former is better for you.

如果您使用的是 cPanel 或 InterWorx/SiteWorx,您可以配置一些地址以使用脚本(例如 PHP 脚本)处理接收到的电子邮件,因此您可以借助上述类编写自己的处理方式。或者当然你仍然可以创建普通的电子邮件帐户并通过 POP3 或 IMAP 检索邮件,然后解释它们。我认为第一个更好,因为它是直接的,您不必使用其他渠道,例如 IMAP。当然,如果你不能配置你的邮件服务器,或者不知道怎么做,那么前者更适合你。

Good luck! :)

祝你好运!:)

回答by michiel

In the php mail command http://php.net/mail

在 php 邮件命令中http://php.net/mail

you use the fifth parameter and add "-f" to it.

您使用第五个参数并向其添加“-f”。

So, you use "-f [email protected]" as the parameter

因此,您使用“-f [email protected]”作为参数

the phpList newsletter manageruses this to manage bounces.

phpList通讯管理器使用它来管理反弹。

Once the bounces fill up in the mailbox, you can POP them, and process them. That's the easiest way to deal with them, as opposed to handling them when they arrive.

一旦退回邮件填满邮箱,您就可以 POP 并处理它们。这是处理它们的最简单方法,而不是在它们到达时处理它们。

回答by Dustin Flick

Here is a canned solution to process bounces using IMAP.

这是使用 IMAP 处理反弹的罐头解决方案。

I changed the Return-Path header of my Mail instance to a dedicated [email protected]

我将我的邮件实例的返回路径标头更改为专用的[email protected]

The only method easy enough for me to consider viable is the following, which checks via POP3 the dedicated inbox and can handle each email based on the message received.

我认为可行的唯一方法是以下方法,它通过 POP3 检查专用收件箱,并可以根据收到的消息处理每封电子邮件。

$inst=pop3_login('mail.xxxxxx.us','110','[email protected]','pass');
$stat=pop3_stat($inst);
//print_r($stat);
if($stat['Unread']>0){
    echo "begin process<br><br>";
    $list=pop3_list($inst);
    //print_r($list);
    foreach($list as $row){
        if(strpos($row['from'],'MAILER-DAEMON')!==FALSE){
            $msg=imap_fetchbody($inst,$row['msgno'],'1');
            if(strpos($msg,'550')!==FALSE){
                echo "handle hard bounce".$msg."<br><br>";
                //WHATEVER HERE TO PROCESS BOUNCE
            }   
        }
        else{
            $msg=imap_fetchbody($inst,$row['msgno'],'1');
            echo "not from my server. could be spam, etc.".$msg."<br><br>";
            //PROBABLY NO ACTION IS NEEDED
        }   
        //AFTER PROCESSING
        //imap_delete ( resource $imap_stream , int $msg_number [, int $options = 0 ] )
        //commented out because I havent implemented yet. see IMAP documentation
    }   
}   
else{
    echo "no unread messages";  
}


//imap_close ( resource $imap_stream [, int $flag = 0 ] )
//commented out because I havent implemented yet. see IMAP documentation.
//flag: If set to CL_EXPUNGE, the function will silently expunge the mailbox before closing, removing all messages marked for deletion. You can achieve the same thing by using imap_expunge()




function pop3_login($host,$port,$user,$pass,$folder="INBOX",$ssl=false) 
{ 
    $ssl=($ssl==false)?"/novalidate-cert":""; 
    return (imap_open("{"."$host:$port/pop3$ssl"."}$folder",$user,$pass)); 
} 
function pop3_stat($connection)        
{ 
    $check = imap_mailboxmsginfo($connection); 
    return ((array)$check); 
} 
function pop3_list($connection,$message="") 
{ 
    if ($message) 
    { 
        $range=$message; 
    } else { 
        $MC = imap_check($connection); 
        $range = "1:".$MC->Nmsgs; 
    } 
    $response = imap_fetch_overview($connection,$range); 
    foreach ($response as $msg) $result[$msg->msgno]=(array)$msg; 
        return $result; 
} 
function pop3_retr($connection,$message) 
{ 
    return(imap_fetchheader($connection,$message,FT_PREFETCHTEXT)); 
} 
function pop3_dele($connection,$message) 
{ 
    return(imap_delete($connection,$message)); 
}