-2

I have coded a form in which I am using if statement so when someone click on send button this if statement will redirects to "thanks.php" page from "career-a.php" page. In thanks.php page I have simply added bootstrap column with thanks message. But this code is not directing to other page. Can you tell me how should I solve this issue? Code on top of thanks.php:

<?php
require "header.php";
session_start();
$message=$_SESSION['msg'];
echo $message;
?>

Code for career-a.php (I have added php and html on same page so SUBMIT button is on this same page).

<?php
$page = 'careers';
require "header.php";
?>

<?php
$statusMsg='';
if(isset($_FILES["file"]["name"])){
   $email = $_POST['email'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $phone = $_POST['phone'];
    $option = $_POST['option'];
    $message = $_POST['message'];
    
    if(!empty($_POST['check_list'])) {
    $checks = array();
    foreach($_POST['check_list'] as $check) {
        $checks[] = $check;
    }
    $check = implode('</br>&bull;', $checks);
}
$fromemail =  $email;
$subject="Tell Us About Yourself to MyGeo";
$email_message = '<h2>User Information</h2>
                    <p><b>First Name:</b> '.$fname.'</p>
                    <p><b>Last Name:</b> '.$lname.'</p>
                    <p><b>Email:</b> '.$email.'</p>
                    <p><b>Phone:</b> '.$phone.'</p>
                    <p><b>Interest :</br></b> &bull;'.$check .'</p>
                    <p><b>Field :</b> '.$option.'</p>
                    <p><b>Introduce Yourself :</b> '.$message.'</p>';
$email_message.="Please find the attachment";
$semi_rand = md5(uniqid(time()));
$headers = "From: ".$fromemail;
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

if($_FILES["file"]["name"]!= ""){  
    $strFilesName = $_FILES["file"]["name"];  
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));  
    
    
    $email_message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_message .= "\n\n";


    $email_message .= "--{$mime_boundary}\n" .
    "Content-Type: application/octet-stream;\n" .
    " name=\"{$strFilesName}\"\n" .
    //"Content-Disposition: attachment;\n" .
    //" filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $strContent  .= "\n\n" .
    "--{$mime_boundary}--\n";
}
$toemail="mail@mail.com"; 

if(mail($toemail, $subject, $email_message, $headers)){
    session_start();
    $_SESSION['msg'] = $statusMsg;
   header("Location: thanks.php?mailsend");
}else{
   
}
}
   ?>
RAMA
  • 33
  • 6
  • We need to see more code, specifically what leads up to `mail()`. I'm assuming it's failing. – GrumpyCrouton Dec 01 '20 at 13:00
  • I have added code – RAMA Dec 01 '20 at 13:10
  • Go and enable proper PHP error reporting, I am pretty sure that would have something to tell you here. – CBroe Dec 01 '20 at 13:14
  • When I enter this code it displays on top , but I want to display it on other page : `if(mail($toemail, $subject, $email_message, $headers)){ $statusMsg= "Thank you for showing your interest"; }else{ $statusMsg= "Not sent"; } }` – RAMA Dec 01 '20 at 13:17

1 Answers1

1

You have bunch of syntax errors in your code and that is why it does not work. It seems like you used different sources of information on how you send mails via PHP.

Personally I recommend to use library like PHPMailer, but if you want to keep your dependencies at minimum you can adapt your code based on this answer

So it will look something like this:

<?php

session_start();

// Best not to use $_POST directly
$input = filter_input_array(\INPUT_POST, [
    'email' => FILTER_VALIDATE_EMAIL,
    'fname' => FILTER_DEFAULT,
    'lname' => FILTER_DEFAULT,
    'phone' => FILTER_DEFAULT,
    'option' => FILTER_DEFAULT,
    'message' => FILTER_DEFAULT,
    'check_list' => [
        'filter' => FILTER_DEFAULT,
        'flags'  => FILTER_REQUIRE_ARRAY,
    ],
]);

$to="mail@mail.com";
$subject="Tell Us About Yourself to MyGeo";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (RFC)
$eol = "\r\n";

$attachment = null;
$attachmentName = null;
if (isset($_FILES["file"]["tmp_name"])) {
    $attachmentName = $_FILES["file"]["name"];  
    $attachment = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
}

// main header (multipart mandatory)
$headers = "From: {$input['fname']} <{$input['email']}>{$eol}";
$headers .= "MIME-Version: 1.0{$eol}";
$headers .= "Content-Type: multipart/mixed; boundary=\"mixed-{$separator}\"";

ob_start();
?>
<h2>User Information</h2>
<p><b>First Name:</b> <?php echo $input['fname']; ?></p>
<p><b>Last Name:</b> <?php echo $input['lname']; ?></p>
<p><b>Email:</b> <?php echo $input['email']; ?></p>
<p><b>Phone:</b> <?php echo $input['phone']; ?></p>
<p><b>Interest:</br></b> &bull; <?php echo implode('</br>&bull;', $input['check_list']); ?></p>
<p><b>Field:</b> <?php echo $input['option']; ?></p>
<p><b>Introduce Yourself:</b> <?php echo $input['message']; ?></p>
<?php
$html = ob_get_clean();

ob_start();
?>
--mixed-<?php echo $separator; ?>  
Content-Type: multipart/alternative; boundary="alternative-<?php echo $separator; ?>" 

--alternative-<?php echo $separator; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<?php echo strip_tags($html); ?>

--alternative-<?php echo $separator; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<?php echo $html; ?>

--alternative-<?php echo $separator; ?>-- 
<?php if ($attachment) : ?>
--mixed-<?php echo $separator; ?>  
Content-Type: application/octet-stream; name="<?php echo $attachmentName; ?>"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
<?php endif; ?>
--mixed-<?php echo $separator; ?>-- 
<?php
$message = ob_get_clean();


if (mail($to, $subject, $message, $headers)) {
    $_SESSION['msg'] = 'Thank you for showing your interest';
    $location = 'thanks.php?mailsend';
} else {
    $_SESSION['msg'] = 'Mail function failed!';
    $location = 'thanks.php?mailfailed';
}
header("Location: {$location}");

Please note that this code is not tested! Its purpose is to give you directions on how to continue