doukundong9652 2014-08-12 15:37
浏览 130
已采纳

PHPMailer - 重复变量

I have a HTML form that is being processed by a php page. I have had it working exactly as I need using mail() but am running into issues with the email part. Sending an email out is very inconsistent which is not acceptable. I understand that mail() only takes care of a small part of the process and the mail servers take care of the heavy lifting.

I am trying out PHPMailer as an alternative. I have it up and running and am able to get mail to go out but some of the functionality is not there.

In my form you can add multiple 'projects' to a single submission. The php is supposed to loop over those projects and create a section for each in the email. Again, this is working with mail() but doesn't always send.

The code I am trying to implement is below. It will send an email but will not loop over the form fields if there are more than one. It will only see the last one entered.

<?php
require 'PHPMailerAutoload.php';

date_default_timezone_set('America/New_York');
$today = date("F j - Y - g:i a");

$mail = new PHPMailer;

$mail->isSMTP();                       // Set mailer to use SMTP
$mail->Host = 'mail.example.com';      // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                // Enable SMTP authentication
$mail->Username = 'mail@example.com';  // SMTP username
$mail->Password = 'password';          // SMTP password
$mail->SMTPSecure = 'tls';             // Enable encryption, 'ssl' also accepted

$mail->From = 'mail@example.com';
$mail->FromName = 'From name';
$mail->addAddress('mail@example.com', 'personName');     // Add a recipient
//$mail->addAddress('mail@anotherexample.com');          // Name is optional
$mail->addReplyTo('mail@example.com', 'replyTO');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

$mail->WordWrap = 500;                               // 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



//VARIABLES FROM FORM FIELDS
$contractor = $_POST['ct'];
$noactive = $_POST['noconactivity'];
$hours = $_POST['hours'];
$project = $_POST['prjx'];
$city = $_POST['cx'];
$street = $_POST['street'];
$from = $_POST['from'];
$to = $_POST['to'];
$crewxtown = $_POST['cxtown'];
$construction = $_POST['construction'];
$mpt = $_POST['mpt'];
$direction = $_POST['direction'];
$police = $_POST['police'];
$optcomments = $_POST['optcomments'];
$submissionemail = $_POST['submissionemail'];
$mail_cm = $_POST['cm'];
$mail_pm = $_POST['pm'];
$intersection = $_POST['intersection'];
$parking = $_POST['parking'];


$count = count($street)-1;


$data = array();

//REPETITIVE VARIABLES
for( $i = 0; $i <= $count; $i++ )
{   
    $hours0 = $hours[$i];
    $street0 = $street[$i];
    $from0 = $from[$i];
    $to0 = $to[$i];
    $crewxtown0 = $crewxtown[$i];
    $construction0 = $construction[$i];
    $mpt0 = $mpt[$i];
    $direction0 = $direction[$i];
    $police0 = $police[$i];
    $optcomments0 = $optcomments[$i];
    $parking0 = $parking[$i];
    $intersection0 = $intersection[$i];

    $data[] = "$today, $noactive, $contractor, $hours0, $project, $city, $street0, $from0, $to0, $intersection0, $construction0, $mpt0, $crewxtown0, $direction0, $police0, $parking0, $optcomments0, $submissionemail, $mail_cm, $mail_pm
";

$mail->Subject = $project;
$mail->Body    = 'Message content header stuff.<br><br><br><b>Street: </b> ' . $street0;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

}


// WRITING DATA TO CSV TABLE
if(!empty($data)) {
    $data = implode('', $data);

    $fh = fopen("dailyupdatedata.csv", "a");
    fwrite($fh, $data);
    fclose($fh);
}


//SUCCESS & FAILURE MESSAGE ON PHP PAGE

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

Here is the mail() code that works but does not consistently send.

<?php
date_default_timezone_set('America/New_York');

$contractor = $_POST['ct'];
$noactive = $_POST['noconactivity'];
$hours = $_POST['hours'];
$project = $_POST['prjx'];
$city = $_POST['cx'];
$street = $_POST['street'];
$from = $_POST['from'];
$to = $_POST['to'];
$crewxtown = $_POST['cxtown'];
$construction = $_POST['construction'];
$mpt = $_POST['mpt'];
$direction = $_POST['direction'];
$police = $_POST['police'];
$optcomments = $_POST['optcomments'];
$submissionemail = $_POST['submissionemail'];
$mail_cm = $_POST['cm'];
$mail_pm = $_POST['pm'];
$intersection = $_POST['intersection'];
$parking = $_POST['parking'];
$count = count($street)-1;

