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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动