2016-05-11 102 views
0

我们有一个提交表格的网站,通过以下代码(通过下面的代码);为什么PHP Mailer不总是发送电子邮件?

  1. 发送一个测试的邮件,自己之前插入到MySQL只是让我知道PHP邮包的工作,它总是在我所有的测试确实

  2. 然后从表格中的所有数据插入到MYSQL(这也总是有效)

  3. 通过PHP邮件发送另一封电子邮件与他们提交的所有信息。我希望它以纯文本格式(这仅适用于大约80%的时间)。

发生了什么事是,有时人们提交和它的一切,但有时它只做步骤1和2。所以有时候,有些事情似乎在某处断裂。我觉得这可能是某些标点符号的问题,但不确定。就像在$复制字段中一样,人们可以输入任何他们想要的东西。不确定是否有某些标点可能会打破第二封电子邮件程序。

注意:每次有人提交时,都会将它们带到request-success.php页面。但它并不总是发送来自步骤3的电子邮件。

如果有人看到有改进的地方或者错误的东西,请分享。这让我疯狂。

<?php 
    session_start(); 
    include_once("config.php"); 
    include_once("includes/functions.php"); 
    require 'phpmailer/PHPMailerAutoload.php'; 
    //database configuration & connection (hiding for privacy purposes, but the database connections work fine so not relevant 

    if ($_POST['submit']) { 

     $type=$_POST['type']; 

     $category= substr($type, 0, strpos($type, ' -')); 
     $category= strtolower($category); 
     $category= ucfirst($category); 
     $need = substr($type, strpos($type, "-") + 1);  

     $subject="REQUEST for " . $type; 
     $fullname= $_SESSION['google_data']['name']; 
     $fromemail=$_SESSION['google_data']['email']; 

     $brands=$_POST['brand']; 

     $size=$_POST['size']; 
     if ($size == "") { 
      $size="n/a"; 
     } 
     $bleed=$_POST['bleed']; 
     if ($bleed =="no") { 
      $bleedsize="n/a"; 
     } else { 
      $bleedsize=$_POST['bleedsize']; 
     } 
     $filetype=$_POST['filetype']; 
     if ($filetype=="") { 
      $filetype="n/a"; 
     } 
     $footerurl=$_POST['footer-url']; 
     if ($footerurl=="") { 
      $footerurl="n/a"; 
     } 
     $footerphone=$_POST['footer-phone']; 
     if ($footerphone=="") { 
      $footerphone="n/a"; 
     } 
     $copy=mysqli_real_escape_string($con,$_POST['copy']); 
     $copyforemail=$_POST['copy']; 

     $approved=$_POST['approved']; 
     $seo=$_POST['seo']; 
     $proofread=$_POST['proofread']; 
     $info=mysqli_real_escape_string($con,$_POST['info']); 
     $infoforemail=$_POST['info']; 

     $priority=$_POST['priority']; 
     $requestdate= date('Y-m-d'); 
     $duedate = date('Y-m-d', strtotime(str_replace('-', '/', $_POST['duedate']))); 
     if ($duedate =="1969-12-31") { 
     $duedate="0000-00-00"; 
     } 
     $timinginfo=mysqli_real_escape_string($con,$_POST['timinginfo']); 
     $timinginfoforemail=$_POST['timinginfo']; 

     $communication=mysqli_real_escape_string($con,$_POST['communication']); 
     $communicationforemail=$_POST['communication']; 

    //TEST EMAIL BEFORE INSERTING 
    $mail = new PHPMailer; 
    //$mail->SMTPDebug = 3;        // Enable verbose debug output 
    /* 
    $mail->isSMTP();          // Set mailer to use SMTP 
    $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
    $mail->SMTPAuth = true;        // Enable SMTP authentication 
    $mail->Username = '';    // SMTP username 
    $mail->Password = '';       // SMTP password 
    $mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
    $mail->Port = 587;         // TCP port to connect to 
    */ 
    $mail->setFrom($fromemail, $fullname); 
    $mail->addAddress('[email protected]', 'my name');  // Add recipients 
    $mail->addReplyTo('[email protected]', 'my name'); 
    /*$mail->addCC('[email protected]'); 
    $mail->addBCC('[email protected]'); 


    $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 = "Request coming for Creative Team"; 
    $mail->Body = "Request coming"; 
    $mail->AltBody = "Request coming"; 

    if(!$mail->send()) { 
     echo 'Message could not be sent.'; 
     echo 'Mailer Error: ' . $mail->ErrorInfo; 
    } else { 
     //continue to insert 
    } 

    $sql = "INSERT INTO requests (firstname, lastname, email, picture, category, type, brand, size, bleed, bleedsize, filetype, footerurl, footerphone, copy, approved, proofread, seo, info, priority, requestdate, duedate, timinginfo, communication) VALUES ('" . $_SESSION['google_data']['given_name'] . "', '" . $_SESSION['google_data']['family_name'] . "','" . $_SESSION['google_data']['email'] . "', '" . $_SESSION['google_data']['picture'] . "', '$category', '$need', '$brands', '$size', '$bleed', '$bleedsize', '$filetype', '$footerurl', '$footerphone', '$copy', '$approved', '$proofread', '$seo', '$info', '$priority', '$requestdate', '$duedate', '$timinginfo', '$communication')"; 

     $insertinfo = mysqli_query($con, $sql); 
     if (!$insertinfo) { 
     die("Database query failed: " . mysqli_error($con)); 
     } else {  
     //Success, continue to email... 
     } 


    $plaintextversion= " 
    $type 

    BRAND: $brands 


    SPECS 

    SIZE: $size 

    BLEED: $bleed 

    BLEED SIZE: $bleedsize 

    FILE TYPE: $filetype 

    FOOTER URL: $footerurl 

    FOOTER PHONE: $footerphone 

    COPY: $copyforemail 

    COPY APPROVED? $approved 

    PROOFREAD? $proofread 

    ADDITIONAL INFO: $infoforemail 


    TIMING 

    PRIORITY: $priority 

    REQUEST DATE: $requestdate 

    DESIRED DUE DATE: $duedate 

    TIMING INFO: $timinginfoforemail 


    COMMUNICATION 

    ADDITIONAL PEOPLE TO INCLUDE: $communicationforemail"; 


    } else { 
     header("Location:index.php"); 
    } 

    $mail = new PHPMailer; 
    //$mail->SMTPDebug = 3;        // Enable verbose debug output 
    /* 
    $mail->isSMTP();          // Set mailer to use SMTP 
    $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
    $mail->SMTPAuth = true;        // Enable SMTP authentication 
    $mail->Username = '';    // SMTP username 
    $mail->Password = '';       // SMTP password 
    $mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
    $mail->Port = 587;         // TCP port to connect to 
    */ 
    $mail->setFrom($fromemail, $fullname); 
    $mail->addAddress('[email protected]', 'someone');  // Add recipients 
    $mail->addReplyTo('[email protected]', 'my name'); 
    /*$mail->addCC('[email protected]'); 
    $mail->addBCC('[email protected]'); 


    $mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name*/ 
    $mail->isHTML(false);         // Set email format to plain text since that is what Salesforce needs 

    $mail->Subject = $subject; 
    $mail->Body = $plaintextversion; 
    $mail->AltBody = $plaintextversion; 

    if(!$mail->send()) { 
     echo 'Message could not be sent.'; 
     echo 'Mailer Error: ' . $mail->ErrorInfo; 
    } else { 
     header("Location:request-success.php"); 
    } 
    ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <html> 
    <head> 
    <meta charset="utf-8"> 
    <meta name="robots" content="noindex, nofollow"> 
    <title>Untitled Document</title> 
    </head> 

    <body> 
    </body> 
    </html> 
+1

您的查询全面开放SQL注入攻击,如果您的内容中有引号,可能是您的问题的原因。你已经在使用mysqli了,所以你应该准备好语句功能供你使用。 –

回答

1

如果你想纯文本只,来电isHTML(false)只有Body设定 - 不要放任何东西AltBody

没有必要每次都从头开始 - 您可以重新使用相同的PHPMailer实例,只需在第二次发送之前更改属性。

您正在使用提交者地址作为发件人地址 - 这是伪造的,并且会导致SPF失败而退回,因此请将您的地址放入发件人和他们的回复中。

正如Jon所说的,验证,消毒和转义任何进入SQL的东西 - 很可能您的失败来自于包含'的提交,这将破坏您的SQL。

+0

感谢您的详细解答。在你看来,消毒和逃生的最佳方式是什么(我知道那里有很多意见)。 mysqli_real_escape_string够了吗?我使用textareas,但我会根据您的建议将它应用于每个字段。 – user3304303

+1

转义解决了大部分问题,但如果首先不允许错误数据,它会好得多。通过对任何不应包含HTML的输入应用'strip_tags',从电话号码字段中去除不允许的任何内容,从名称字段中去除条形码和符号,如果列表中有值(例如,在一个HTML选择),检查它是否等等。你可以通过做客户端(而不是!)来简化用户的操作。 jQuery验证对此很有帮助。 – Synchro