Php 间歇工作的附件
我的phpMailer脚本有问题。我可以通过脚本发送电子邮件,甚至可以通过脚本发送附件。然而,它只在某些时候起作用,而在其他时间不起作用。我尝试过更换服务器,但我也遇到了同样的问题,所以我认为这是一个编码问题 当它不起作用时,电子邮件仍然会被彻底删除,但会从任何附件中删除 附件肯定是在同一位置上发送到服务器上的,然后此脚本将发送一组附件,然后添加到消息中 在一个实例中,相同的代码和相同的文件将发送和不发送!非常混乱!通常,有些人会发送一段时间,然后有些人会不发送一段时间 代码如下:Php 间歇工作的附件,php,phpmailer,Php,Phpmailer,我的phpMailer脚本有问题。我可以通过脚本发送电子邮件,甚至可以通过脚本发送附件。然而,它只在某些时候起作用,而在其他时间不起作用。我尝试过更换服务器,但我也遇到了同样的问题,所以我认为这是一个编码问题 当它不起作用时,电子邮件仍然会被彻底删除,但会从任何附件中删除 附件肯定是在同一位置上发送到服务器上的,然后此脚本将发送一组附件,然后添加到消息中 在一个实例中,相同的代码和相同的文件将发送和不发送!非常混乱!通常,有些人会发送一段时间,然后有些人会不发送一段时间 代码如下: <?p
<?php
include('connection.php');
require_once('PHPMailer/PHPMailerAutoload.php');
class Mailer extends PHPMailer {
public function copyToFolder($folderPath) {
$message = $this->MIMEHeader . $this->MIMEBody;
$path = "INBOX" . (isset($folderPath) && !is_null($folderPath) ? ".".$folderPath : ""); // Location to save the email
$imapStream = imap_open("{mail.server.com:143/novalidate-cert}" . $path , $this->Username, $this->Password) or die(mysql_error());
imap_append($imapStream, "{mail.server.com:143/novalidate-cert}" . $path, $message)or die(mysql_error());
}
imap_close($imapStream);
}
}
$from = $_POST['from'];
$username = $from;
$grabPassword = mysql_query("SELECT * FROM `email_pw_db` WHERE `emailaddress` = '$from'");
$fetchPassword = mysql_fetch_assoc($grabPassword);
$password = $fetchPassword['password'];
$company = $_POST['to'];
$toemail = $_POST['toemail'];
$from = $username;
$namefrom = $_POST['from'];
$subject = $_POST['subject'];
$cc = trim($_POST['cc']);
$bcc = trim($_POST['bcc']);
$message = $_POST['body'];;
$attachments = $_POST['attachments'];
$mail = new Mailer();
$body = $message;
/*Create a new email*/
$mail = new Mailer();
$mail->isSMTP();
$mail->Host = 'mail.server.com';
$mail->Username = $username;
$mail->Password = $password;
$mail->From = $from;
$mail->AddReplyTo($from,$namefrom);
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->SetFrom($from, $namefrom);
$mail->AddReplyTo($from,$namefrom);
$address = $toemail;
$mail->AddAddress($address, $company);
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
if($cc!=''){
$mail->addCC($cc);
}
//CC EMAILS//
$breakOutCC = explode(',', $cc);
$countBreakOut = count($breakOutCC);
$i = 0;
while($i <$countBreakOut)
{
$mail->addCC($breakOutCC[$i]);
$i++;
}
$breakOutBCC = explode(',', $bcc);
$countBreakOutBCC = count($breakOutBCC);
$i = 0;
while($i <$countBreakOutBCC)
{
$mail->addBCC($breakOutBCC[$i]);
$i++;
}
$breakoutAttachments = explode(',', $attachments);
$countAttachments = count($breakoutAttachments);
$i = 0;
while($i <$countAttachments)
{
$mail->AddAttachment("attachments/email_attachments/".$breakoutAttachments[$i]);
$i++;
}
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
$errorMessage = $mail->ErrorInfo;
header( 'Location: email-software.php?dialog=Whoops Something Happened- '.$errorMessage ) ;
} else {
//$mail->copyToFolder(); //save email
$mail->copyToFolder("Sent"); // Will save into Sent folder
$attachments = $_POST['attachments'];
$breakoutAttachments = explode(',', $attachments);
$countAttachments = count($breakoutAttachments);
$i = 0;
while($i <$countAttachments)
{
unlink("attachments/email_attachments/".$breakoutAttachments[$i]);
$i++;
}
header( 'Location: email-software.php?dialog=email sent' ) ;
}
?>
完成了任何基本调试,比如检查附件文件是否确实存在并且可读?检查了AddAttachment的返回值?我检查了服务器上的图像,它们肯定在那里并且存在…并且可读。至于返回值,你的意思是像这样$success=$mail->AddAttachment(“附件/email_attachments/”)$breakutattachments[$i]);如果它真的是间歇性的,我建议您使用调试器并设置适当的断点来捕获它。您的示例中有太多的代码需要调试-请将其减少以消除错误源。@ExecutiveCloser您是否了解了这一点?我现在也有同样的问题。每当我进行故障排除时,附件都很好,但随后电子邮件就会发送到客户机,附件就会失败。正如您所描述的,相同的文件、相同的脚本,但附件只是间歇性工作。@sunny我确实解决了这个问题,我的附件从本地计算机上载到本地服务器,然后邮寄出去。我相信这是由于文件被覆盖/删除,或者在将页面发送到我的邮件脚本之前页面上的引用不存在。