0

im trying to make it so that when a form is submitted, it would send an email. The database connecting/submitting is working so it's nothing from the database or form side hopefully.

Here is my code:

    <?php
$mysql_host     = "localhost";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$mysql_database2 = "";

$sub = $_POST['subject'];
$mysqli2  = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database2) or die(mysqli_error());
$head = $mysqli2->query("SELECT head FROM class WHERE subject = '$sub'")->fetch_object()->head; 

$status = 1;
$mysqli  = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysqli_error());
$prepare = $mysqli->prepare("INSERT INTO `Overrides`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`subject`,`section`,`semester`,`professor`,`status`,`dean`,`head`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$prepare->bind_param("ssssssssssssss", $_POST['name'], $_POST['mname'], $_POST['fname'], $_POST['sid'], $_POST['email'], $_POST['phone'], $_POST['Scolarship'], $_POST['subject'], $_POST['section'], $_POST['semester'], $_POST['professor'], $status, $_POST['dean'], $head);
$prepare->execute();

$name = $_POST['name'];
$mname= $_POST['mname'];
$fname = $_POST['fname'];
$email = $_POST['email'];
$semester = $_POST['semester'];
$sid = $_POST['sid'];
$subject = $_POST['subject'];
$section = $_POST['section'];
$professor = $_POST['professor'];

if(prepare)
{

$to      = '$email'; //can receive notification

$subject = 'Override Request';
$message = 'Dear $name<br /><br /> 
Your Following override request has been submitted.<br /><br />
Name: $name . $mname . $fname
Student ID : $sid
Semester : $semester
Subject : $subject
Section : $section
Professor : $professor<br /><br />
Please note that the request is passed to different faculty members in order to be revised. You will be notified on each update';


$headers = 'From: system@auke.edu.kw' . "\r\n" .
    'Reply-To: webmaster@ourcompany.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

echo 'Email Sent.';
}

print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
?>

It prints email sent but i get nothing on my mail.

Thanks.

Kyle Josh
  • 69
  • 8

3 Answers3

1
$to      = '$email';

Have to be

$to      = "$email";

or - better:

$to      = $email;
Richard
  • 2,840
  • 3
  • 25
  • 37
0

You should read this Curly braces in string in PHP

In your code "Email sent" will be allways display.

Try:

if ( mail($to, $subject, $message, $headers) )
   echo "Sent";
else
   echo "Error";
Community
  • 1
  • 1
Marcin Jaworski
  • 330
  • 2
  • 9
0

One of the problems is here

if(prepare)

prepare is treated as a constant rather than the intended variable $prepare.

Do if($prepare){...}

Also your variables need to be in double quotes.

$to = '$email';

variables do not get interpolated in single quotes

$to = "$email";

same for $message = '...'; that should be $message = "...";

You should also be doing and replacing $prepare->execute(); with

if(!$prepare->execute()){ 
  trigger_error("there was an error....".$mysqli->error, E_USER_WARNING);
}

or

if(!$prepare->execute()){ 
  trigger_error("there was an error....".$mysqli2->error, E_USER_WARNING);
}

for your SELECT connection.

to check for possible db errors.

Also make sure your form uses a POST method and that all elements bear the proper name attributes.

  • Form will fail silently if the POST method is omitted.

It also defaults to GET if omitted. I don't know what the form looks like, so you'll need to look into that.


Edit: (adding PHPmailer information)

Seeing as you may not have access to using mail(), you could try using PHPmailer and a Gmail account if you have one.

Here is an example pulled from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

Visit also their main page: http://phpmailer.worxware.com/index.php

<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "yourpassword";

//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');

//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • fixed, still won't send. – Kyle Josh May 16 '15 at 21:53
  • i actually removed the prepare so it will submit it anyways. but it still won't send and gives me an error. – Kyle Josh May 16 '15 at 21:54
  • form is post. i double checked. – Kyle Josh May 16 '15 at 21:56
  • @KyleJosh firstly, do you have access to using `mail()`? if so, run a test script without all that and as a basic contact form, using hard coded values. If you get mail, then you'll know your query failed somewhere. Try and do a `var_dump();` and see what echos out. Also, check your spam box. – Funk Forty Niner May 16 '15 at 22:31
  • @KyleJosh Post your HTML form also, just so I can be 100% sure. Use `isset()` or `!empty()` against all your POST arrays. Also try your query without the INSERT and just the SELECT. Something is failing you somewhere. – Funk Forty Niner May 16 '15 at 22:32
  • @KyleJosh I made an additional edit regarding PHPmailer. It could be an option for you to try, along with a Gmail account, if you have one. Reload it and look near the bottom under **Edit** (adding PHPmailer information). – Funk Forty Niner May 16 '15 at 23:12