简体   繁体   中英

Email form sends multiple mails depending on attachments

So basically I want an email system that can send a message with one or more pictures, the problem I have now is that when I upload let's say 4 pictures, the form sends me 4 emails with both the same text but one picture at a time. I want them all to be added as 4 attachments to 1 email, not 1 attachment to 4 emails.

Here is my HTML:

<form id="upload" method="post" action="upload.php">
            <div id="drop">
                Onderwerp<br>
                <input name="txtSubject" type="text" id="txtSubject" /><br>

                Omschrijving<br>
                <textarea name="txtDescription" cols="30" rows="4" id="txtDescription"></textarea><br>

                Voeg afbeeldingen toe
                <input type="file" name="fileAttach" multiple accept="image/*;capture=camera" />

                <input type="submit" name="Submit" value="Send">
            </div>

            <ul>
                <!-- The file uploads will be shown here -->
            </ul>

        </form>

and here is my PHP:

<?
    $strTo = "email@email.com";
    $strSubject = $_POST["txtSubject"];
    $strMessage = nl2br($_POST["txtDescription"]);

    //*** Uniqid Session ***//
    $strSid = md5(uniqid(time()));

    $strHeader = "";
    $strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

    $strHeader .= "MIME-Version: 1.0\n";
    $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
    $strHeader .= "This is a multi-part message in MIME format.\n";

    $strHeader .= "--".$strSid."\n";
    $strHeader .= "Content-type: text/html; charset=utf-8\n";
    $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
    $strHeader .= $strMessage."\n\n";

    //*** Attachment ***//
    if($_FILES["fileAttach"]["name"] != "")
    {
        $strFilesName = $_FILES["fileAttach"]["name"];
        $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
        $strHeader .= "--".$strSid."\n";
        $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
        $strHeader .= "Content-Transfer-Encoding: base64\n";
        $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
        $strHeader .= $strContent."\n\n";
    }


    $flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //

    if($flgSend)
    {
        echo "Mail send completed.";
    }
    else
    {
        echo "Cannot send mail.";
    }
?>

I'm not that great with PHP, I took an multiple image uploader and tried to make it into a mailing form, wich works.. except for the multiple email sending part.

Any help is greatly appreciated! :)

try something like:

for($i=0;$i<count($_FILES["fileAttach"]["name"]);$i++)  
{  
if($_FILES["fileAttach"]["name"][$i] != "")  
{  
$strFilesName = $_FILES["fileAttach"]["name"][$i];  
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"][$i])));  
$strHeader .= "--".$strSid."\n";  
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
$strHeader .= "Content-Transfer-Encoding: base64\n";  
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
$strHeader .= $strContent."\n\n";  
}  
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM