download12749 2011-07-13 09:33
浏览 49
已采纳

foreach数组由限定符分隔

Right now, i am reading in an array and doing a foreach loop on the array to run a set script, in this case, PHPmail. In that array, I have it working 100% to send the array to update the subject file. I want to update the code so that the script will still run on one foreach loop but will read in two separate strings (almost in effect in parallel) that will be delimited by a character... for example the character ~ ...

so, assume "list" had the following SUBJECT Goes here~/home/username/filename SUBJECT went here~/home/username2/filename2

Right now, the code would read in each line above as SubjectLiner and send the enter line in the subject.

I want the PHP to break these apart so that when the php is ran it would pull these two apart in one sequence. Right now, it works great without the ~ or the attachement component. (or the one many works just fine... i want to get the one to one working)..

so the desired result would be on the first pass SubjectLiner would be "SUBJECT Goes here" AttachmentLiner would be "/home/username/filename" and then on the second pass: SubjectLiner would be "SUBJECT went here" AttachmentLiner would be "/home/username2/filename2

below the is code I am testing.. I dont know how delimit based on a qualifier ~ in my example above.. and then have the two strings processed.

This same thing can be accomplished with two separate files that are matched line for line.. but the risk goes exponentially up to send the wrong attachment.

At the end of the day, I need to send a routine email to the same recipient (a server) that has a different Subject (w/ spaces and ;s) and different attachements. I have it 85% beaten. It is the dual information per line that is needed. Super appreciate any comments. thx.

<?php
require("class.phpmailer.php");

$addylist = file("list"); // Subject line feeder.

foreach($addylist as $SubjectLiner)
{
$mail = new PHPMailer();
$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username = "username";  // SMTP username
$mail->Password = "yourpassword"; // SMTP password
$mail->From = "username";
$mail->FromName = "username";
$mail->AddReplyTo("FromAddy", "From Name");
$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->IsHTML(true);                                  // set email format to HTML
$mail->AddAddress("ToAddy", "To Name");
$mail->Body    = "";
$mail->AltBody = "";

$mail->Subject = $SubjectLiner;

$mail->AddAttachment("$AttachmentLiner");         // add attachments



if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}


}

?>
  • 写回答

2条回答 默认 最新

  • dougai8673 2011-07-13 09:40
    关注

    Explode the $SubjectLiner and use the two parts:

    <?php
    require("class.phpmailer.php");
    
    $addylist = file("list"); // Subject line feeder.
    
    foreach($addylist as $SubjectLiner)
    {
        $SubjectLiner = explode("~",$SubjectLiner);
        $subject = $SubjectLiner[0];
        $attachment = $SubjectLiner[1];
    
        //$mail = .... Same as in your example
    
        $mail->Subject = $subject;
        $mail->AddAttachment($attachment);         // add attachments
    
        if(!$mail->Send())
        {
           echo "Message could not be sent. <p>";
           echo "Mailer Error: " . $mail->ErrorInfo;
           exit;
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度