[在与PEAR一起使用时遇到问题
$smtp = Mail::factory ('smtp', $params);
通过自定义电子邮件服务器重定向smtp并添加附件(实际上是几个),这似乎是不可能的。
$HeadersCO = array (
'From' => $From,
'Return-path' => $From,
'To' => $Email,
'Subject' => $Subject2C,
'Reply-To' => $From,
'Date' => date("r"),
'Errors-To' => $Admin_Email,
'Return-Path' => $Company,
'X-Mailer' => "PHP v".phpversion(),
'X-Domain' => $SERVER_NAME,
'X-Page' => $PHP_SELF,
'Content-type' => $contentUTF8,
'MIME-Version' => $mime);
$mimeCO = new Mail_mime(array('eol' => $crlf));
$mimeCO->setTXTBody($textcontent); //Text version
$mimeCO->setHTMLBody($message); //HTML version
$imgNo=0;
while ($imgNo < $totalImages) {
$imageAtt = $imgarr[$imgNo];
$mimeCO->addAttachment($imageAtt, 'image/jpg');
$imgNo++;
}
$bodyCO = $mimeCO->get();
$hdrsCO = $mimeCO->headers($HeadersCO);
$mail2C = $smtp->send($email, $hdrsCO, $bodyCO);
这会将一封带有嵌入式图像但没有附件图像的电子邮件发送到正确的电子邮件服务器。即使我仅创建一个带有特定文件的附件,$mimeCO->addAttachment($imageAtt, 'image/jpg');
也似乎根本不起作用。
我读过某个地方,在使用PEAR MIME和邮件时,您可以将电子邮件重定向到其他SMTP服务,也可以添加附件,但不能同时添加两个。它是否正确?如果是的话,那就可以解释为什么我在使用PEAR中的$smtp->send
发送附件时遇到问题。
我能够通过使用PHPMailer解决PEAR [通过外部SMTP服务器发送并添加附件]的局限性。
我将PHPMailer设置为打开外部SMTP通道,最多发送4封不同的电子邮件,然后关闭SMTP会话,同时选择哪些电子邮件具有附件,图像嵌入,不同的主题,不同的正文,回复对象,发件人和addAddresses。
总体而言,我的代码从1400多行减少到800行以下,我对代码的灵活性感到非常满意。
这是新的PHPMailer代码的核心:
$mail = new PHPMailer(true); // Passing `true` enables exceptions
$mail->SMTPDebug = false; //3 Enables SMTP debugging, 0 turns off
$mail->isSMTP(); //Set PHPMailer to use SMTP.
$mail->SMTPKeepAlive = true; //Keep SMTP alive
$mail->Host = "smtp.emailserver.com";//Specify external SMTP servers
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "mypass";
$mail->SMTPSecure = "tls"; // Enable encryption, 'ssl'
$mail->Port = 587; //Set TCP port to connect to
//Recipients
$mail->addReplyTo('[email protected]', 'Company Name');
$mail->setFrom('[email protected]', 'Company Name');
//Add recipeints
$mail->addAddress($Email, "'".$FirstName." ".$LastName."'");
//CC and BCC
$mail->addBCC('[email protected]', "WebMaster Backup");
$mail->addCC('[email protected]', "CC Backup");
//Attachments from array using PHP
$imgNo=0;
while ($imgNo < $totalImages) {
$imageAtt = $imgArray[$imgNo];
$imageNam = $imgName[$imgNo];
$mail->addAttachment($imageAtt, $imageNam);
$imgNo++;
}
//Content
$mail->Subject = $Subject2C;
$mail->Body = $message;
$mail->AltBody = $textcontent;
$mail->isHTML(true); //Set email to be sent as HTML, false for Text only
/* =============Sending Customer Email====#1====== */
if($mail->send()) {
/* ==============Sending Company Email=====#2====== */
$mail->clearReplyTos();
$mail->clearAllRecipients(); // clear all Address/CC/BCC
$mail->clearAttachments();
$mail->addReplyTo($Email, "'".$FirstName." ".$LastName."'");
$mail->setFrom($Email, "'".$FirstName." ".$LastName."'");
$mail->addAddress('[email protected]', 'Email Sent');
$mail->addBCC('[email protected]', "WebMaster Backup");
$mail->Subject = $Subject2Company;
$mail->Body = $MessageEmail;
$mail->AltBody = $textcontent;
$mail->AltBody = $textcontent;
$mail->isHTML(true);
$mail->send();
/* ==============Subscribing Email=========#3====== */
$mail->clearAttachments();
$mail->clearReplyTos();
$mail->clearAllRecipients();
$mail->clearCCs();
$mail->addReplyTo('[email protected]', 'Company Name');
$mail->setFrom('[email protected]', 'Company Name');
$mail->addAddress($listmail, 'Subscribe List');
$mail->addBCC('[email protected]', "WebMaster Backup");
$mail->Subject = $subject;
$mail->Body = $SMessage;
$mail->AltBody = $body;
$mail->isHTML(false);
$mail->send();
$mail->SmtpClose(); //closing SMTP session