Constant Contact API - php

I'm trying to write a simple Constant Contact script for adding and updating emails. The script is simple enough and should've ran smoothly. But when I start to include 'src/Ctct/autoload.php' the page just returns blank. I tried running it on another server and it works. But on my working server, it returns blank. It uses OAuth authentication from CTCT. I think it's a setting in the server, but I have no control over the server and any changes need to be forwarded to an admin, I just don't know what I need to change.
Here's the code:
require "Scripts/ConstantContact/src/Ctct/autoload.php";
use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;
define("APIKEY", "*** Censored Media (18+ only) ***");
define("ACCESS_TOKEN", "*** Censored Media (18+ only) ***");
$cc = new ConstantContact(APIKEY);
// attempt to fetch lists in the account, catching any exceptions and printing the errors to screen
$lists = $cc->getLists(ACCESS_TOKEN);
$action = "Getting Contact By Email Address";
$Email = "asdf#asdf.com";
$FirstName = "Asdf";
$LastName = "Ghjk";
// check to see if a contact with the email addess already exists in the account
$response = $cc->getContactByEmail(ACCESS_TOKEN, $Email);
// create a new contact if one does not exist
if (empty($response->results)) {
$action = "Creating Contact";
$contact = new Contact();
$contact->addEmail($Email);
$contact->addList('1');
$contact->first_name = $FirstName;
$contact->last_name = $LastName;
$returnContact = $cc->addContact(ACCESS_TOKEN, $contact);
// update the existing contact if address already existed
} else {
$action = "Updating Contact";
$contact = $response->results[0];
$contact->addList('1');
$contact->first_name = $FirstName;
$contact->last_name = $LastName;
$returnContact = $cc->updateContact(ACCESS_TOKEN, $contact);
}
// catch any exceptions thrown during the process and print the errors to screen
if (isset($returnContact)) {
echo '<div class="container alert-success"><pre class="success-pre">';
print_r($returnContact);
echo '</pre></div>';
}
print '<p>'.$action.'</p>';
Again, this works on another server I tried, but doesn't work on my working server.
Any help would be appreciated.
Thanks!

Are you running PHP 5.3 or higher on the other server? Also did the domain change at all, if so that may throw an exception resulting in a blank page as your API key is domain specific. Feel free to shoot me an email and I will be glad to help you out with this - mstowe [at] constantcontact.com
-Mike

Related

Zend registry mail sender : How to debug?

