- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个由 PHP 页面处理的 HTML 表单。我已经使用 mail() 完全按照我的需要工作了,但是我遇到了电子邮件部分的问题。发送电子邮件非常不一致,这是 Not Acceptable 。我知道 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 不支持循环变量吗?
谢谢,埃里克
最佳答案
$mail->Body = 'Message content header stuff.<br><br><br><b>Street: </b> ' . $street0;
您是在循环中执行此操作,这意味着您的代码在每次迭代时都会覆盖电子邮件正文,只有最后一个会保留下来。然后退出循环并发送电子邮件。
您应该将这些值附加到主体而不是覆盖值。
$mail->Body.="New content for new project";
^
关于PHPMailer - 重复变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25268488/
如何安装 phpMailer在共享托管环境中?我需要将其用于用户的电子邮件验证和密码更改。 最佳答案 您可以在这里下载:https://github.com/PHPMailer/PHPMailer 将
当我使用 phpmailer 通过 Gmail 发送时,电子邮件需要很长时间才能发送,而且有时发送不成功。我正在使用 XAMPP 作为本地主机。 这是我的代码: if($_POST['submit']
我正在使用 PHPMailer 发送一些简单的电子邮件,但是函数 SetFrom() 似乎不起作用,即使我使用的代码直接来自 phpmails 文档 ( http://phpmailer.worxwa
使用 PHP 邮件程序 6.0.6 版 我知道类似的问题得到了回答,但我的情况不同。 我已经成功配置了它运行良好的脚本;我正在接收邮件,然后突然之间没有任何变化,它开始向我抛出这个错误。 错误
我可以生成 pdf 以便轻松查看和下载。我正在尝试发送一封附有 pdf 的电子邮件。我已经连续几天浏览谷歌了几个小时,没有什么比这个链接更让我更接近了,sending an email attachm
我可以生成 pdf 以便轻松查看和下载。我正在尝试发送一封附有 pdf 的电子邮件。我已经连续几天浏览谷歌了几个小时,没有什么比这个链接更让我更接近了,sending an email attachm
由于错误,我无法收到确认电子邮件。这是我的 PhpMailer 错误。 fatal error :未捕获错误:在 C:\xampp\htdocs\Web\PHPMailer\PHPMailer.php
这个问题在这里已经有了答案: SMTP connect() failed PHPmailer - PHP (17 个答案) 关闭 7 年前。 这是我在网上引用了很多之后从本地主机发送电子邮件的代码。
我写了一个 PHP 脚本,可以发送邮件。我从“mail@something.com”发送它们并将“返回路径”设置为“bounce@something.com”,但我仍然收到退回邮件到发件人邮件(“ma
我使用 php curl() 函数从服务器 A 连接到邮件服务器 B 以发送邮件。 $body = "body\n\nmore body\nmore body\n\nmore body\n\n"; $
PHPMailer 只是在发送电子邮件时挂起。我逐行注释了所有代码,一切正常,直到到达 如果(!$邮件->发送()) 此时页面进入旋转状态 - 没有错误消息或任何其他错误指示。我想我拥有所有必需的
我在问这里之前阅读了一些其他问题,因为其他答案没有回应我的问题。 我有一个用 php 定制的 cms。例如,如果我插入一个新的付款,脚本会向所有管理员用户发送一个通知: function insert
我用 Ubuntu 和 DigitalOcean 构建了一个 droplet,我正在尝试将它配置为使用 SMTP 发送电子邮件。 我知道 DigitalOcean 通过 IPv6 而不是通过 IPv4
我是 PHP 的新手。我从这里下载了一个 phpmailer 脚本(带有文件)-> phpmailer 我正在使用 XAMPP,我将文件定位在: \htdocs\QMS\phpmailer 我的 ph
我这里有一个大问题! 我需要向所有订阅者发送时事通讯(大约 1200) 问题是它只将时事通讯发送给其中的 150-180。 我有一个在 php 中实现的脚本,它使用 PhpMailer() 类将时事通
是否可以使用phpmailer发送异步电子邮件? 常规邮件发送代码段如下: $mail->Send(); PHP在继续之前等待Send()返回结果。是否有可能使phpmailer立即返回结果,而无需等
我有这样一种情况,我发送了两封不同的电子邮件,一封给客户,另一封在成员接受任务后发送给成员。在函数中我调用了 2 个函数,一个发送给客户端,一个发送给成员。发生的情况是第二封电子邮件同时发送给成员(m
我正在使用 PHPMailer在我的网站上,但它返回一个错误: 您必须提供至少一个收件人电子邮件地址。 服务器正在运行 PHP 7。我查看了以下页面以寻找答案: PHP Mailer You must
我正在尝试配置 PHPMailer 我上传了 1 个文件,它是 class.phpmailer.php 并创建了另一个包含以下内容的 php 文件: IsSMTP(); // enable SMTP
使用 PHPMailer 向我的 Gmail 帐户发送电子邮件后,单击“邮件发送者”右侧的“显示详细信息”后,它会显示“yourhostingaccount.com”。这是一张图片: 我读过您可以使用
我是一名优秀的程序员,十分优秀!