Triggering conversion tracking code on form submit - forms

I have a PHP form that mail()s the form data on submit and then if successful returns them to the referring page (in other words keeping them on the same page as the form) and appends ?success=TRUE to the URL.
The question is, how would I implement the AdWords and Yahoo Search Marketing conversion code snippets to trigger only when the form is submitted? For functionality purposes, it is unfortunately not feasible to send them to another page on submit which would have been the easiest way to do it.
The relevant code from the form submit action that mails the results and sends them back to the homepage is below. I have a hunch it might be as simple as outputting the conversion tracking code snippets in the if statement at the end there but I'm not sure if that is correct or the syntax to properly do that.
if ( isset($_POST['sendContactEmail']) )
{
$fname = $_POST['posFName'];
$lname = $_POST['posLName'];
$phone = $_POST['posPhone'];
$email = $_POST['posEmail'];
$note = $_POST['posText'];
$to = $yourEmail;
$subject = $yourSubject;
$message = "From: $fname $lname\n\n Phone: $phone\n\n Email: $email\n\n Note: $note";
$headers = "From: ".cleanPosUrl($_POST['posFName']. " " .$_POST['posLName'])." \r\n";
$headers .= 'To: '.$yourName.' '."\r\n";
$mailit = mail($to,$subject,$message,$headers);
if ( #$mailit ) {
header('Location: '.$referringPage.'?success=true');
}
else {
header('Location: '.$referringPage.'?error=true');
}
}

Outputting it in the if-Statement would be a possibility, but the script you posted adds another way to do it as it redirects to the $referringPage - if the mail was successfully sent. And that's the only event you want to track a conversion.
So edit the code of $referringPage (the page that holds the form fields) and add:
<?php
if($_GET['success'] == 'true') {
echo "...";
}
?>
"..." ofcourse has to be replaced by the Adwords conversion Code Google gave you.
If you add it to your question, I could even add it to my answer.

Related

Why might php mail sent but not received unless I manually call the script?

Why might php mail not be received in the second case where twilio is calling the page? Are there characters that might be returned from the api that make the email undeliverable?
1) If I type the URL into the broweser, it attempts to make a wav file in my recordings folder and I get the emails and text messages to all the recipients. The output below is served and the "1" indicates the message was sent.
2) If twilio calls the page and handles the xml, the actual message is recorded to my server, and the output below is served to twilio (which I have verified in my account), but no one receives the email or text. The body of the xml still shows "1" after good bye, indicating the message was sent.
My script:
<?php
date_default_timezone_set('America/New_York');
//copy the remote wav file to my server
$recording = file_get_contents($_REQUEST['RecordingUrl']);
$name = "recordings/".str_replace("+","",$_REQUEST['Caller'])."-".date('Y-m-d-G-i-s',time()).".wav";
$fh = fopen("../".$name, 'w') or die("can't open file");
$stringData = $recording;
fwrite($fh, $stringData);
fclose($fh);
//email the people that need to get the message
$to = "email1#yahoo.com";
$subject = "Voicemail from ".$_REQUEST['From'];
$message = "Click below to listen to your message:\n\r http://domain.com/twilio/".$name;
$from = "email2#domain.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
$to = "email3#domain.com";
mail($to,$subject,$message,$headers);
$to = "9545555555#messaging.sprintpcs.com";
$sent = mail($to,$subject,$message,$headers);
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n
<Response>
<Say>Thank you for your message. Good bye.".$sent."</Say>
<Hangup/>
</Response>";
?>
outputs:
<Response>
<Say>Thank you for your message. Good bye.1</Say>
<Hangup/>
</Response>
I just check to see what the contents of each variable in the php mail function are when twilio calls the script to see if there is a character present from a live twilio message. It comes out like this:
$to = 9545555555#messaging.sprintpcs.com
$subject = Voicemail from +19545555555
$message = Click below to listen to your message:
http://domain.com/twilio/recordings/19545555555-2013-10-12-22-57-03.wav
$headers = From:email2#domain.com
If I call the script manually in the browser they are:
$to = 9545555555#messaging.sprintpcs.com
$subject = Voicemail from
$message = Click below to listen to your message:
http://domain.com/twilio/recordings/-2013-10-12-23-04-37.wav
From:email2#domain.com
I tried removing the "+" out of the subject with no success, but when I removed the entire phone number from the subject it works every which way. The emails and texts are delivered appropriately. It makes no sense to me. Maybe its godaddy webhosting messing with my life again.
Perhaps the from string has bad characters that ruin the mail() call, but echo transparently. Try cleaning your string before putting it into mail().
Something like:
$subject= preg_replace("/[^a-zA-Z0-9]+/", "", $_REQUEST['From']);

PHP mail function not working

