简体   繁体   中英

problem in opening the attachment in mail

i am able to send attachment with mail but attachment is not getting opened. i tried addStringAttachment PHPMailer function for sending mail with parameter($pathinfo,$filename,'base64','MIME') i want to open attachment in mail but not opening problem occuring could not load image or any file..

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

if(isset($_POST["send"]))
{

    
    $mail=new PHPMailer(true);

    $attachment=$_FILES['file']['tmp_name'];
    $folder="upload/";
    $file_name=$_FILES['file']['name'];
    
    
    //move_uploaded_file($_FILES['file']['tmp_name'],"$folder".$_FILES['file']['name']);

    $mail->isSMTP();
    $mail->Host='smtp.gmail.com';
    $mail->SMTPAuth=true;
    $mail->Username='lilyj1895@gmail.com';
    $mail->Password='plyvoqmpadbnqjls';
    $mail->SMTPSecure='ssl';
    $mail->Port=465;

    $mail->setFrom('lilyj1895@gmail.com');

    $mail->addAddress($_POST["email"]);

    
    $mail->addStringAttachment($folder,$file_name,'base64','application/octet-stream');

    $mail->isHTML(true);

    $mail->Subject=$_POST["subject"];

    $mail->Body=$_POST["message"];

    

    $mail->send();

    $attach_file = $folder."".$file_name;
    
    echo"
        <script>
                    alert('Sent Successfully');
                    document.location.href='mailattach.php';
        </script>";
}

?>

        
        

        
        



    

First the bad news: your form is a trivially vulnerable spam and phishing/malware gateway. It will quickly be exploited and used to send spam because you allow the subject, recipient, message body, and attachments to be specified by the submitter.

As for the specific problem you asked about: You're using the wrong method.

addStringAttachment is used to add an in-memory string as an attachment, and is often used for things like PDFs generated by TCPDF, where you have the actual content in a variable. In your case, the content you're attaching is the filename!

You need to use addAttachment instead, which loads the content from a local file path (note not a URL).

Change your code to this:

$mail->addStringAttachment($folder, $file_name);

Note that I've also removed the encoding and MIME type params. These are figured out automatically from the filename, so you don't need to specify them yourself.

You commented out the call to move_uploaded_file ; that's a bad idea as skipping that is unsafe.

Overall, I recommend that you rewrite your script following the PHPMailer example for building a safe contact form , and how to send an uploaded attachment safely .

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