Mail doesn't get sent properly. I need to help to properly debug as to why it isn't working on the server. Any solution?
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);
$mailer = Zend_Registry::get('mailer');
if ($to) {
$mailer->setsendBcc(true);
}
// Add admin emails as recipient.
$to[$config->email->to] = '';
$mailer->setTo($to);
$mailer->setTokens($mailParams);
$mailer->setTemplate($template);
$result = $mailer->send();
Zend_Registry only stores stuff (eg. objects). We don't know what object is '$mailer'. It's not standard Zend_Mail object. You would need to check it - get_class($mailer) or look for Zend_Registry::set('mailer'.
You can always try:
try{
$result = $mailer->send();
}catch(Exception $e){
echo $e->getMessage();
}
Edit:
Also check if you are setting recipient correctly. It looks a bit odd in your code.

Wordpress + Constant Contact PHP SDK include path

I'm current trying to implement the Constant Contact PHP sdk into a wordpress site. Ask part of the SDK you have to include some core PHP files in order for the email code to work. If those files aren't found, the page dies with no PHP error. Currently I have the folder called "src" 9their naming convention not mine) in the root of the site's theme (/wp-content/themes/mytheme/src...).
Here is the email form code that calls the files:
<?php
// require the autoloader
require_once(TEMPLATEPATH . '/src/Ctct/autoload.php');
use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;
// Enter your Constant Contact APIKEY and ACCESS_TOKEN
define("APIKEY", "XXXXXX");
define("ACCESS_TOKEN", "XXXXXX");
$cc = new ConstantContact(APIKEY);
// attempt to fetch lists in the account, catching any exceptions and printing the errors to screen
try {
$lists = $cc->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
foreach ($ex->getErrors() as $error) {
print_r($error);
}
}
// check if the form was submitted
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['first_name'];
$contact->last_name = $_POST['last_name'];
/*
* 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, false);
// update the existing contact if address already existed
} else {
$action = "Updating Contact";
$contact = $response->results[0];
$contact->addList($_POST['list']);
$contact->first_name = $_POST['first_name'];
$contact->last_name = $_POST['last_name'];
/*
* 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, false);
}
// catch any exceptions thrown during the process and print the errors to screen
} 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();
}
}
?>
autoload.php is available where it should be at http://www.thedaileymethod.com/_main_site/wp-content/themes/dailey-method/src/Ctct/autoload.php which is what I thought I called in the PHP file, but the page keeps on breaking for me.
Do I have the require_once path incorrect?
EDIT:
When the for code is loaded, the page 500 errors. When I remove the form code, that error goes away and the page loads fine. Nothing nothing in the log from a PHP error standpoint.
Remove statements with use use keyword and change
$cc = new ConstantContact(APIKEY);
to
$cc = new \Ctct\ConstantContact(APIKEY);

Receiving incoming email: Google AppEngine PHP

Per the instructions (https://developers.google.com/appengine/docs/php/mail/), I'm expecting that when users send email to a configured email address, my designated script will get invoked with POST fields containing the data from the email. I have gotten it set up so that my script is being invoked BUT ... the $_POST array is empty. (The "else" block below is getting executed.)
The code:
if ($_POST) {
$email = $_POST['sender'];
$content = $_POST['original'];
$subject = $_POST['subject'];
}
else {
syslog(LOG_ERR, "[handle_incoming_email.php] EMPTY POST fields... Bailing.");
return;
}
Any ideas on why this might be?
Thanks so much.
Liz

bcc multiple addresses with swiftmailer

Im using the below php code to send an email to one address and bcc 2 other addresses. It sends to the recipient fine but I can only get it to send to one of the 2 bcc addresses. (see comments in code for what ive tried)
Oddly enough though, $result comes back as 3 so it seems that its trying to send the second bcc email but it never comes through.
<?php
$tracker='tracking#pnrbuilder.com';
$subject = $_POST['subject'];
$sender = $_POST['sender'];
$toEmail=$_POST['toEmail'];
$passedInEmail=stripslashes($_POST['message']);
$passedInEmail=preg_replace('/ /',' ',$passedInEmail);
require_once('swiftLib/simple_html_dom.php');
require_once('swiftLib/swift_required.php');
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance();
//turn the meesage into an object using simple_html_dom
//so we can iterate through and embed each image
$content = str_get_html($passedInEmail);
// Retrieve all img src tags and replace them with embedded images
foreach($content->find('img') as $e)
{
if($e->src != "")
{
$value = $e->src;
$newValue = $message->embed(Swift_Image::fromPath($value));
$e->src = $newValue;
}
}
$message->setSubject($subject);
$message->setFrom($sender);
$message->setTo($toEmail);
//this is my problem
$message->setBcc(array('tracking#pnrbuilder.com',$sender));
//as it is above only "sender" gets the email
//if I change it like this:
//$message->setBcc($tracker,$sender);
//only "tracker" gets the email
//same if I change it like this:
//$message->setBcc($sender);
//$message->addBcc($tracker);
$message->setReplyTo(array('flights#pnrbuilder.com'));
$message->setBody($content,'text/html');
$result = $mailer->send($message);
if ($result=3) {
echo 'Email Sent!';
}
else {
echo 'Error!';
}
?>
What is the proper way to do this?
You can find the swiftmailer tutorial here
example:
$message->setBcc(array(array('some#address.tld' => 'The Name'),array('another#address.tld' => 'Another Name')));
Try setting the names for the email addresses and see if it makes any difference.
This ended up being an issue on the server side, I contacted my hosting provider (GoDaddy) who were able to make some changes on their end fixing the problem. Thank you to all those who tried to help!

Swift-Mailer error, "Address in mailbox given [] does not comply with RFC"

I have built a simple PHP contact form that is supposed to send mail trough the Swift-Mailer script.
Problem is I keep getting this error
Uncaught exception
'Swift_RfcComplianceException' with
message 'Address in mailbox given []
does not comply with RFC 2822, 3.6.2.'
Which I guess means I am using an invalid e-mail address. But since I am using myaddress#gmail.com to test the scrip the problem is probably somewhere else. This is my configuration:
Where the mail is sent to:
$my_mail = 'mymail#mydomain.com';
$my_name = 'My Name';
The content of the message:
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$date = date('d/m/Y H:i:s');
$ipaddress = $_SERVER['REMOTE_ADDR'];
$content = $message.'\n\nSent on: '.$date.' From: '.$ipaddress;
The function i use to send the mail using swiftmailer:
function send_mail () {
require('/path/to/swift_required.php');
//The means of transport
$transport = Swift_SmtpTransport::newInstance('mail.mydomain.com', 25);
$transport->setUsername('myusername');
$transport->setPassword('mypass');
$mailer = Swift_Mailer::newInstance($transport);
//The message
$mail = Swift_Message::newInstance();
$mail->setSubject('Hello');
$mail->setFrom(array($email => $name ));
$mail->setTo(array($my_mail => $my_name));
$mail->setBody($content, 'text/plain');
//Sending the message
$test = $mailer->send($mail);
if ($test) {
echo '<p>Thank you for contacting us '.$name.'! We will get in touch soon.</p>';
}
else {
echo '<p>Something went wrong. Please try again later.</p>';
}
}
As you can see it is really simple form with three fields, name, mail and message. I also have other validation set up for each of contact form fields, but I think it is of little concern here.
Thank you for the help.
Edit:
Just test with using gmail as the smtp server. Unfortunately it still gives the same exact results.
All your data variables (addresses, names...) appear to be global. Global variables cannot be read from within functions unless you pass them as parameters (the recommended way) or use the global keyword (or the $GLOBALS array).

Categories