使用常量联系邮件服务器而不是php邮件发送电子邮件

时间:2014-12-01 12:15:51

标签: php email constantcontact

从过去的几天开始,我在http://developer.constantcontact.com/libraries/sample-code.html搜索Constant Contact API中的解决方案,但无法找到它。所以,以下是我们的要求。

在我的网站上,我们有捐款,会员资格等。当用户填写捐款表格或会员表格或联系表格等时,一旦用户提交表格,我们需要向用户发送确认电子邮件,我们需要此电子邮件要从Contact Contact邮件服务器发送,而不是发送网格或php邮件服务器。我们已经不断联系发送新闻信件。我们需要使用这些详细信息,为单个用户发送condirmation电子邮件,并将电子邮件ID存储在我们的常规联系人帐户中。

我们需要有用的API以上述程序/方式进行沟通。

我们没有找到任何解决方案来为单个捐赠用户发送电子邮件。如何向单个用户发送电子邮件和消息。

目前我们正在使用第三方服务(发送网格),现在我们想转向Constant Contact。我们用PHP编程语言开发了这个网站。

请帮助我们整合上述程序/方式。

这里我附上了文件。当我使用给定的api和格式

时,我收到错误

以下是我的简单代码

     <?php

require_once 'Ctct/autoload.php';

use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Components\EmailMarketing\Campaign;
use Ctct\Components\EmailMarketing\MessageFooter;
use Ctct\Components\EmailMarketing\Schedule;
use Ctct\Exceptions\CtctException;

// Enter your Constant Contact APIKEY and ACCESS_TOKEN
define("APIKEY", "xxxxxxxxxxxxxxxxxxxxxxxx");
define("ACCESS_TOKEN", "xxxxxxxx-xxxx-xxxx-xxxx-989939366970");

$date = date('Y-m-d H:i:s');

$cc = new ConstantContact(APIKEY);

try {
    $lists = $cc->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
    foreach ($ex->getErrors() as $error) {
        print_r($error);
    }
}
print_r($lists);

if (isset($_POST['email']) && strlen($_POST['email']) > 1) {
    $action = "Getting Contact By Email Address";
    try {
        // check to see if a contact with the email addess already exists in the account
        $response = $cc->getContactByEmail(ACCESS_TOKEN, $_POST['email']);
        // create a new contact if one does not exist
        if (empty($response->results)) {
            $action = "Creating Contact";
            $contact = new Contact();
            $contact->addEmail($_POST['email']);
            $contact->addList($_POST['list']);
            $contact->first_name = $_POST['fname'];
            $contact->last_name = $_POST['fname'];
            /*
             * The third parameter of addContact defaults to false, but if this were set to true it would tell Constant
             * Contact that this action is being performed by the contact themselves, and gives the ability to
             * opt contacts back in and trigger Welcome/Change-of-interest emails.
             *
             * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
             */
            $returnContact = $cc->addContact(ACCESS_TOKEN, $contact, true);
            // update the existing contact if address already existed
        } else {
            $action = "Updating Contact";
            $contact = $response->results[0];
            $contact->addList($_POST['list']);
            $contact->first_name = $_POST['fname'];
            $contact->last_name = $_POST['fname'];
            /*
             * The third parameter of updateContact defaults to false, but if this were set to true it would tell
             * Constant Contact that this action is being performed by the contact themselves, and gives the ability to
             * opt contacts back in and trigger Welcome/Change-of-interest emails.
             *
             * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
             */
            $returnContact = $cc->updateContact(ACCESS_TOKEN, $contact, true);
        }

    }
        catch (CtctException $ex) {
        echo '<span class="label label-important">Error ' . $action . '</span>';
        echo '<div class="container alert-error"><pre class="failure-pre">';
        print_r($ex->getErrors());
        echo '</pre></div>';
        die();
    }
}



function createCampaign(array $params)
{
    $cc = new ConstantContact(APIKEY);
    $campaign = new Campaign();
    $campaign->name = $params['fname'];
    $campaign->subject = "Test Mail Subject";
    $campaign->from_name = $params['fname'];
    $campaign->from_email = $params['email'];
    //$campaign->greeting_string = $params['greeting_string'];
    //$campaign->reply_to_email = $params['email'];
    $campaign->text_content = $params['content_text'];
    $campaign->email_content = $params['content_text'];
    $campaign->email_content_format = 'HTML';
    // add the selected list or lists to the campaign
$campaign->addList('venky.para@gmail.com');
    return $cc->addEmailCampaign(ACCESS_TOKEN, $campaign);
}

