如何使用PHP发送带有使用html表单上传的图像的电子邮件

So right now. Email send is just fine. Works great. Except the uploaded image does not send. That is the main issue here. What can I do to get this working? Again. Basically only problem image not sending. Thank you.

function sendEmail($email,$subject,$content,$sent_from_email_address)
{
        $mail = new PHPMailer();  
        $mail->IsSMTP();  
        $mail->Host = "stuff";   
        $mail->SMTPAuth = true;       
        $mail->Username = "stuuff";
        $mail->Password = "stuff";
        $mail->Port = "stuff";        
        $mail->setFrom($sent_from_email_address, "stuff");
        $mail->Encoding = "stuff";  
        $mail->Subject = $subject;
        $mail->msgHTML($content); 
        $mail->AddAddress($email);    
        if (!$mail->Send()) 
            return 0;
        else
            return 1;
}///// close function /////




$email_to = "email@email.com";
$email_subject = "Subject";
$email_from = "People";
        
$b_frontPhoto = $_FILES['before1']; // required
$b_backPhoto = $_FILES['before2']; // required 
$b_sidePhoto = $_FILES['before3']; // required
$s_Weight = $_POST['startWeight']; // not required
$s_pantSize = $_POST['startPants']; // required
$s_dressSize = $_POST['startDress']; // required
// $why_start = $_POST['bwell_code']; // required

$email_message = "Form details below.<br><br>";

$email_message .= "Name: ".$b_frontPhoto."<br>";
$email_message .= "Address: ".$b_backPhoto."<br>";
$email_message .= "City: ".$b_sidePhoto."<br>";
$email_message .= "State: ".$s_Weight."<br>";
$email_message .= "Zip: ".$s_pantSize."<br>";
$email_message .= "Email: ".$s_dressSize."<br>";
// $email_message .= "Redemption_Code: ".$b_redemption."<br>";
    
sendEmail($email_to,$email_subject,$email_message,'email@email.com');

</div>

You need to modify the headers of the email to let PHPMailer know it is expecting images. PHPMailer has a function AddEmbeddedImage that will do this for you. This answer may help.

Here is how I embed images using PHPMailer..

$mail->AddEmbeddedImage('pathto/image.jpg', 'uniqueID');

Then in the body of the message ...

   $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ......
    <img alt="alt text for image" src=\'cid:uniqueID\' />

And FYI, If the images are being uploaded, then they are saved to your server, at least to your /tmp directly where one would usually move them to a desired location, but you can certainly delete them after sending though.

I have added the files option in your code. See below.

function sendEmail($email,$subject,$content,$sent_from_email_address,$files)
{
    $mail = new PHPMailer();  
    $mail->IsSMTP();  
    $mail->Host = "stuff";  
    $mail->SMTPAuth = true;      
    $mail->Username = "stuuff";
    $mail->Password = "stuff";
    $mail->Port = "stuff";       
    $mail->setFrom($sent_from_email_address, "stuff");
    $mail->Encoding = "stuff";  
    $mail->Subject = $subject;
    $mail->msgHTML($content); 
    $mail->AddAddress($email);   
    foreach ($files as $fileName => $fileLocation) {
        $mail->AddAttachment($fileLocation, $fileName);
    }
    if (!$mail->Send()) 
        return 0;
    else
        return 1;
}

function extractLocation($file) {
    $allowedTypes = [
        'image/gif',
        'image/jpg',
        'image/jpeg',
        'image/png'
    ];
    if (isset($file['tmp_name']) && in_array($file['type'], $allowedTypes)) {
        return $file['tmp_name'];
    }
}

$email_to = "email@email.com";
$email_subject = "Subject";
$email_from = "People";

$files = [];

$files['b_frontPhoto'] = extractLocation($_FILES['b_frontphoto']); 
$files['b_backPhoto'] = extractLocation($_FILES['b_backPhoto']); 
$files['b_sidePhoto'] = extractLocation($_FILES['b_sidePhoto']); 

// Always sanitize or validate input
$s_Weight = filter_input(INPUT_POST, 'startweight',  FILTER_SANITIZE_NUMBER_FLOAT);
$s_pantSize = filter_input(INPUT_POST, 'startPants',  FILTER_SANITIZE_NUMBER_FLOAT);
$s_dressSize = filter_input(INPUT_POST, 'startDress',  FILTER_SANITIZE_NUMBER_FLOAT);
// $why_start = filter_input(INPUT_POST, 'bwell_code', FILTER_SANITIZE_STRING);

$email_message = "Form details below.<br><br>";


//$email_message .= "Name: ".$b_frontPhoto."<br>";
//$email_message .= "Address: ".$b_backPhoto."<br>";
//$email_message .= "City: ".$b_sidePhoto."<br>";
$email_message .= "State: ".$s_Weight."<br>";
$email_message .= "Zip: ".$s_pantSize."<br>"; 
$email_message .= "Email: ".$s_dressSize."<br>";
// $email_message .= "Redemption_Code: ".$b_redemption."<br>";

sendEmail($email_to,$email_subject,$email_message,'email@email.com',$files);
function rrmdir($src) { //FROM PHP.net
    $dir = opendir($src);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            $full = $src . '/' . $file;
            if ( is_dir($full) ) {
                rrmdir($full);
            }
            else {
                unlink($full);
            }
        }
    }
    closedir($dir);
    rmdir($src);
}

