Linking contact form with thank-you page - php

I am trying to link contact form with thankyouurl.php so customized page will be displayed after sending an email. Everything works in the code except for that one bit. I have tried out a few different things, but php is not my strong side.
<?php
#ini_set('display_errors', 0);
#ini_set('track_errors', 0);
#date_default_timezone_set('Europe/Bucharest'); // Used only to avoid annoying warnings.
if($_REQUEST['action'] = 'email_send') {
$array['name'] = isset($_REQUEST['name']) ? strip_tags(trim($_REQUEST['name'])) : '';
$array['email'] = isset($_REQUEST['email']) ? ckmail($_REQUEST['email']) : '';
$array['subject'] = isset($_REQUEST['subject']) ? strip_tags(trim($_REQUEST['subject'])) : '-';
$array['message'] = isset($_REQUEST['message']) ? (trim(strip_tags($_REQUEST['message'], '<b><a><strong>'))) : '';
// Visitor IP:
$ip = ip();
// DATE
$date = date('l, d F Y , H:i:s');
// BEGIN
require('config.inc.php');
require('phpmailer/5.1/class.phpmailer.php');
$m = new PHPMailer();
$m->IsSMTP();
$m->SMTPDebug = false; // enables SMTP debug information (for testing) [default: 2]
$m->SMTPAuth = true; // enable SMTP authentication
$m->Host = $config['smtp_host']; // sets the SMTP server
$m->Port = $config['smtp_port']; // set the SMTP port for the GMAIL server
$m->Username = $config['smtp_user']; // SMTP account username
$m->Password = $config['smtp_pass']; // SMTP account password
$m->SingleTo = true;
$m->CharSet = "UTF-8";
$m->Subject = ($array['subject'] == '-') ? $config['subject'] : $array['subject'];
$m->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$m->AddAddress($config['send_to'], 'Contact Form');
$m->AddReplyTo($array['email'], $array['name']);
$m->SetFrom($config['smtp_user'], 'Contact Form');
$m->MsgHTML("
<b>Date:</b> {$date} <br>
<b>Name:</b> {$array['name']}<br>
<b>Email:</b> {$array['email']}<br>
<b>Subject:</b> {$array['subject']}<br>
<b>Message:</b> {$array['message']}<br>
---------------------------------------------------<br>
IP: {$ip}
");
if($config['smtp_ssl'] === true)
$m->SMTPSecure = 'ssl'; // sets the prefix to the server
// #SEND MAIL
if($m->Send()) {
header( "http://xyz.ie/thankyouurl.php" );
}
else
{
header( "http://xyz.ie/errorurl.php" );
}
function ip() {
if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); }
elseif (getenv('HTTP_X_FORWARDED_FOR')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); }
elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); }
elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); }
elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); }
else { $ip = $_SERVER['REMOTE_ADDR']; }
return $ip;
}?>

Use header location to redirect to a different page:
header("Location: http://xyz.ie/thankyouurl.php");
So change your code from:
// #SEND MAIL
if($m->Send()) {
header( "http://xyz.ie/thankyouurl.php" );
}
else
{
header( "http://xyz.ie/errorurl.php" );
}
to:
// #SEND MAIL
if($m->Send()) {
header("Location: http://xyz.ie/thankyouurl.php");
} else {
header("Location: http://xyz.ie/errorurl.php");
}
exit();
For more information you can visit: http://php.net/manual/en/function.header.php

Your using header wrong.
header("Location: http://xyz.ie/thankyouurl.php");
exit();

It's
header("Location: http://...");

Related

Email verification to know email really exists on server using php

i am trying to make a function where i can check if a email really exists or not i searched various forums but the code only works fine with gmail not others i am checking mx records can i do something to connect to those mx records and ask server if email really does exists or not.
I have added much of the code so that you can understand what do i need to do
Thank's
private function verifyEmail($toemail, $fromemail, $getdetails = false)
{
$result='';
$details=' ';
// Get the domain of the email recipient
$email_arr = explode('#', $toemail);
$domain = array_slice($email_arr, -1);
$domain = $domain[0];
// Trim [ and ] from beginning and end of domain string, respectively
$domain = ltrim($domain, '[');
$domain = rtrim($domain, ']');
if ('IPv6:' == substr($domain, 0, strlen('IPv6:'))) {
$domain = substr($domain, strlen('IPv6') + 1);
}
$mxhosts = array();
// Check if the domain has an IP address assigned to it
if (filter_var($domain, FILTER_VALIDATE_IP)) {
$mx_ip = $domain;
} else {
// If no IP assigned, get the MX records for the host name
getmxrr($domain, $mxhosts, $mxweight);
}
if (!empty($mxhosts)) {
$mx_ip = $mxhosts[array_search(min($mxweight), $mxhosts)];
} else {
// If MX records not found, get the A DNS records for the host
if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$record_a = dns_get_record($domain, DNS_A);
// else get the AAAA IPv6 address record
} elseif (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$record_a = dns_get_record($domain, DNS_AAAA);
}
if (!empty($record_a)) {
$mx_ip = $record_a[0]['ip'];
} else {
// Exit the program if no MX records are found for the domain host
$result = 'Invalid Email MX ';
$details .= 'No suitable MX records found.';
return ((true == $getdetails) ? array($result, $details) : $result);
}
}
// Open a socket connection with the hostname, smtp port 25
$connect = #fsockopen($mx_ip, 25);
if ($connect) {
// Initiate the Mail Sending SMTP transaction
if (preg_match('/^220/i', $out = fgets($connect, 1024))) {
// Send the HELO command to the SMTP server
fputs($connect, "HELO $mx_ip\r\n");
$out = fgets($connect, 1024);
$details .= $out."\n";
// Send an SMTP Mail command from the sender's email address
fputs($connect, "MAIL FROM: <$fromemail>\r\n");
$from = fgets($connect, 1024);
$details .= $from."\n";
// Send the SCPT command with the recepient's email address
fputs($connect, "RCPT TO: <$toemail>\r\n");
$to = fgets($connect, 1024);
$details .= $to."\n";
// Close the socket connection with QUIT command to the SMTP server
fputs($connect, 'QUIT');
fclose($connect);
// The expected response is 250 if the email is valid
if (!preg_match('/^250/i', $from) || !preg_match('/^250/i', $to)) {
$result = 'Invalid Email Address';
} else {
$result = 'Valid Email Address';
}
}else{echo 'layht';}
} else {
$result = 'Invalid Email Address';
$details .= 'Could not connect to server';
}
if ($getdetails) {
return array($result, $details);
} else {
return $result;
}
}
The only way to know for certain if an email address exists is to send mail to it and get a response from the user.
In the past, it was possible to query the remote server using SMTP and ask it if the address was valid; however because spammers used this to harvest addresses, it no longer works.
In the past it was also possible to send mail to a server and if it wasn't rejected, assume that it was a valid address, however this is no longer reliable either.

Send a notification after uploading multiple files with Dropzone JS

I have been working on a project where user uploads image on other users profile page. I have been successful in uploading and sending emails also but it sends multiple emails if there are multiple files in a single upload.
$sendmail = true;
if(!empty($_FILES)){
$team_id = $_GET['param1'];
$user_id = $_GET['param2'];
$id = $_GET['param3'];
$sql = "SELECT * FROM team where team_id = '$team_id'" ;
$sql_result = mysqli_query($conn,$sql) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysqli_fetch_assoc($sql_result)){
$abcd = $row;
}
$targetDir = "../user_images/";
$fileName = $_FILES['file']['name'];
$fileNamex = substr($fileName, -3);
$fileName = rand(1000,100000).md5($fileName).'_'.$fileName;
$targetFile = $targetDir.$fileName;
if( move_uploaded_file( $_FILES['file']['tmp_name'] , $targetFile ) ){
mysqli_query($conn, "INSERT INTO gallery (team_id , user_id , id , userPic, token) VALUES('$team_id','$user_id','$id','$fileName','0')") or die(mysqli_error($conn));
if($sendmail === true){
$htmlContent = file_get_contents ("../email_template_header.php");
$registerURL = $siteURL.'/register/';
if($abcd['claimed'] == '1'){
$htmlContent .= "New image(s) has been uploaded to your business. Please <a href='$registerURL'>login</a> and approve them";
}
else{
$htmlContent .= "New image(s) has been uploaded to your business. Please <a href='$registerURL'>register</a> to claim your business";
}
$htmlContent .= file_get_contents ("../email_template_footer.php");
$to = $abcd['b_email'];
require '../PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->setFrom('mail#abc.com', 'ABC');
$mail->addAddress($to);
$mail->isHTML(true);
$mail->Subject = 'New Image Uploaded';
$mail->Body = $htmlContent;
$mail->send();
$mail->clearAddresses();
$sendmail = false;
}
}
}
This doesn't work as the complete code is running for every file, there is also no loop in the default examples on http://www.dropzonejs.com/ so that I could've checked with help of a index flag whether a file has been uploaded or not. Any help is appreciated.
Thanks
Dropzone.options.filedrop = {
init: function () {
this.on("complete", function (file) {
if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
doSomething();
}
});
}
};

redirecting to thank you page after sending a message via contact form - php

