Using custom 'Contact Us' form to add e-mails

Regular Participant

Using custom 'Contact Us' form to add e-mails

Hi,

 

I've been looking for solutions to this problem and I just keep getting more and more confused. I have some experience with PHP programming, but I'm hardly an expert.

 

What I'm hoping for is a relatively simple snippet of code that can open up a channel when the contact form is submitted to my CC account, check if the address to be added is already on the chosen list and if not add it along with the First/Last name of the new mail contact.

 

As this is a custom contact form, it asks for a lot of other information - it's part of a booking enquiry form that the visitor fills in with any holiday queries they have. Once they've filled in the form, there's a check box at the end that asks for their permission to be added to the CC mailing list and once they have made their choice, the form will be submitted sending me the booking enquiry while adding their contact details onto the CC mailing list if they choose to be added.

 

Currently, I'm using an IF statement with the default condition being that they don't get added to the list, and the ELSE condition being if they choose to be added. This is where I get stuck as I'm not sure which is the best solution - as stated, I'm hoping that a relatively simple snippet of code can handle this...

 

Any advice and guidance on this will be much appreciated, as I've experimented with straightforward PHP code and OAuth and both seem to go wrong somewhere, only I don't know where the problem lies as the server doesn't produce any error messages - just a blank screen (which is not what's supposed to happen, as I've indicated that a confirmation message should appear and I have that working fine without the API-related code)

 

Thanks

12 REPLIES 12
Employee

Hello,

 

Based on the situation you describe, I would suggest using our new PHP library that we have recently developed.  This will allow you to add to the php that you already have, with just a few extra lines, and it will allow you to integrate with us.  It also has a wiki that can be really helpful for someone who is new to the library, and new to php in general.

 

You can download the php library here, and install it to the directory that the php for the form you currently have is located.  Once you have it there, you will want to make sure to read the wiki, it has a getting started section where it will show you how to include the library.

 

Once the library is included in your php, you will want to follow the direction here, which will give you examples of code you can put into the section of your php that is run after the form data is posted.

 

If you are still running into trouble, feel free to send the php that you have to us, and we can see what is going on.

 

Regards,

Benjamin Soder
NOC Analyst
Constant Contact
Regular Participant

Thanks so much for your reply - I tried what you suggested, but I still keep running into problems. I have sent an e-mail to you with the code I am using.

 

Thanks and Kind Regards

Regular Participant

Hello,

 

I apologise for taking so long - thank you so much for your help and reply. You were right, the script I wrote up does work. I tested it on another server that is very up to date with its capabilities, so I think that the website it's intended to be used on has some kind of server issue (apart from running a slightly older version of PHP - PHP 5.0.5)

 

With that knowledge, I'll be working on figuring out why the intended server doesn't like the script - it's plain HTML and PHP so there's nothing to cause any issues, also all warnings and errors have been dealt with.

 

Thank You again for your help : )

Hello,

 

No problem!  Again, please feel free to ask us for help here if you run into any issues!

 

Regards,

Benjamin Soder
NOC Analyst
Constant Contact
Regular Participant

Hi again,

 

I've just gone and complicated things for myself...The code works fine, but I completely forgot to put in a function that searches if a contact already exists before adding them...

 

I'm using the following code (I've paraphrased some parts):

 

(Check if visitor has selected NO to being added to mailing list - if positive, send e-mail only)

else

{

$search = $ConstantContact->searchContactsByEmail($email);

 

if(isset($search))

{

echo "Already on the list";

mail("my e-mail address", $subject, $message, $from);

}

else

{

(execute code to add visitor to mailing list and send e-mail)

}

}

 

I'm not sure if that works...when I test it, it seems to be working okay in that if a contact exists the e-mail is still sent (which didn't happen before, as it would flag up the error about a contact already existing since it didn't know what to do) however, when I add an e-mail address that doesn't exist, the e-mail is sent, but the contact isn't added...

 