$today = date("F j - Y - g:i a");//

$message = '<html><body>';
$message .= "Please see the info blah blah<br><strong>Date:</strong> $today<br><strong>Submission by:</strong> $submissionemail<br><br>"; // Beginning message content
$data = array();

for( $i = 0; $i <= $count; $i++ )
{   
    $hours0 = $hours[$i];
    $street0 = $street[$i];
    $from0 = $from[$i];
    $to0 = $to[$i];
    $crewxtown0 = $crewxtown[$i];
    $construction0 = $construction[$i];
    $mpt0 = $mpt[$i];
    $direction0 = $direction[$i];
    $police0 = $police[$i];
    $optcomments0 = $optcomments[$i];
    $parking0 = $parking[$i];
    $intersection0 = $intersection[$i];

    $data[] = "$today, $noactive, $contractor, $hours0, $project, $city, $street0, $from0, $to0, $intersection0, $construction0, $mpt0, $crewxtown0, $direction0, $police0, $parking0, $optcomments0, $submissionemail, $mail_cm, $mail_pm
";

    $message .= "<strong>Project:</strong> $project<br><strong>Active / Not Active:</strong> $noactive<br><strong>Contractor:</strong> $contractor<br><strong>Town:</strong> $city<br><strong>Hours:</strong> $hours0<br><strong>Street:</strong> $street0<br><strong>From:</strong> $from0<br><strong>To:</strong> $to0<br><strong>Intersection:</strong> $intersection0<br><strong>Construction Activity:</strong> $construction0<br><strong>MPT:</strong> $mpt0<br><strong>Crew Town:</strong> $crewxtown0<br><strong>Closure Direction:</strong> $direction0<br><strong>Police & Flaggers:</strong> $police0<br><strong>Parking Restrictions:</strong> $parking0<br><strong>Optional Comments:</strong> $optcomments0<br><br> -- <br><br>"; //Data for message
}
$message .= '</body></html>';

if(!empty($data)) {
    $data = implode('', $data);

    $fromemail = "email@email.com"; // email@email.com
    $subject = $project; 

    //$headers = "From:" . $fromemail;
    $headers  = "MIME-Version: 1.0" . "
";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "
";
    $headers .= "From: "."Team Traffic "." <email@email.com>" . "
";

    mail($submissionemail,$subject,$message,$headers); // Submission Email
    mail($mail_cm,$subject,$message,$headers); // C Manager Email
    mail($mail_pm,$subject,$message,$headers); // P Manager Email
    mail("email@email",$subject,$message,$headers);
    //mail($trafficemail,$subject,$message,$headers); // Traffic


    $fh = fopen("dailyupdatedata.csv", "a");
    fwrite($fh, $data);
    fclose($fh);
}

?>

Am I missing a step here? Does PHPMailer not work with looping over variables?

Thank you, Eric

  • 写回答

2条回答 默认 最新

  • douji0588 2014-08-12 15:43
    关注
    $mail->Body    = 'Message content header stuff.<br><br><br><b>Street: </b> ' . $street0;
    

    You are doing that in a loop, that means your code is overwriting the email body on every iteration and only the last one will stay. And then you exit the loop and send the email.

    You should be appending those values to the body instead of overwriting the value.

    $mail->Body.="New content for new project";
               ^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongqie2028 2014-08-12 15:45
    关注

    I think $mail->Subject = $project; should be out of the loop.

    As for body I think you forgot adding a dot(.=) to accumulate the message.

    $mail->Body .= 'Message content header stuff.<br><br><br><b>Street: </b> ' . $street0;
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (关键词-聊天软件)
  • ¥15 求大家看看这个编程的编法没有思路啊
  • ¥20 WSL打开图形化程序子窗口无法点击
  • ¥15 Jupyter Notebook 数学公式不渲染
  • ¥20 ERR_CACHE_MISS 确认重新提交表单
  • ¥20 关于vba使用HTMLfile执行js函数问题
  • ¥60 悬赏求解,通过实时现场摄像头的视频图像识别其他对家打出的麻将牌,识别麻将牌,识别牌墙位置,通过识别对家打出了什么牌
  • ¥15 关于#GPU jetson#的pcie驱动开发问题,如何解决?
  • ¥15 stm32f103zet6 串口5无法收发数据
  • ¥15 关于C语言使用线程队列实现多线程并发