PHPMailer可以在测试页面发送电子邮件,但不能在其他页面中发送

时间:2015-08-19 06:35:36

标签: php email

我尝试使用PHPMailer,当我发送带附件的简单邮件传输(测试页面)时,它已成功发送到我的电子邮件。 但是,如果我将代码放在我的项目中,则无法传递消息(真实页面)。 我为我的测试页面和真实页面使用相同的smtp,用户名和密码。

制作文件并上传到服务器完美无缺,但无法发送电子邮件。 我尝试调试,并显示消息“已发送消息”,因此它证明$ mail-> send()为真。

我经历了痛苦的日子,找出了虫子的位置,但仍然无法找到它。

任何人都可以追踪我的错误,我会非常感激。

这是我的代码(真实页面):     

<?php 
session_save_path(realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/cgi-bin/tmp'));
session_start(); 

?>
<!DOCTYPE html>
<html lang="en-US">
<?php include("head.php"); ?>
<body>
<div id="container">
<?php include("header.php");?>
<div id="wrapper">
<div id="content">

<?php                       
$total=0;
$jawab=$_SESSION["jawab"];
$jawaban=$_POST["jawaban"];
$tgl=$_POST["tgl"];
$level=$_POST["level"];
$format=$_POST["format"];
$message=$_POST["message"];
$fn=$_POST["fn"];
$ln=$_POST["ln"];
$gender=$_POST["gender"];
$email=$_POST["email"];
$conemail=$_POST["conemail"];
$country=$_POST["country"];
$msgbody ="Due date : ".$tgl."\n";
$msgbody.="Level    : ".$level."\n";
$msgbody.="Format   : ".$format."\n";
$msgbody.="Message  : ".$message."\n";
$msgbody.="Name     : ".$fn." ".$ln."\n";
$msgbody.="Gender   : ".$gender."\n";
$msgbody.="Email    : ".$email."\n";
$msgbody.="Country  : ".$country."\n";

if (!($jawab==$jawaban && isset($_POST['agree']) && filter_var($email, FILTER_VALIDATE_EMAIL) && filter_var($conemail, FILTER_VALIDATE_EMAIL) && $email==$conemail)) {
echo("<p>Email does not match or invalid email or you have not checked the terms and conditions agreement. or your answer to our captcha is incorrect.</p>");
}
else{
$namafile="fileyangdiupload/".date("Ymdhisa").$email.".txt";                        

//make text file                            
$myfile = fopen($namafile, "w") or die("Unable to open file!");
fwrite($myfile,"Due date: ".$tgl."\r\n");
fwrite($myfile,"Level   : ".$level."\r\n");
fwrite($myfile,"Format  : ".$format."\r\n");
fwrite($myfile,"Message : ".$message."\r\n");
fwrite($myfile,"Name    : ".$fn." ".$ln."\r\n");
fwrite($myfile,"Gender  : ".$gender."\r\n");
fwrite($myfile,"Email   : ".$email."\r\n");
fwrite($myfile,"Country : ".$country."\r\n");
fclose($myfile);

//uploading file
$target_dir = "fileyangdiupload/";
for($i=0;$i<10;$i++){
    $target_file[$i] = $target_dir . date("Ymdhi"). "-".$i."-".$email."-".basename($_FILES["ul".$i]["name"]);
    //$target_file[$i]=$target_dir . date("Ymdhisa"). "-".$i."-".$email."-".basename($_FILES["ul"]["name"][$i]);
    $uploadOk[$i] = 1;
    $extFileType = pathinfo($target_file[$i],PATHINFO_EXTENSION);
    if(is_uploaded_file($_FILES['ul'.$i]['tmp_name'])){
        // Check file size
        if ($_FILES["ul".$i]["size"] >2000000) {
            echo "<p>Sorry, your number ".$i." file is too large.</p>";
            $uploadOk[$i] = 0;
        }
        // Allow certain file formats
        if($extFileType != "doc" && $extFileType != "docx" && $extFileType != "pdf"
        && $extFileType != "txt" && $extFileType != "rtf" && $extFileType != "jpg" 
        && $extFileType != "jpeg" && $extFileType != "png" && $extFileType != "gif"
        && $extFileType != "zip" && $extFileType != "rar" && $extFileType != "7z"  ) {
            echo "<p>Sorry, your number ".$i." file are not allowed.</p>";
            $uploadOk[$i] = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk[$i] == 0) {
            echo "<p>Sorry, your number ".$i." file was not uploaded.</p>";
        // if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["ul".$i]["tmp_name"], $target_file[$i])) {

                echo "<p>The file ". basename( $_FILES["ul".$i]["name"]). " has been uploaded.</p>";
                if($uploadOk[$i]==1){
                    $uploadedfile[$total]=$target_file[$i];
                }
                $total=$total+1;
            } else {
                echo "<p>Sorry, there was an error uploading your number ".$i." file.</p>";
            }
        }
    }
}
if ($total>0){
    echo('<h2 style="margin-left:10px">Thank you for submitting your problem<br>
          We will review and notice you for further process</h2>
          <p>Click <a href="home.php">here</a> to go back to main page</p>');
}
else{
    echo('<p>There is no uploaded files</p>');
}

//send email
require 'requiredir/PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$subject = "Project: " . date("Ymdhisa");
$to = "zxxx@gmail.com";
$un = 'adm@mywebsite.com';
$pass = 'xxx';

$mail->IsSMTP(true);                                      // Set mailer to use SMTP
$mail->Host = 'smtp.mywebsite.com';
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = $un;                             // SMTP username
$mail->Password = $pass;                        // SMTP password
$mail->SMTPSecure = 'tls';                           // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
$mail->From = $email;
$mail->FromName = $fn.' '.$ln;
$mail->addAddress($to,'DEH');     // Add a recipient

for($i=0;$i<$total;$i++){
    $mail->addAttachment($uploadedfile[$i]);    
}

$mail->Subject = $subject;
$mail->Body    = $msgbody;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}                           
}
?>
</div>
</div>
<?php include("nav.php"); 
include("extra.php"); ?>
<div id="footer">
<?php include("footer.php");?>
</div>
</div>
</body>
</html>

这是我的代码(测试页面):     

?>
<!DOCTYPE html>

<?php
require 'requiredir/PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$email = 'xxx@xxx.com';
$fn = 'First';
$ln = 'Last';
$subject = "project: " . date("Ymdhisa");
$to = "zxxx@gmail.com";
$un = 'adm@mywebsite.com';
$pass='xxx';

$mail->IsSMTP(true);                                      // Set mailer to use SMTP
$mail->Host = 'smtp.mywebsite.com';
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = $un;                             // SMTP username
$mail->Password = $pass;                        // SMTP password
$mail->SMTPSecure = 'tls';                           // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
$mail->From = $email;
$mail->FromName = $fn.' '.$ln;
$mail->addAddress($to,'DEH');     // Add a recipient
$uploadedfile[0]='fileyangdiupload/xxx.pdf';

for($i=0;$i<2;$i++){
    $mail->addAttachment($uploadedfile[$i]);    
}

$mail->Subject = $subject;
$mail->Body    = $fn.'  ~  '.$ln.' test body message';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

谢谢你们:)

0 个答案:

没有答案