PHP Issue / Please HELP

SashenG

Member
Joined
Feb 16, 2017
Messages
28
Reaction score
1
Hi All,

Having an issue where my PHP redirects to the actual form when i click on submit, (just a white page with errors), but i did get the email like 6 hours later, Can anyone assist ?


<?php
// Variables
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$subject = "A Query from Basikwa Website";
$message = trim($_POST['message']);


function is_email_valid($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}


if( isset($name) && isset($email) && isset($phone) && isset($message) && is_email_valid($email) ) {


$pattern = "/(content-type|bcc:|cc:|to:)/i";
if( preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $message) ) {
exit;
}

// Email Sent To
$to = "[email protected]";
$sub = $subject; // You can define email subject
// HTML Elements for Email Body
$body = <<<EOD
<strong>Name:</strong> $name <br>
<strong>Email:</strong> <a href="mailto:$email?subject=feedback" "email me">$email</a> <br> <br>
<strong>Message:</strong> $message <br>
EOD;
/

$headers = "From: $name <$email>\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header('Location: http://www.basikwa.com/contact.html');
header("Location:http://www.basikwa.com/contact.html");


// PHP email sender
mail($to, $sub, $body, $headers);

echo "Your Email Was Sent Successfully"
}


?>
 
Last edited:
header("Location:http://www.basikwa.com/contact.html"); will start the redirect process, but the script will still process everything below while the browser initiates the location switch. If you don't want any redirect following a header() call,run die() directly after it. Also, run your email before initiating the header() if the mail is intended to go out. Finally, base your error message on the status returned by the mail() call.
 
Thank You,

so if i had to edit the script,
I would want to redirect after the email sends ...
Where do i edit ?
 
Thank You,

Where do i edit ?

PHP:
$headers = "From: $name <$email>\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header('Location: http://www.basikwa.com/contact.html');
header("Location:http://www.basikwa.com/contact.html");
This causes the page to redirect


// PHP email sender
PHP:
mail($to, $sub, $body, $headers);
This will send the email.

PHP:
echo "Your Email Was Sent Successfully"
 
slightly off-topic but:
https://foundation.zurb.com/emails.html

I'd recommend Foundation's email builder.

Also on myBB, use (like SBSP is)
PHP:
 [/ PHP] (remove the space) for syntax highlighting/saving formatting, it will also remove the issue of the emoji.
I'm also curious as to why you have $sub = $subject?

For e-mails I like doing:
[PHP]
$myemail = [
    "to" => "[email protected]",
    "subject" => $subject,
    "body" => <<<EOD
    <strong>Name:</strong> $name <br>
            <strong>Email:</strong> <a href="mailto:$email?subject=feedback" "email me">$email</a> <br> <br>
            <strong>Message:</strong> $message <br>
EOD
    ,
    "headers" => 
        'From: $name <$email>'  . "\r\n" .
        'MIME-Version: 1.0'  . "\r\n" .
        'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
        //'Location: http://www.basikwa.com/contact.html' . "\r\n"
];

mail($myemail["to"], $myemail["subject"], $myemail["body"], $myemail["headers"]);
Don't really use EOD, most of the time I copy-paste from the foundation template I built.
Can't guarantee above works, didn't test it, but same logic as yours (most of it copy pasted and just formatted differently).

I would be careful and run all the variables through htmlspecialchars though.
 
Last edited:
I would recommend you switch to a mail library instead.
Take a look at phpmailer, it's very easy to work with, example:
PHP:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
 
I would recommend you switch to a mail library instead.
Take a look at phpmailer, it's very easy to work with, example:
PHP:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

Still no Codeigniter?
 
Top
Sign up to the MyBroadband newsletter
X