Forum Moderators: coopster
The solution would be to add some code on the signup side, so that as well as submitting the form to Campaign Monitor, it sends it as an email to me. I would like to do this using PHP. The problem is I am not sure how!?
Currently, the code with the Campaign Monitor snippets looks like this:
<form action="http://mywebsite.createsend.com/t/r/s/hubt/" method="post">
<div>
<label for="name">Name:</label><br /><input type="text" name="cm-name" id="name" /><br />
<label for="hubt-hubt">Email:</label><br /><input type="text" name="cm-hubt-hubt" id="hubt-hubt" /><br />
<input type="submit" value="Subscribe" />
</div>
</form>
When I am not using Campaign Monitor I usually use the following PHP snippets to submit forms directly to my email:
This goes on the first line of my web page:
<?php
if (array_key_exists('submit', $_POST)) {
// mail processing script
// initialize variables
$to = 'info@mysite.com';
$subject = 'Membership Application';
// build the message
$message .= 'Name: '.$_POST['name']."\n\n";
$message .= 'Email: '.$_POST['email']."\n\n";
$additionalHeaders = "From: My Website<info@mysite.com>\r\n";
// send the email
mail($to, $subject, $message, $additionalHeaders);
}
?>
And this goes in the form section:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<div>
<label><input name="name" type="text" class="textareas" id="name" /></label><br />
<label><input name="email" type="text" class="textareas" id="email" /></label><br />
<input type="submit" value="submit" />
</div>
</form>
What I really need help with is how I can put these two together so I can submit the form to Campaign Monitor, as well as send it as an email to me.
All help is greatly appreciated!
The easiest way to post to campaign monitor would be using CURL. Do you have CURL available? If not, there's also a way to post using fputs.
I use both methods to do exactly what you're doing - post to an API and e-mail someone (plus I then save everything to the DB).
Let me know if you want the curl or non-curl solution.
// The POST URL and parameters
$request = 'http://mywebsite.createsend.com/t/r/s/hubt';
$postargs = '';
$postargs .= 'cm-name=' . $_POST['cm-name'] . '&cm-hubt-hubt=' . $_POST['cm-hubt-hubt'];
// Get the curl session object
$session = curl_init($request);
// Set the POST options.
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Do the POST and then close the session
$response = curl_exec($session);
curl_close($session);
// Get HTTP Status code from the response
$status_code = array();
// Check for errors
if ($status_code[0] == 199) //var_dump your status code to see what it contains - 199 is specific to my app
{
//success message
}
else
{
//fail message
}
I think I did this correctly; I have pasted below my code - unfortunately I can't get it to work. The email comes through to me, but the subscriber doesn't get added to Campaign Monitor. The Fail message also shows up when the page loads, which makes me think I have done something wrong? Perhaps the status codes? Silly question, but how do I var dump my status code?
<?php
if (array_key_exists('submit', $_POST)) {
// mail processing script
// initialize variables
$to = 'info@website.com';
$subject = 'Newsletter';
// build the message
$message .= 'Name: '.$_POST['cm-name']."\n\n";
$message .= 'Telephone: '.$_POST['cm-hubt-hubt']."\n\n";
$additionalHeaders = "From: My Company<info@mywebsite.com>\r\n";
// send the email
mail($to, $subject, $message, $additionalHeaders);
}
// The POST URL and parameters
$request = 'http://mywebsite.createsend.com/t/r/s/juujhj/';
$postargs = '';
$postargs .= 'cm-name=' . $_POST['cm-name'] . '&cm-hubt-hubt=' . $_POST['cm-hubt-hubt'];
// Get the curl session object
$session = curl_init($request);
// Set the POST options.
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Do the POST and then close the session
$response = curl_exec($session);
curl_close($session);
// Get HTTP Status code from the response
$status_code = array();
// Check for errors
if ($status_code[0] == 199) //var_dump your status code to see what it contains - 199 is specific to my app
{
//success message
echo "This worked";
}
else
{
//fail message
echo "This does not work";
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<div>
<label><input name="cm-name" type="text" class="textareas" id="cm-name" /></label><br />
<label><input name="cm-hubt-hubt" type="text" class="textareas" id="cm-hubt-hubt" /></label><br />
<input type="submit" name="submit" id="submit" value="Subscribe" />
</div>
</form>
</body>
</html>