function createSchedule($campaignId, $time)
{
    $cc = new ConstantContact(APIKEY);
    $schedule = new Schedule();
    $schedule->scheduled_date = $time;
    return $cc->addEmailCampaignSchedule(ACCESS_TOKEN, $campaignId, $schedule);
}

global $wpdb;
if(isset($_POST['submit']))
{
$fname=mysql_real_escape_string($_POST['fname']);
$useremail=mysql_real_escape_string($_POST['email']);
$content=mysql_real_escape_string($_POST['content_text']);


    // attempt to create a campaign with the fields submitted, displaying any errors that occur
    try {
        $campaign = createCampaign($_POST);
    } catch (CtctException $ex) {
        echo '<span class="label label-important">Error Creating Campaign</span>';
        echo '<div class="container alert-error"><pre class="failure-pre">';
        print_r($ex->getErrors());
        echo '</pre></div>';
        die();
    }

    // attempt to schedule a campaign with the fields submitted, displaying any errors that occur
    try {
        $schedule = createSchedule($campaign->id,$date);
    } catch (CtctException $ex) {
        echo '<span class="label label-important">Error Scheduling Campaign</span>';
        echo '<div class="container alert-error"><pre class="failure-pre">';
        print_r($ex->getErrors());
        echo '</pre></div>';
        die();
    }


}
?>
<script type="text/javascript">
function validtestmail(){
var fname=document.getElementById('fname').value;
var email=document.getElementById('email').value;
var content_text=document.getElementById('content_text').value;
if(fname=='')
  {
  alert('Enter Name');
  document.getElementById('fname').focus();
  return false;
  }
  else if(email=='')
  {
  alert('Enter Email');
  document.getElementById('email').focus();
  return false;
  }
  else if(content_text=='')
  {
  alert('Enter Content');
  document.getElementById('content_text').focus();
  return false;
  }


}
</script>
<div class="container ">
    <div class="row ">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div class="innerPagesBlock fullWidth">



                 <div id="primary" class="col-xs-12 col-sm-12 col-md-8 col-lg-8 content-area contactbg">
<div class="myprofileHeading">Change Password </div>
<form class="form-horizontal" role="form" id="member_cp" name="member_cp" action="<?php echo get_bloginfo('home'); ?>/testmail" method="post" onsubmit="return validtestmail();" style="margin-top:30px;">
            <!-- Self form start-->   

<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 formLog">
            <span style="font-size:16px; font-weight: bold; color: #FFFFFF;">
                <?php  
                if($msg)
                {?>
                    <div class="<?php echo $class; ?>" role="alert"><?php
                        echo $msg;
                        ?>
                    </div><?php
                } 
                ?>
                </span>
            </div>  


      <div class="form-login formMain">

        <label for="inputEmail3" class="col-xs-12 col-sm-4 col-md-4 col-lg-4 control-label">Name:<span>*</span></label>
        <div class="col-xs-12 col-sm-8 col-md-8 col-lg-8 formLog">
          <input type="text" class="form-control" id="fname" name="fname"  placeholder="Name" value="" >
        </div>
         <label for="inputEmail3" class="col-xs-12 col-sm-4 col-md-4 col-lg-4 control-label">Email: <span>*</span></label>
        <div class="col-xs-12 col-sm-8 col-md-8 col-lg-8 formLog">
          <input type="text" class="form-control" id="email" name="email" placeholder="Email" value="" >
        </div>
         <label for="inputEmail3" class="col-xs-12 col-sm-4 col-md-4 col-lg-4 control-label">Content: <span>*</span></label>
        <div class="col-xs-12 col-sm-8 col-md-8 col-lg-8 formLog">
          <input type="text" class="form-control" id="content_text" name="content_text" placeholder="Content" value="" >
        </div>

         <div class="col-xs-6 col-sm-2 col-md-2 col-lg-2 formLog">
      <button type="submit" class="btn btn-primary btnSubmit" name="submit" id="submit">Submit</button>
    </div>
     <div class="col-xs-6 col-sm-2 col-md-2 col-lg-2 formLog">
     <button type="reset" name="button2" id="button2" class="btn btn-primary btnReset">Reset</button>
    </div>



        </div>
        </form>
                </div>

            </div>
        </div>
    </div>
</div>


<div id="main-content" class="main-content">

<?php
    if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
        // Include the featured content template.
        get_template_part( 'featured-content' );
    }
?>

</div><!-- #main-content -->

此处如何向单个用户发送电子邮件。怎么样?。可能吗?。请帮我。我浪费了2周的时间。

感谢你

1 个答案:

答案 0 :(得分:0)

您需要将$ _POST ['list']转换为字符串。它可能是一个整数。

更改此内容:

$contact->addList($_POST['list']);

到此:

$contact->addList( (string)$_POST['list'] );

可能会这样做。