function sendEmail($email,$subject,$content,$sent_from_email_address, $arrImgFullPath)
{
        $mail = new PHPMailer();  
        $mail->IsSMTP();  
        $mail->Host = "stuff";  
        $mail->SMTPAuth = true;      
        $mail->Username = "stuuff";
        $mail->Password = "stuff";
        $mail->Port = "stuff";       
        $mail->setFrom($sent_from_email_address, "stuff");
        $mail->Encoding = "stuff";  
        $mail->Subject = $subject;
        $mail->msgHTML($content); 
        $mail->AddAddress($email);
        /*process your array*/
        foreach($arrImgFullPath as $ImgData){
           $mail->$email->AddAttachment( $ImgData['path'] , $ImgData['filename'] );
        }
        if (!$mail->Send()) 
            return 0;
        else
            return 1;
}///// close function /////





/* create a tmp folder to move and rename your of uploaded images */
$tmp_dirName = $_SERVER['DOCUMENT_ROOT'] . strval(time()). "_tmpPhotoDir"; //fullPath
mkdir($tmp_dirName, 0777);

/* create an array for post processing in mail function */
$arrImgFullPath = array();

foreach($_FILES as $k=>$v){
    /*move image with new/real filename*/

   move_uploaded_file($_FILES[$k][$v]['tmp_name'], $tmp_dirName . "/" . $_FILES[$k][$v]['name']);
    /*array for post processing*/

   $arrImgFullPath[] = array(
     'path' => $tmp_dirName . "/" . $_FILES[$k][$v]['name'],
     'filename' =>$_FILES[$k][$v]['name']
   );
}

$email_to = "email@email.com";
$email_subject = "Subject";
$email_from = "People";

$s_Weight = $_POST['startWeight']; // not required
$s_pantSize = $_POST['startPants']; // required
$s_dressSize = $_POST['startDress']; // required
// $why_start = $_POST['bwell_code']; // required

$email_message = "Form details below.<br><br>";

$email_message .= "Name: ".$b_frontPhoto."<br>";
$email_message .= "Address: ".$b_backPhoto."<br>";
$email_message .= "City: ".$b_sidePhoto."<br>";
$email_message .= "State: ".$s_Weight."<br>";
$email_message .= "Zip: ".$s_pantSize."<br>";
$email_message .= "Email: ".$s_dressSize."<br>";
// $email_message .= "Redemption_Code: ".$b_redemption."<br>";

sendEmail($email_to,$email_subject,$email_message,'email@email.com', $arrImgFullPath);
rrmdir($tmp_dirName) //remove your temp dir

Please test and let me know.

Okay, so basically figured this whole thing out. The problem was with my form not having (enctype="multipart/form-data") I think that was the main issue. I will include working code below.

<?php require_once("../includes/PHPMailer/PHPMailerAutoload.php"); ?>
<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
    
    function sendEmail($email,$subject,$content,$sent_from_email_address)
{   $b_frontPhoto = $_FILES['before1']['tmp_name']; // required
    $b_backPhoto = $_FILES['before2']['tmp_name']; // required 
    $b_sidePhoto = $_FILES['before3']['tmp_name']; // required
 var_dump($b_frontPhoto);
        $mail = new PHPMailer();  
        $mail->IsSMTP();  
        $mail->Host = "xxxxxx";  
        $mail->SMTPAuth = true;       
        $mail->Username = "xxxxxx";
        $mail->Password = "xxxxx";
        $mail->Port = "xxxx";         
        $mail->setFrom($sent_from_email_address, "xxxx");
        $mail->Encoding = "8bit";  
        $mail->Subject = $subject;
        $mail->msgHTML($content);
        $mail->AddAddress($email); 
        $mail->addAttachment($b_frontPhoto);
        $mail->addAttachment($b_backPhoto);
        $mail->addAttachment($b_sidePhoto);
 
        if (!$mail->Send()) 
            return 0;
        else
            return 1;
}///// close function /////

if(!empty($_POST)){
    
    
    $email_to = "xxxxxx";
    $email_subject = "xx";
    $email_from = "xx";
        
    $b_frontPhoto = $_FILES['before1']; // required
    $b_backPhoto = $_FILES['before2']; // required 
    $b_sidePhoto = $_FILES['before3']; // required
    $s_Weight = $_POST['startWeight']; // not required
    $s_pantSize = $_POST['startPant']; // required
    $s_dressSize = $_POST['startDress']; // required
   // $why_start = $_POST['bwell_code']; // required

    $email_message = "Form details below.<br><br>";

    $email_message .= "Name: ".$b_frontPhoto."<br>";
    $email_message .= "Address: ".$b_backPhoto."<br>";
    $email_message .= "City: ".$b_sidePhoto."<br>";
    $email_message .= "State: ".$s_Weight."<br>";
    $email_message .= "Zip: ".$s_pantSize."<br>";
    $email_message .= "Email: ".$s_dressSize."<br>";
   // $email_message .= "Redemption_Code: ".$b_redemption."<br>";
    
sendEmail($email_to,$email_subject,$email_message,'xxxxx',$b_frontPhoto,$b_backPhoto,$b_sidePhoto);
    

exit;
    
    
    
    }


?>


<form name="beforePhoto" method="post" id="beforePhoto" action="" enctype="multipart/form-data">
        <div class="form-group">
          <label>Front Before Photo</label>
          <div class="input-group">
            <input type="text" class="form-control">
            <label class="input-group-btn"> <span class="btn btn-grn rip"> Choose file
              <input name="before1" type="file" style="display:none;">
              </span> </label>
          </div>
        </div>
</form>
etc...

</div>