首页 > 解决方案 > 如何在 php 中为 send_mail 生成 unique_id

问题描述

目前我们有 send_mail 和这个函数在不同的文件中调用。

我们有 auto_mailer 将报告订阅电子邮件与以编程方式生成的电子邮件地址连接起来。

我的任务是生成 uniqueId,这样我们就不必在调用 send_mail 函数的每个文件上更改/手动添加 send_mail 参数/参数。

生成的 uniqueId 必须代表调用它的 send_mail 函数。

我们已经尝试使用 debug_backtrace() 并通过 sha1() 转换它以生成 uniqueId,但问题是未来代码的变化。

我们已经尝试使用 send_mail 参数/参数并通过 sha1() 将其转换以生成 uniqueId,但问题是参数/参数数据是动态的。

这是示例代码:

$subject = 'Tickets 1st QTR 2020';

// 1st scenario
function send()
{
    send_mail('',$recipient.'', "Reports Guy", $sender, $subject);
}

function send2()
{
    send_mail('',$recipient.'', "Reports Guy", $sender, $subject);
}

// 2nd scenario: uniqueId shoud remain the same even there's a change/update in php code
function send()
{
    // must be same uniqueId
    send_mail('',$recipient.'', "Reports Guy", $sender, $subject);
}

function send3()
{
    // must be different uniqueId
    send_mail('',$recipient.'', "Reports Guy", $sender, $subject);
}

function send2()
{
    // must be same uniqueId
    send_mail('',$recipient.'', "Reports Guy", $sender, $subject);
}

function send_mail($module,$to_email,$from_name,$from_email,$subject,$contents,$cc='',$bcc='',$attachment='',$emailid='',$logo='',$set='',$filename='')
{    
    global $DEV_TEST_ENV;   
    global $DEV_TEST_EMAIL; 
    global $DEV_FROM_EMAIL; 
    
    // add code here that generate and check uniquedId in tbl_subscriptions
    // create new entry if not exist else continue to send.

    $mail_log = preg_replace("/\s{2}/", "", nl2br("'".$module."','".$to_email."','".str_replace(array("'", "\n"), "", $from_name)."','".$from_email."','".str_replace(array("'", "\n"), "", $subject)."','','".$cc."','".$bcc."','".$attachment."','".$emailid."','".$logo."'"));

        
    return $mail_error;
}

例子:

在上面的场景 #1 中,假设 send2() 在第 22 行并生成 uniquedId - 123asd。

在场景#2 send2() 被移动到第 32 行或参数被更改,我如何生成相同的 uniqueId?

标签: phpsendmailunique-id

解决方案


推荐阅读