将Zapier与PHPMailer脚本连接

时间:2019-05-21 16:17:06

标签: php phpmailer zapier

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

除一件事外,所有事物都起作用。 PHPMailer似乎会对zapier造成麻烦,因为每当启用email()函数时,Zapier都会给我一个错误。

FYI-当我转到脚本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的电子邮件。

1 个答案:

答案 0 :(得分:0)

两个关键问题。首先,您使用的是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'] ?? '';