I do not think the problem lies with the mail function code, but with my aproach of the $_SESSION variables. I have a form consisting of 5 pages, the fifth being a preview page. Upon the final submission, on the preview page, i want the entire $_SESSION data to be sent to two different email adresses.
I am displaying the data on the preview page as follows:
<?php
//retrieve session data
echo "<b> Varname: </b>". $_SESSION['varname'];
?>
in a form with method="post" and action="mail.php".
In the mail.php, i start the session, and then:
$_SESSION['email'] = $mail;
$_SESSION['varname'] = $varname;
$email_from = 'mail#company.de';
$email_subject = "Mail";
$email_body = "You have submitted the following data: $inhalt.\n";
$to = "mymail#company.de, $mail";
$headers = "From: Company";
mail($to,$email_subject,$email_body,$headers);
Upon submitting the form the page goes to blank. What exactly am i doing wrong?
I managed to solve the problem in the end, as follows:
$email_from = 'mail#company.de';
$email_subject = "Mail";
$to = ("myadress#work.de," . $_SESSION['email'] . "");
mail($to,"Form submission","Form data:
Inhalt: " . $_SESSION['inhalt1'] . "
");
As I said in the question, the problem was my aproach of the $_SESSION variables. Instead of $_SESSION['varname'] = $varname, i just went directly with . $_SESSION['varname'] .´.

PHP mail to variable

I've searched and searched for something similar, but i think i'm not doing it right. So i will ask a question. This is very basic. My problem is as follows:
I have a multi-page form, consisting of 4 pages + 1 preview page. On the preview page, upon submitting i want the entire form data to be sent to 2 different mail adresses. One standard, and the other one, the one that the user has submitted.
So i have:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
?>
<?php
$email_from = 'mail#company.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "yourname#yourwebsite.com, $email /n";
$headers = "From: Company";
mail($to,$email_subject,$email_body,$headers);
?>
How should I insert the submitted $email variable in order for it to work?
Remove the \n
$to = 'yourname#yourwebsite.com,' . $visitor_email;
According to the mail() function documentation the $to parameter will take a comma-separated list of addresses as you have attempted, but should have no line break at the end.
Also, your variable is $visitor_email, rather than $email.
$to = "yourname#yourwebsite.com, $visitor_email";
You might also consider adding the visitor's email as a CC or BCC rather than the TO address. In that case, add it as a CC or BCC header (separated by \r\n), but you need to be cautious to be sure that the address is an email address and contains no line break characters which could be used for header injection.
// The From should be an email address
$headers = "From: company#example.com\r\n";
$headers .= "CC: $visitor_email;

CodeIgniter: Cannot redirect after $this->email->send()

i have the following code which sends an email from a posted form:
$this->load->library('email');
$config['charset'] = 'iso-8859-1';
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from('info#mysite.com', 'Scarabee');
$this->email->to('info#mysite.com');
$this->email->subject('Message via website');
$data['msg'] = nl2br($this->input->post('msg'));
$data['msg'] .= '<br><br><b>Verstuurd door:</b><br>';
if($this->input->post('bedrijf')){
$data['msg'] .= $this->input->post('bedrijf').'<br>';
}
$data['msg'] .= $this->input->post('naam').'<br>';
$data['msg'] .= $this->input->post('adres').' - '.$this->input->post('postcode').', '.$this->input->post('gemeente').'<br>';
$data['msg'] .= $this->input->post('tel').'<br>';
$data['msg'] .= $this->input->post('email');
$message = $this->load->view('email', $data, TRUE);
$this->email->message($message);
if($this->email->send()){
$success = 'Message has been sent';
$this->session->set_flashdata('msg', $success);
redirect('contact/'.$this->input->post('lang'));
}
else{
show_error('Email could not be sent.');
}
Problem: the email gets sent with proper formatting (from the email view template), but the page then goes blank. For instance, if I try to echo out $message just below the $this->email->send() call, nothing shows up. Redirecting, as I’m attempting above, obviously doesn’t work either. Am I missing something? Thanks for any help…
Update
Traced the problem down to a function inside /system/libraries/Email.php (CI's default email library). Commenting out the code inside this function allows the mail to get sent, as well as a proper redirect:
protected function _set_error_message($msg, $val = '')
{
$CI =& get_instance();
$CI->lang->load('email');
if (substr($msg, 0, 5) != 'lang:' || FALSE === ($line = $CI->lang->line(substr($msg, 5))))
{
$this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
}
else
{
$this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
}
}
The line that caused the error is: $CI->lang->load('email');
Go figure...
UPDATE 2: SOLVED
I had this in my controller's construct:
function __construct() {
parent::__construct();
$this->lang = $this->uri->segment(2, 'nl');
}
I guess there was a conflict between $this->lang and the _set_error_message function which also returns a "lang" variable (see var_dump further down).
Solution: changed $this->lang to $this->language, and everything's hunky dory!
Thought I'd post the answer here in case anyone else is ever losing hair with a similar problem :-)
It's likely that an error is occurring when you attempt to send an e-mail which is then killing your script during execution. Check your apache and PHP error logs ( /var/log/apache/ ) and enable full error reporting.
You could, for debugging purposes, try doing this:
...
$this->email->message($message);
$this->email->send();
redirect('contact/'.$this->input->post('lang'));
This should redirect you, regardless of your mail being sent or not (if you still recieve it, you can assume that the redirect would be the next thing that happens).
My own solution looks like this:
...
if ( $this->email->send() )
{
log_message('debug', 'mail library sent mail');
redirect('contact/thankyou');
exit;
}
log_message('error', 'mail library failed to send');
show_error('E-mail could not be send');
$ref2 = $this->input->server('HTTP_REFERER', TRUE);
redirect($ref);
By using the above code you can redirect to the current page itself
and you can set your flash message above the redirect function.

how to email form on submit using zend_form?

OK. I finally got my zend form working, validating, filtering and sending the contents to my process page (by using $form->populate($formData);)
Now, how do I email myself the contents of a form when submitted? Is this a part of Zend_form or some other area I need to be looking in?
Thanks!
You can use Zend Mail for this, Zend form does not offer such functions.. but its an nice idea.
In your controleller, after $form->isValid();
if ($form->isValid($_POST)) {
$mail = new Zend_Mail();
$values = $form->getValues();
$mailText = 'My form valueS: ';
foreach ($values as $v) {
$mailText .= 'Value ' . $v . PHP_EOL;
}
$mail->setFrom('user#user.de', 'user');
$mail->AddTo('Me#me.de', 'me');
$mail->setSubject->$form->getName();
$mail->send();
} else {
// what ever
}