dongya8378 2015-06-19 10:22
浏览 39
已采纳

如何将这两个PHP代码段合并在一起发送电子邮件并将XML发送到数据库?

I am generating leads via website forms. The old system is to get the leads emailed using the below code:

<?php
if(isset($_POST['submit'])) {
$to = "example@example.com";
$subject = "Email Subject";

// data the visitor provided
$title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
$first_name = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$last_name = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
$when_to_call = filter_var($_POST['whentocall'], FILTER_SANITIZE_STRING);
$house_number = filter_var($_POST['housenumber'], FILTER_SANITIZE_STRING);
$postcode = filter_var($_POST['postcode'], FILTER_SANITIZE_STRING);
$kw = filter_var($_POST['kw'], FILTER_SANITIZE_STRING);
$pos = filter_var($_POST['pos'], FILTER_SANITIZE_STRING);
$device = filter_var($_POST['device'], FILTER_SANITIZE_STRING);
$adgroup = filter_var($_POST['adgroup'], FILTER_SANITIZE_STRING);

//constructing the message
$body = " From: $title $first_name $last_name

 Email: $email

 Phone: $phone

 When to call: $when_to_call

 House Name/Number: $house_number

 Postcode: $postcode 

 Keyword: $kw 

 Position: $pos 

 Device: $device 

 ad group: $adgroup";

// ...and away we go!
mail($to, $subject, $body);

// redirect to confirmation
header('Location: callbackconfirm.php');
} else {

}
?>

Our new system is to send the leads via XML straight into an online CRM, here is the code I have which works just fine:

<?php
echo "Thank you for your submission ".$_POST["firstname"]; 

$XML = "";
$XML .= "<Application><Cases><Case>";

$XML .= "<CreateCase>1</CreateCase>";

$XML .= "<FirstName>".$_POST["firstname"]."</FirstName>";

$XML .= "<LastName>".$_POST["surname"]."</LastName>";

$XML .= "<HomeTelephone>".$_POST["phone"]."</HomeTelephone>";

$XML .= "<HouseNumber>".$_POST["housenumber"]."</HouseNumber>";

$XML .= "<PostCode>".$_POST["postcode"]."</PostCode>";

$XML .= "<KEYWORD>".$_POST["kw"]."</KEYWORD>";

$XML .= "<ADGROUP>".$_POST["adgroup"]."</ADGROUP>";

$XML .= "<DEVICE>".$_POST["device"]."</DEVICE>";

$XML .= "<POSITION>".$_POST["pos"]."</POSITION>";

$XML .= "<URL>".$_POST["currentUrl"]."</URL>";

$XML .= "<IPADDRESS>".$_POST["ipaddress"]."</IPADDRESS>";

$XML .= "<SourceName>".$_POST["adgroup"]."</SourceName>";

$XML .= "</Case></Cases></Application>";

postXML($XML);



function postXML($XML){
$url = "CRM URL";

$headers = array(
    "Content-Type: application/x-www-form-urlencoded"
);

$post = http_build_query(
    array("XMLApplication" => $XML)
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

curl_close($ch);
}

?>

What I need is to be able to merge these two together so that when the magic submit button is pressed, it sends this XML feed into the software and generates the email at the same time.

I have tried and failed, can anybody help?

Many thanks

  • 写回答

3条回答 默认 最新

  • douping3891 2015-06-19 10:30
    关注

    Merge the code like this and remove the XML echo?

    <?php
    if(isset($_POST['submit'])) {
    $to = "example@example.com";
    $subject = "Email Subject";
    
    // data the visitor provided
    $title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
    $first_name = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
    $last_name = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
    $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
    $phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
    $when_to_call = filter_var($_POST['whentocall'], FILTER_SANITIZE_STRING);
    $house_number = filter_var($_POST['housenumber'], FILTER_SANITIZE_STRING);
    $postcode = filter_var($_POST['postcode'], FILTER_SANITIZE_STRING);
    $kw = filter_var($_POST['kw'], FILTER_SANITIZE_STRING);
    $pos = filter_var($_POST['pos'], FILTER_SANITIZE_STRING);
    $device = filter_var($_POST['device'], FILTER_SANITIZE_STRING);
    $adgroup = filter_var($_POST['adgroup'], FILTER_SANITIZE_STRING);
    
    //constructing the message
    $body = " From: $title $first_name $last_name
    
     Email: $email
    
     Phone: $phone
    
     When to call: $when_to_call
    
     House Name/Number: $house_number
    
     Postcode: $postcode 
    
     Keyword: $kw 
    
     Position: $pos 
    
     Device: $device 
    
     ad group: $adgroup";
    
    // ...and away we go!
    mail($to, $subject, $body);
    
    $XML = "";
    
    $XML .= "<Application><Cases><Case>";
    
    $XML .= "<CreateCase>1</CreateCase>";
    
    $XML .= "<FirstName>".$_POST["firstname"]."</FirstName>";
    
    $XML .= "<LastName>".$_POST["surname"]."</LastName>";
    
    $XML .= "<HomeTelephone>".$_POST["phone"]."</HomeTelephone>";
    
    $XML .= "<HouseNumber>".$_POST["housenumber"]."</HouseNumber>";
    
    $XML .= "<PostCode>".$_POST["postcode"]."</PostCode>";
    
    $XML .= "<KEYWORD>".$_POST["kw"]."</KEYWORD>";
    
    $XML .= "<ADGROUP>".$_POST["adgroup"]."</ADGROUP>";
    
    $XML .= "<DEVICE>".$_POST["device"]."</DEVICE>";
    
    $XML .= "<POSITION>".$_POST["pos"]."</POSITION>";
    
    $XML .= "<URL>".$_POST["currentUrl"]."</URL>";
    
    $XML .= "<IPADDRESS>".$_POST["ipaddress"]."</IPADDRESS>";
    
    $XML .= "<SourceName>".$_POST["adgroup"]."</SourceName>";
    
    $XML .= "</Case></Cases></Application>";   
    
    
    function postXML($XML){
    $url = "CRM URL";
    
    $headers = array(
        "Content-Type: application/x-www-form-urlencoded"
    );
    
    $post = http_build_query(
        array("XMLApplication" => $XML)
    );
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    
    curl_close($ch);
    
    postXML($XML);
    
    // redirect to confirmation
    header('Location: callbackconfirm.php');
    } else {
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器