如果((($ _SERVER ["REQUEST_METHOD"] =="POST"),则使用php发送邮件 [英] sending mail in php if (($_SERVER["REQUEST_METHOD"] == "POST")
问题描述
我有一个php邮件脚本,sendmail.php
在使用简单的html脚本和if(isset($_POST['btn_send']))
命令运行它时可以工作.
I have a php mailing script, sendmail.php
that works when I run it with a simple html script and if(isset($_POST['btn_send']))
command.
但是我想将此脚本集成到我拥有的php表单脚本solliciteerform.php
中.
表单首先验证用户输入,然后在没有错误的情况下通过准备好的语句PDO将用户输入发送到我的mysql数据库.
But I want to integrate this script in a php form script, solliciteerform.php
that I have.
The form first validates the user input and when there're no errors sends the user input to my mysql db via a prepared statement PDO.
我正在使用的
if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== "" && $telErr== "" && $afileErr== "") {
$servername = "xxxxx.eu.mysql";
$username = "xxxxxx";
$password = "xxxxx";
try {
$conn = new PDO("mysql:host=$servername;dbname=xxxxxx", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/***
irrelevant code
***/
if(move_uploaded_file($file_loc,$folder.$final_file))
{
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO sollicitatie_form (geslacht, voornaam, familienaam, email, tel, afile, type)
VALUES (:geslacht, :voornaam, :familienaam, :email, :tel, :final_file, :file_type)");
$stmt->bindParam(':geslacht', $geslacht);
$stmt->bindParam(':voornaam', $voornaam);
$stmt->bindParam(':familienaam', $familienaam);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':tel',$tel);
$stmt->bindParam(':final_file', $final_file);
$stmt->bindParam(':file_type', $file_type);
$stmt->execute();
?>
<script>
alert('successfully uploaded');
window.location.href='thank.php';
</script>
<?php
exit();
}
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn->null;
}
在没有sendmail.php
的情况下进行测试时,此脚本有效.
This scripts works when tested without the sendmail.php
.
所以问题不在于sendmail.php
或我的solliciteerform.php
,因为它们都可以单独工作.
So the problem is not with the sendmail.php
or with my solliciteerform.php
as they both work separately.
我遇到的问题是我想在
if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== "" && $telErr== "" && $afileErr== "")
但是在将数据发送到我的数据库之前......
But before the data is sent to my database....
我尝试了两种解决方案,但都没有奏效....:
I've tried two solutions but neither have worked....:
解决方案1:
Solution 1:
在我的solliciteerform.php
顶部,添加include ('Send-HTML-mail-in-PHP/mailer/sendmail.php');
比起sendmail.php
我取代了if(isset($_POST['btn_send']))
具有:
if(($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== "" && $telErr== "" && $afileErr== ""){
结果是solliciteerform.php
可以正常运行,但我没有收到电子邮件.
The result is that solliciteerform.php
runs as it's supposed to do but I don't get an email.
解决方案2:
Solution 2:
在我的solliciteerform.php
顶部,添加了:
include ('Send-HTML-mail-in-PHP/mailer/class.phpmailer.php');
那么之前:
if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== "" && $telErr== "" && $afileErr== ""){
我添加了:
$mail = new PHPMailer(true);
以及
if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== "" && $telErr== "" && $afileErr== ""){
我已经粘贴了sendmail.php
中的所有代码.
I've paste all the code that's in sendmail.php
.
当我这样做时,我的solliciteerform.php
甚至都没有运行.获取错误页面.
When I do this than my solliciteerform.php
doesn't even run. Get an error page.
什么是我想要实现的最佳解决方案?
Whats the best solution for what I want to achieve?
推荐答案
WouterS,
在您的IF条件下,尝试一次删除一个条件...,看看是否可以解决该问题.即
In your IF condition, try removing one condition at a time... and see if that fixes it. i.e.
if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== "" && $telErr== ""){
然后
if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== ""){
然后
if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== ""){
等
最后,如果您已经在sendmail.php中创建了PHPMailer的实例,则无需重新创建它...因为您已经在solliciteerform.php中包含了sendmail.php文件
Lastly, if you have already created an instance of PHPMailer in sendmail.php, then you wouldn't need to re-create it... as you are already including sendmail.php file in your solliciteerform.php
所以我将删除以下内容:
So I would remove the following:
$mail = new PHPMailer(true);
希望这会有所帮助!
-Rush
这篇关于如果((($ _SERVER ["REQUEST_METHOD"] =="POST"),则使用php发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!