提交表单时发送电子邮件

时间:2015-05-16 20:40:26

标签: php html mysqli

我试图这样做,以便在提交表单时,它会发送一封电子邮件。数据库连接/提交工作正常,因此数据库或表单方面没有任何帮助。

这是我的代码:

    <?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;
?>

它会打印发送的电子邮件,但我的邮件中没有任何内容。

感谢。

3 个答案:

答案 0 :(得分:1)

$to      = '$email';

必须

$to      = "$email";

或 - 更好:

$to      = $email;

答案 1 :(得分:0)

您应该阅读此Curly braces in string in PHP

在您的代码中&#34;已发送电子邮件&#34;将永远展示。

尝试:

-r

答案 2 :(得分:0)

其中一个问题是

if(prepare)

prepare被视为constant而非预期变量$prepare

执行if($prepare){...}

此外,您的变量需要使用双引号。

$to = '$email';

变量不会用单引号插值

$to = "$email";

$message = '...';应该是$message = "...";

您还应该用

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

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

用于SELECT连接。

检查可能的db错误。

还要确保您的表单使用POST方法,并且所有元素都具有正确的名称属性。

  • 如果省略POST方法,表单将无提示失败。

如果省略,它也默认为GET。我不知道表格是什么样的,所以你需要调查一下。

修改(添加PHPmailer信息)

看到您可能无法使用mail(),您可以尝试使用PHPmailer和Gmail帐户(如果有的话)。

以下是从https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

中提取的示例

访问他们的主页: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!";
}