Also, after a few goes of this, I am now getting the following error:

 

Warning: mail() [function.mail]: SMTP server response: 554 <'my e-mail address'>: Recipient address rejected: Relay access denied in E:\directory\testing\enquiry_response_cb_nostyle.php on line 95

 

Any help is much appreciated - Thank you!

Hello,

 

Thanks for your inquiry.

 

Regarding the contact not being added to your account, it would be helpful if you could provide the exact code that you are using in place of "(execute code to add visitor to mailing list and send e-mail)" in your example.  We will do the best we can to troubleshoot the problem within your code.

 

Also, it appears that you are using the mail function, which is not related to Constant Contact.  The mail function typically works with your own local mail server.  This would explain the SMTP error message that you are receiving.  I would suggest reviewing your mail server settings in order to troubleshoot this error.

 

Thanks,

Matt R.
Support Engineer, Constant Contact
Regular Participant

Hi Matt,

 

I apologise for that - I thought that since the code to add the contact to the mailing list worked on its own then I didn't need to supply it...but here is the code I'm working with in its entirety, with some aspects omitted.

 

---

 

<?php

include_once('ConstantContact.php');

$ConstantContact = new ConstantContact('basic', "API Key", "username", "password");

 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Page Title</title>

</head>

<body>

 

<?php

$fname = $_POST['FName'];

$lname = $_POST['LName'];

$address = $_POST['Address'];

$towncity = $_POST['TownCity'];

$countystate = $_POST['CountyState'];

$postcodezip = $_POST['PostCodeZip'];

$countryregion = $_POST['CountryRegion'];

$telephone = $_POST['Telephone'];

$email = $_POST['Email'];

$divingqualification = $_POST['DivingQualification'];

$liveaboard = isset($_POST['Liveaboard']);

$destination = $_POST['Destination'];

$airport = $_POST['Airport'];

$traveldate = $_POST['DateOfTravel'];

$duration = $_POST['Duration'];

$party = $_POST['NumberTravelling'];

$notes = $_POST['SpecialRequests'];

 

if(isset($_POST['RedSea']))

$redsea = $_POST['RedSea'];

else

$redsea = ' ';

 

if(isset($_POST['Worldwide']))

$worldwide = $_POST['Worldwide'];

else

$worldwide = ' ';

 

if(isset($_POST['Nature']))

$nature = $_POST['Nature'];

else

$nature = ' ';

 

if(isset($_POST['Italy']))

$italy = $_POST['Italy'];

else

$italy = ' ';

 

if(isset($_POST['Turkey']))

$turkey = $_POST['Turkey'];

else

$turkey = ' ';

 

if(isset($_POST['Canaries']))

$canaries = $_POST['Canaries'];

else

$canaries = ' ';

 

$checkbox = isset($_POST['JoinNewsletter']);

 

$nosend = "<h2>Your Enquiry was not sent.</h2> \n";


if (eregi('http:', $notes))

{

die ("Do NOT try that!!");

}

 

$email=strtolower($email);

if(!$email == "" && (!strstr($email,"@") || !strstr($email,".")))

{

echo $nosend;

echo "<h2>Please return to the previous window and make sure to enter a valid E-Mail address.</h2>\n \n";

die;

}

 

if(empty($fname) || empty($lname) || empty($email))

{

echo $nosend;

echo "<h2>Please return to the previous window and make sure that you have entered your name and e-mail address.</h2>\n \n";

die;

}

$attn = "Website Booking Enquiry";

$subject = $attn;

$notes = stripcslashes($notes); 
$message = "Booking Enquiry\n \n Name: $fname $lname\n \n Address: $address\n \n Town or City: $towncity\n \n County or State: $countystate\n \n Post/Zip Code: $postcodezip\n \n Country or Region: $countryregion\n \n Telephone: $telephone\n \n Email: $email\n \n Diving Qualification: $divingqualification\n \n Liveaboard Preferred: $liveaboard\n \n Destination: $destination\n \n Airport Preference: $airport\n \n Date of Travel: $traveldate\n \n Duration: $duration\n \n Number Travelling: $party\n \n Special Requests: $notes\n \n Areas of Interest:\n $redsea; $worldwide; $nature; $italy; $turkey; $canaries";$from = "From: $email\r\n";

 

