PHPMailer-重复变量
我有一个由php页面处理的HTML表单。使用mail()时,我已经让它完全按照我的需要工作,但在电子邮件部分遇到了问题。发送电子邮件是非常不一致的,这是不可接受的。我知道mail()只负责流程的一小部分,邮件服务器负责繁重的工作 我正在尝试另一种选择。我已经安装并运行了它,能够将邮件发送出去,但有些功能不存在 在我的表单中,您可以将多个“项目”添加到单个提交中。php应该循环这些项目,并在电子邮件中为每个项目创建一个部分。同样,这也适用于mail(),但并不总是发送 下面是我试图实现的代码。它将发送电子邮件,但如果有多个表单字段,则不会在表单字段上循环。它将只看到最后一个输入PHPMailer-重复变量,php,email,phpmailer,Php,Email,Phpmailer,我有一个由php页面处理的HTML表单。使用mail()时,我已经让它完全按照我的需要工作,但在电子邮件部分遇到了问题。发送电子邮件是非常不一致的,这是不可接受的。我知道mail()只负责流程的一小部分,邮件服务器负责繁重的工作 我正在尝试另一种选择。我已经安装并运行了它,能够将邮件发送出去,但有些功能不存在 在我的表单中,您可以将多个“项目”添加到单个提交中。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';
}
我在这里漏了一步吗?PHPMailer不能在变量上循环吗
谢谢,,
Eric我认为
$mail->Subject=$project代码>应该在循环之外
至于body,我想你忘了加一个点(.=)来积累信息
$mail->Body .= 'Message content header stuff.<br><br><br><b>Street: </b> ' . $street0;
$mail->Body.=“邮件内容标题内容。
街道:”$第0街;
您正在每个循环迭代中覆盖$mail->Subject
、$mail->Body
和$mail->AltBody
。只需像在旧代码中那样附加一个字符串,然后将->Body设置为stringThreak@user574632。你说得对。一点点。让我很困惑。非常感谢您的投入和帮助!我非常感激。将在时间限制到期时标记为正确。
$mail->Body = 'Message content header stuff.<br><br><br><b>Street: </b> ' . $street0;
$mail->Body.="New content for new project";
^
$mail->Body .= 'Message content header stuff.<br><br><br><b>Street: </b> ' . $street0;