I am having problem with contact form which is working fine and displays a message "sent ok" after sending it. BUT when I changed code so it will display "a thank you page" after sending a message instead of simple "sent ok" page goes blank and message never arrives at my email box. Why that can be? the only code that was changes was:
// #SEND MAIL
if($m->Send()) {
die('_sent_ok_');
} else {
die($m->ErrorInfo);
}
na
// #SEND MAIL
if($m->Send()) {
header ("Location: http://xyz.ie/thankyouurl.php");
} else {
header ("Location: http://xyz.ie/errorurl.php");
}
exit();
Here is current version of code.
<?php
#ini_set('display_errors', 1);
#ini_set('track_errors', 0);
#date_default_timezone_set('Europe/Bucharest'); // Used only to avoid annoying warnings.
if($_REQUEST['action'] = 'email_send') {
$array['name'] = isset($_REQUEST['name']) ? strip_tags(trim($_REQUEST['name'])) : '';
$array['email'] = isset($_REQUEST['email']) ? ckmail($_REQUEST['email']) : '';
$array['subject'] = isset($_REQUEST['subject']) ? strip_tags(trim($_REQUEST['subject'])) : '-';
$array['message'] = isset($_REQUEST['message']) ? (trim(strip_tags($_REQUEST['message'], '<b><a><strong>'))) : '';
// Visitor IP:
$ip = ip();
// DATE
$date = date('l, d F Y , H:i:s');
// BEGIN
require('config.inc.php');
require('phpmailer/5.1/class.phpmailer.php');
$m = new PHPMailer();
$m->IsSMTP();
$m->SMTPDebug = false; // enables SMTP debug information (for testing) [default: 2]
$m->SMTPAuth = true; // enable SMTP authentication
$m->Host = $config['smtp_host']; // sets the SMTP server
$m->Port = $config['smtp_port']; // set the SMTP port for the GMAIL server
$m->Username = $config['smtp_user']; // SMTP account username
$m->Password = $config['smtp_pass']; // SMTP account password
$m->SingleTo = true;
$m->CharSet = "UTF-8";
$m->Subject = ($array['subject'] == '-') ? $config['subject'] : $array['subject'];
$m->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$m->AddAddress($config['send_to'], 'Contact Form');
$m->AddReplyTo($array['email'], $array['name']);
$m->SetFrom($config['smtp_user'], 'Contact Form');
$m->MsgHTML("
<b>Date:</b> {$date} <br>
<b>Name:</b> {$array['name']}<br>
<b>Email:</b> {$array['email']}<br>
<b>Subject:</b> {$array['subject']}<br>
<b>Message:</b> {$array['message']}<br>
---------------------------------------------------<br>
IP: {$ip}
");
if($config['smtp_ssl'] === true)
$m->SMTPSecure = 'ssl'; // sets the prefix to the server
// #SEND MAIL
if($m->Send()) {
header ("Location: http://xyz.ie/thankyouurl.php");
} else {
header ("Location: http://xyz.ie/errorurl.php");
}
exit();
function ip() {
if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); }
elseif (getenv('HTTP_X_FORWARDED_FOR')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); }
elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); }
elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); }
elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); }
else { $ip = $_SERVER['REMOTE_ADDR']; }
return $ip;
}?>
Your problem could be because you are missing your close "}" of your first "if".
if($_REQUEST['action'] = 'email_send') {
I think should be before:
function ip() {
And I think you don't need the exit line.
It turned out that ckmail below were coasing a problem. So I changed it on strip_tags. And It worked out.
$array['email'] = isset($_REQUEST['email']) ? ckmail($_REQUEST['email']) : '';

PHPMailer: Attach a file located on server

I'm trying to send an (up to 4) attachments using PHPMailer. The files are on my server.
This is how my script works:
User sees a job or resume and contacts them by using the contact form. The contact form stores the data along with the attachments on the server. We will then review the message filtering out spams/scams. If the message is genuine then we will send the message through.
I have the information stored on the server and Im able to send that information via email however the attachments are not sending.
Here is the code used to send the mail:
if ($_GET['status'] == 'approve') {
$id = $_GET['id'];
dbconnect($dbuser, $dbpass, $db);
$sql = "SELECT * FROM $db.mail WHERE id = $id";
$results=mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($results, MYSQL_ASSOC)) {
$id = $row["id"];
$to = $row["to"];
$from = $row["from"];
$subject = $row["subject"];
$message = $row["message"];
$file1 = $row["file1"];
$file2 = $row["file2"];
$file3 = $row["file3"];
$file4 = $row["file4"];
$result = mysql_query("UPDATE mail SET `status` = '1', `approvedtime` = NOW() WHERE id = " . $_GET['id'])
or die(mysql_error());
header("Location: mail.php");
// PHPMailer - Start
$email = new PHPMailer();
$email->setLanguage('ja');
$email->From = $from;
$email->FromName = 'MySite.com';
$email->Subject = $subject;
$email->Body = $message;
$email->addAddress($to);
if (!empty($file1)) {
$file1 = 'upload/'.$file1;
$email->AddAttachment($file1);
}
if (!empty($file2)) {
$file2 = 'upload/'.$file2;
$email->AddAttachment($file2);
}
if (!empty($file3)) {
$file3 = 'upload/'.$file3;
$email->AddAttachment($file3);
}
if (!empty($file4)) {
$file4 = 'upload/'.$file4;
$email->AddAttachment($file4);
}
return $email->Send();
// PHPMailer - Start
}
} elseif ($_GET['status'] == 'reject') {
dbconnect($dbuser, $dbpass, $db);
$result = mysql_query("UPDATE mail SET `status` = '2', `approvedtime` = NOW() WHERE id = " . $_GET['id'])
or die(mysql_error());
header("Location: mail.php");
}
Why are the attachments not sending? Am I missing a step in my code or have something incorrect?
Thanks for the help!

what to do with this error?

I have had my script running on a localhost WampServer 1st which where it worked and then exported it to my online live domain.
After some adjustments i got the script partically working again but i am still getting below error
Call to undefined function filter_var()
The purpose of this script is when an user wants to registrate it will validate the email address and add the user to the database and send an validation link to the users emailaddress.
Here is the script:
<?PHP
error_reporting(E_ALL);
ini_set('display_errors', 1);
// connection towards database with a include function
include ('connection.php');
if (isset($_REQUEST['send']))
{
if(isset($_REQUEST['NAAM']))
{
$naam = $_REQUEST['NAAM'];
}
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field = filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}else
{
return FALSE;
}
}
//if "email" is filled out, proceed
if (isset($_REQUEST['mail']))
{
//check if the email address is invalid
$mailCheck = spamcheck($_REQUEST['mail']);
if ($mailCheck == TRUE)
{
$email = $_REQUEST['mail'];
}else
{
$mailCheck = FALSE;
echo "Invalid input email";
}
}
if(isset($_REQUEST['question']))
{
$quest = $_REQUEST['question'];
// checks if the filled in Question is de same as the answer novice or Novice
if ($quest =="novice" or $quest =="Novice")
{
$questCheck = TRUE;
}else
{
$questCheck = FALSE;
echo "Your answer to the question was incorrect!<br>";
}
}
if(isset($_REQUEST['wachtwoord'] , $_REQUEST['c_wachtwoord']))
{
$WW = $_REQUEST['wachtwoord'];
$c_WW = $_REQUEST['c_wachtwoord'];
// checks if the filled in password is de same as the confirmation password
if ($WW == $c_WW)
{
$pwCheck = TRUE;
}else
{
$pwCheck = FALSE;
echo "Your filled in passwords are not the same try again!<BR>";
}
}
// checks if both password confirmation and question are TRUE continue else retrieve fault
if ($pwCheck && $questCheck && $mailCheck == TRUE)
{
$hash = md5( rand(0,1000) );
// insert all filled in values into the database
$opdracht1 = "INSERT INTO users (ID , name , password , mail , staffLevel , hash , active) VALUES ('','$naam','$WW','$email','0','$hash','0')";
// run query
if (mysql_query ($opdracht1))
{
header( "refresh:5;url=http://www.debeerislos.nl/inlog_user.php" );
echo "Your account has succesfully been created! Please check your email to validate your account!<BR>";
$to = $email; //Send email to our user
$subject = 'Signup | Verification'; //// Give the email a subject
$message = '
Thanks for signing up!
Your account has been created!
You can login with the following credentials:
------------------------
Username: '.$naam.'
Password: '.$WW.'
------------------------
After you have activated your account you will have the rights so you can fully use it.
Please click this link to activate your account:
http://www.debeerislos.nl/verify_user.php?email='.$email.'&hash='.$hash.'&name='.$naam.'
'; // Our message above including the link
$headers = 'From:info#debeerislos.nl' . "\r\n"; // Set from headers
mail($to, $subject, $message, $headers); // Send the email
}else
{
echo "Woops something went wrong please contact the Administrator of the website or fill in the form again!<br> <A href='http://www.debeerislos.nl/form_register_user.html'>CLICK HERE!</A> to fill in the forum again";
}
}elseif ($pwCheck && $questCheck == FALSE)
{
echo "you filled both the password confirmation and the answer to the question incorrect!<br>";
}
}else
{
echo "Either you haven't send anything! or you haven't filled in the form<br>";
}
?>
In advance thank you.
Kind Regards,
StaleDevil
where have you defined filter_var() function? It is not defined in your given code. If you are defining it in the same page then how are you defining? provide example. otherwise if you are definfing it in another page then include the page.

Categories