使用 PHPMailer 脚本连接 Zapier

Connecting Zapier with PHPMailer Script

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

除一件事外,一切正常。 PHPMailer 似乎会导致 zapier 出现问题,因为每当启用 email() 函数时,Zapier 都会给我一个错误。

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

Zapier 错误: “我们无法通过您的测试。 该应用返回 "Internal Server Error",但没有更多详细信息。您连接的应用程序的服务器似乎已关闭或当前遇到问题。如果没有报告的问题,请检查应用程序的状态页面或联系支持人员。"

<?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 的电子邮件。

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

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

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'] ?? '';