这已更新。
在我的网站上,我有一个表格供人们将数据提交到mysql数据库。 但我想知道当有人提交新内容时我如何收到电子邮件。
我一直在努力寻找解决方案,但我需要一些帮助。
我有一个不同的联系表单,我使用phpmailer所以我知道它有用。
它会将数据发送到mysql数据库,但它不会向我发送电子邮件。
HTML:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="t3.js"></script>
<form action='t2.php' method='post' id="form1">
<div id = "container">
<h3>Basic Information</h3><br>
<div id="main">
<p><small>Name:</small><input type='text' id="name" name='name' /><br><br><br>
<p><small>Email:</small><input type='text' id="email" name='email' /><br><br><br>
<p><small>Comments:</small><textarea id='comments' name='comments' rows="5" cols="40" placeholder="Comments"></textarea> <br><br><br>
<input type='hidden' name='action' value='create' />
<p><input type="submit" name="submit" id="submit" value="Email Us!" /></p>
<input type="reset" name='reset' value="Reset" class="reset-org"/>
<ul id="response" />
</div>
</form>
Javascript t3.js:
$(function() {
// These first three lines of code compensate for Javascript being turned on and off.
// It simply changes the submit input field from a type of "submit" to a type of "button".
var paraTag = $('input#submit').parent('p');
$(paraTag).children('input').remove();
$(paraTag).append('<input type="button" name="submit" id="submit" value="Email Us Now!" />');
$('#main input#submit').click(function() {
$('#main').append('<img src="img/ajax/contact/ajax-loader.gif" class="loaderIcon" alt="Loading..." />');
var name = $('input#name').val();
var email = $('input#email').val();
var comments = $('textarea#comments').val();
$.ajax({
url: 't2.php',
type: 'POST',
data: $('#form1').serialize(),
success: function(results) {
$('#main img.loaderIcon').fadeOut(1000);
$('ul#response').html(results);
}
});
});
})
Php:t2.php
<?php
$name =$_POST['name'];
$email =$_POST['email'];
$comments =$_POST['comments'];
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){ //the the user submitted the form
$query = "insert into testdb
set
name = '".$mysqli->real_escape_string($_POST['name'])."',
email = '".$mysqli->real_escape_string($_POST['email'])."',
comments = '".$mysqli->real_escape_string($_POST['comments'])."'";
if( $mysqli ->query($query) ) {
//if saving success
echo "User was created.";
require 'PHPMailer/PHPMailerAutoload.php'; //replace with path to PHPMailerAutoload.php
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = '****'; // Specify main and backup server
$mail->Port = ***; // set the SMTP port for the GMAIL server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '*****'; // SMTP username
$mail->Password = '*****!'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('****, 'Info'); // Add a recipient
$mail->AddReplyTo($email, $name);
$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->Send();
}
else{
//if unable to create new record
echo "Database Error: Unable to create record.";
}
//close database connection
$mysqli->close();
}
?>
答案 0 :(得分:0)
如果您安装了PHPMailer,则可以执行此操作(编辑以符合您的信息):
require 'PHPMailerAutoload.php'; //replace with path to PHPMailerAutoload.php
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
$query = "insert into organisation
set name = '".$mysqli->real_escape_string($_POST['name'])."',
$data =$_POST;
$name =$data['name'];
if( $mysqli ->query($query) ) {
//if saving success
echo "User was created.";
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'jswan'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh@example.net', 'Josh Adams'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $name + ' was Registered';
$mail->Body = '<b>' + $name + '</b> was registered. Congrats on the new recruit!';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
}else{
//if unable to create new record
echo "Database Error: Unable to create record.";
}
如果您阅读了readme
here.
答案 1 :(得分:0)
你说当你的脚本收到帖子时你试图发送一封电子邮件,你已经向我们展示了很多代码,它所做的一件事就是尝试发送电子邮件。正如Fred ii所说,你需要学习如何识别和修复的基本语法错误。您在此处向我们展示代码的代码无效。
从PHP发送邮件非常简单:
mail('user@example.com','this is the subject', 'this is the body of the email');
然而,配置工作MTA要困难得多 - 与PHP集成的方式取决于操作系统。这都是well documented in the manual。
当然,如果你想发送比简单的7位ascii文本更复杂的东西,那么使用像swiftmailer或phpmailer这样的工具会减少生活的痛苦 - 但它仍然需要与你的MTA集成工作 - 因此得到mail()先工作。