【问题标题】:Connecting Zapier with PHPMailer Script使用 PHPMailer 脚本连接 Zapier
【发布时间】:2019-10-08 02:19:28
【问题描述】:

我正在使用 Zapier 从 facebook 收集潜在客户,然后将它们发送到我的 CRM。 我有一个脚本连接到我的 CRM,它应该处理潜在客户。 对于脚本将触发的每个新线索, 该脚本收集从 Zapier 传递的数据并将其转换为 XML 并发送给我的客户端。

除了一件事,一切都有效。 PHPMailer 似乎对 zapier 造成了麻烦,因为每当启用 email() 功能时,Zapier 都会给我一个错误。

仅供参考 - 当我转到脚本 url 并手动设置 GET 参数时,这有效。 正在发送 xml。但是当从 zapier 触发脚本时,就会出现问题。

Zapier 错误: “我们在发送你的测试时遇到了麻烦。 该应用程序返回“内部服务器错误”,没有更多详细信息。您连接的应用程序的服务器似乎已关闭或当前遇到问题。如果没有报告问题,请检查应用的状态页面或联系支持人员。”

<?php
$firstName = isset($_GET['firstName']) ? $_GET['firstName'] : '';
$lastName = isset($_GET['lastName']) ? $_GET['lastName'] : '';
$fullName = isset($_GET['fullName']) ? $_GET['fullName'] : '';
$phone = isset($_GET['phone']) ? $_GET['phone'] : '';
$experience = isset($_GET['experience']) ? $_GET['experience'] : '';
$city = isset($_GET['city']) ? $_GET['city'] : '';
$email = isset($_GET['email']) ? $_GET['email'] : '';
$utm_source = isset($_GET['utm_source']) ? $_GET['utm_source'] : '';
$campaignId = isset($_GET['campaignId']) ? $_GET['campaignId'] : '';
$utm_medium = isset($_GET['utm_medium']) ? $_GET['utm_medium'] : '';



require 'vendor/autoload.php';
header('Content-Type: text/plain');

function createXML($data,$dataSource){
$dom = new DOMDocument('1.0', 'utf-8');
$cv = $dom->createElement("cv");
$candidate = $dom->createElement('candidate');
$source_type = $dom->createElement('source_type');

function recursive($dom, $parent, $key, $value) {

    if(is_array($value)) {
        $new_parent = $dom->createElement($key);

        foreach($value as $k => $v){
            recursive($dom, $new_parent, $k, $v);
        }

        $parent->appendChild($new_parent);
    } else {
        $field = $dom->createElement($key, htmlspecialchars($value));
        $parent->appendChild($field);
    }
}
foreach($dataSource as $key => $value){
    // api need COLUMN without end of _<number>
    if(preg_match('/COLUMN_([0-9]+)/', $key)) $key = 'COLUMN';
    recursive($dom, $source_type, $key, $value);
}
foreach($data as $key => $value){
    // api need COLUMN without end of _<number>
    if(preg_match('/COLUMN_([0-9]+)/', $key)) $key = 'COLUMN';
    recursive($dom, $candidate, $key, $value);
}

// $cv->appendChild($candidate)

$cv->appendChild($candidate);
$cv->appendChild($source_type);
$dom->appendChild($cv);
$node = $cv->appendChild($source_type);
$node->setAttribute('type','other');
$dom->formatOutput = true;

return $dom;
}




 $data = array(
    "first_name" => filter_var($firstName, FILTER_SANITIZE_STRING),
    "last_name"  => filter_var($lastName, FILTER_SANITIZE_STRING),
    "mobile"     => filter_var($phone, FILTER_SANITIZE_STRING),
    'email'      => '',
    'id'      => '',


);
$dataSource = array(
    "source_title"        => filter_var($utm_source, FILTER_SANITIZE_STRING),
    "first_name"          => '',
    "last_name"           => '',
    "mobile"           => '',
    "email"           => '',
    "employee_number" => '',
    "department"      => '',
    "email"           => '',
);



//problematic function
function email(){
    global $xmlData;
    $mail = new PHPMailer(true);   
    $mail->isHTML(false);
    $mail->isSMTP();
    $mail->setFrom('XML@gmail.com', 'Yashir CV Lead');

    $mail->addAddress("BinaryRx@gmail.com");        
    $mail->Subject = "Yashir CV Lead";
    $mail->Body = $xmlData;
    $today = date('d-m-Y H:i:s');
    $mail->send();
    echo "Report Sent - " . $today;
}

///////// IF I uncomment bellow,Zapier will give me the following error:
//We had trouble sending your test through.
//The app returned "Internal Server Error" with no further details. 
//It looks like the server for your connected app is down or currently experiencing problems.
//Please check the app's status page or contact support if there are no reported issues.

//Uncomment bellow.
// email();

?>

我希望每个潜在客户都发送一封包含 XML 的电子邮件。

【问题讨论】:

    标签: php phpmailer zapier


    【解决方案1】:

    两个关键问题。首先,您使用的是 SMTP,但您没有将 Host 设置为您的邮件服务器 - 所以除非是 localhost,否则它将无法工作 - 是这样吗?

    您要求 PHPMailer 抛出异常(通过将 true 传递给构造函数),但您没有包装对 PHPMailer 的调用的 try/catch 块,因此任何错误都会导致未捕获的异常 - 这会给你你所看到的症状。试试这个:

    function email()
    {
        global $xmlData;
        $mail = new PHPMailer(true);
        try {
            $mail->isHTML(false);
            $mail->isSMTP();
            $mail->setFrom('XML@gmail.com', 'Yashir CV Lead');
    
            $mail->addAddress("BinaryRx@gmail.com");
            $mail->Subject = "Yashir CV Lead";
            $mail->Body = $xmlData;
            $today = date('d-m-Y H:i:s');
            $mail->send();
            echo "Report Sent - ".$today;
        } catch (Exception $e) {
            echo 'Sending failed'.$e->getMessage();
        }
    }
    

    总的来说,主要的是一次调试一件事——在你开始尝试做依赖于它工作的事情之前,检查email()函数是否真的独立工作,否则你将不知道哪一点代码失败了。

    如果您使用的是 PHP 7.0 或更高版本,则可以使用 null coalesce operator 简化对参数的初始检查。你可以替换这个:

    $firstName = isset($_GET['firstName']) ? $_GET['firstName'] : '';
    

    与:

    $firstName = $_GET['firstName'] ?? '';
    

    【讨论】:

    • 非常感谢!我不能使用简写 isset,因为服务器运行 PHP 5.3。至于使用try and catch。你完全正确。添加 try catch 后,我注意到我的 $->Body = $xmlData; 有问题发现错误并修复它。感谢您的帮助!
    • 很高兴您修复了它。 PHP 5.3?哎呀!是时候升级了!
    猜你喜欢
    • 2020-07-17
    • 2014-01-21
    • 2018-07-17
    • 1970-01-01
    • 2019-01-19
    • 2016-05-14
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多