if($checkbox=="NO")

mail("email@address.com", $subject, $message, $from);

else

{

$search = $ConstantContact->searchContactsByEmail($email);

$result = $ConstantContact->getContactDetails($search[0]);

 

if($email == $result->emailAddress)

{

echo "Already on the list";

mail("email@address.com", $subject, $message, $from);

}

else

{

$lists = $ConstantContact->getLists();
$contactObj = new Contact();

$contactObj->lists = array($lists['lists'][0]->id);

$contactObj->emailAddress = $email;

$contactObj->firstName = $fname;

$contactObj->lastName = $lname;

 

// Create the contact

$Contact = $ConstantContact->addContact($contactObj);

echo "<h2><p align='center'>Thank you for joining our mailing list!</p></h2>";

mail("email@address.com", $subject, $message, $from);

}

}

?>

 

<br /><br />

<p align="center"><strong>Thank you for your enquiry!</strong></p>

<p align="center"><strong>The  form has been submitted and is being processed by Company Name.</strong></p>

<p align="center"><strong>You will be contacted by Company Name A.S.A.P.</strong></p>

<br />

<br />

</body>

</html>

 

---

 

That's the code without the stylesheet applied.

 

Regarding the SMTP issue...when you say 'Mail Server' are you referring to the the one the e-mail address is registered with (i.e. the hosting service we use, rather than the program we use to view our e-mails? ) I don't have direct access to our server information as all of this is handled by our offsite technical support company. Could you let me know what I should instruct them to look for so this issue can be resolved?

 

Many Thanks and Kind Regards.

Thanks for providing your code.  We are going to look into this and I will get back to you as soon as possible.

Matt R.
Support Engineer, Constant Contact

Hi,

I believe that the problem lies in the following isset statement:

else
{
$search = $ConstantContact->searchContactsByEmail($email);
if(isset($search))

Currently, this is just checking to see if the $search variable has been declared.  Since it was declared, this will always be true.  So, you should remove isset so that the statement appears as follows:

else
{
$search = $ConstantContact->searchContactsByEmail($email);
if($search)

I hope this helps!

Regards,

Matt R.
Support Engineer, Constant Contact
Regular Participant

Hi Matt,

 

Thanks so much - that solution works! It didn't occur to me to use the 'if' statement that way...

 

Now I have to figure out how to deal with the SMTP issue...could you clarify as to whether the issue is to do with the webhost hosting the page or the address the e-mail is being sent to? The page and e-mail address are being hosted by different servers - I know, it's not the best solution, but it's the only one we have.

 

If it's to do with the host of the e-mail address, I won't be able to fix it directly as it is being managed by a third party (our support company) Could you provide me with information to guide them on what they need to do?

 

Thanks again for your help!

Generally speaking, the error you're receiving from the SMTP mail request indicates that you aren't able to send the email through your SMTP server.  That said, error codes are not required to be returned exactly through all email domains.  To test this out, I would recomend sending the email to a Gmail or Hotmail address, one you know has a receiving mail server that is up and allows email.  This should take out the receiving mail server from the equation.

 

The most common cause for that error is that the SMTP server you're trying to use is saying that you are not authorized to send through them, typically for some sort of security reason not related to bad username/password (though it could be related to that). 

Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

Hi Dave,

 

Thank you for your reply.

 

I tested sending the form instead to a Gmail/Hotmail address and both worked fine. I don't think we've had the error for some time, but there's no way to tell as the instances aren't recorded in the error log. As far as I know (from when investigating the issue) it occurs intermittently...

Developer Portal

View API documentation, code samples, get your API key.

Visit Page