download12749 2011-07-13 01: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 01: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部