PHPMailer - 重复变量

时间:2014-08-12 15:37:40

标签: php email phpmailer

我有一个由PHP页面处理的HTML表单。我使用 mail()完全按照我的需要工作,但遇到了电子邮件部分的问题。发送电子邮件非常不一致,这是不可接受的。我知道mail()只负责流程的一小部分,邮件服务器负责繁重的工作。

我正在尝试PHPMailer作为替代方案。我已经启动并运行,并且能够将邮件发送出去,但有些功能不存在。

在我的表单中,您可以添加多个'项目'一次提交。 php应该循环遍历这些项目并为电子邮件中的每个项目创建一个部分。同样,这与mail()一起使用,但并不总是发送。

我想要实现的代码如下。它将发送一封电子邮件,但如果有多个,则不会循环显示表单字段。它只会看到输入的最后一个。

<?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\n";

$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';
}

这是有效但不一致发送的mail()代码。

<?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\n";

    $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" . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers .= "From: "."Team Traffic "." <email@email.com>" . "\r\n";

    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);
}

?>

我在这里错过了一步吗? PHPMailer不能用于循环变量吗?

谢谢你, 埃里克

2 个答案:

答案 0 :(得分:5)

$mail->Body    = 'Message content header stuff.<br><br><br><b>Street: </b> ' . $street0;

你是在循环中这样做,这意味着你的代码在每次迭代时都会覆盖电子邮件正文,只剩下最后一个。然后退出循环并发送电子邮件。

您应该将这些值附加到正文而不是覆盖该值。

$mail->Body.="New content for new project";
           ^

答案 1 :(得分:0)

我认为$mail->Subject = $project;应该不在循环中。

至于身体我认为你忘记添加一个点(。=)来积累消息。

$mail->Body .= 'Message content header stuff.<br><br><br><b>Street: </b> ' . $street0;