简体   繁体   中英

Attachment not sent with Email

I am developing a small E-mail app using PHP, For sending out emails I use PHPMailer, All the functionalities work apart form one "Attachments". Attachments just don't go through with the sent email. I tried many different ways to fix it but the one I have now I belief is not far off a working order....

I will post small parts of code that deal with the Attachment process....

HTML Code:

<form action="php/smtp_saved.php" method="post">
  <input type="file" name="attach" id="attach" />
</form>

PHP Code:

if ($_POST['action'] == 'Send') {

    if (preg_match('<,>', $email['recipient'])) {

        $address = explode(',', $email['recipient']);

        if (sizeof($address) < 19) {

            foreach ($address as $recipient) {

                $save = new saveSaved();
                $save->save($_SESSION['user'], $email['recipient'], gmdate('Y-m-d H:i:s', strtotime ('+1 hour')), $_SESSION['delete'],$email['HTML']);

                require_once('../PHPMailer/PHPMailerAutoload.php');
                require_once('../PHPMailer/class.smtp.php');

                $mail = new PHPMailer;

                $mail->isSMTP(); // Set mailer to use SMTP
                $mail->Host = 'smtp.gmail.com'; // Specify main and backup server
                $mail->Port = '465';
                $mail->SMTPAuth = true; // Enable SMTP authentication
                $mail->Username = $_SESSION['user']; // SMTP username
                $mail->Password = $_SESSION['pass']; // SMTP password
                $mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted

                $mail->From = $_SESSION['user'];
                $mail->FromName = 'OneTwoTrade';
                //$mail->addAddress($email['recipient']);  // Add a recipient
                //$mail->addAddress('ellen@example.com');  // Name is optional
                $mail->addAddress(trim($recipient));
                if (preg_match('<,>', $email['cc'])) {

                    $cc = explode(',', $email['cc']);

                    if (sizeof($cc) < 9) {

                        foreach ($cc as $carbonC) {
                            $mail->addCC($carbonC);
                        }
                    } else {
                        exit ('Max 10 Recipients Per Email');
                    }

                } else {
                    $mail->addCC($email['cc']);
                }
                //$mail->addBCC('bcc@example.com');

                $mail->WordWrap = 50; // Set word wrap to 50 characters

                if(is_uploaded_file($_FILES['attach']['tmp_name'])) {
                    $file = $_FILES['attach']['name'];
                    $mail->AddAttachment($file);

                }


                /*
                if(isset($_FILES['uploaded_file']) &&  $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK){

                    $mail->addAttachment($_FILES['uploaded_file']['tmp_name'],
                                         $_FILES['uploaded_file']['name']);

                }
*/
                        // Add attachments



                //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
                $mail->isHTML(true); // Set email format to HTML

                $mail->Subject = $email['subject'];
                $mail->Body = $email['HTML'];
                //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
                //$mail->SMTPDebug =1;
                if (!$mail->send()) {
                    header("Location: ../saved_emails.php?error2");
                    //echo 'Message could not be sent.';
                    //echo 'Mailer Error: ' . $mail->ErrorInfo;
                    exit;
                }
            }
        }
    }

Can someone help me to spot a mistake or direct to a decent source of information....?

When attaching a file via a form add enctype="multipart/form-data" attribute to the form tag. I think the problem occurs because of this.

<form action="php/smtp_saved.php" method="post" enctype="multipart/form-data">
     <input type="file" name="attach" id="attach" />
</form>

After a little search, I have found this: Both temp_name and name keys for the attached file are used in AddAttachment() function:

if(isset($_FILES['attach']) && $_FILES['attach']['error'] == UPLOAD_ERR_OK) {

    $mail->AddAttachment($_FILES['attach']['tmp_name'], $_FILES['attach']['name']);
}

$file = $_FILES['attach']['name']; should be $file = $_FILES['attach']['tmp_name']; . The actual file data is in $_FILES['attach']['tmp_name'] . $_FILES['attach']['name'] is just the name given to the uploaded file on the client side.

OK guys Want to thx all of you who tried to help me and pushed me to the solution, I managed to fix it this is what I had to do...

 if(is_uploaded_file($_FILES['attach']['tmp_name'])) {
                $file = $_FILES['attach']['tmp_name'];
                $name = $_FILES['attach']['name'];
                $mail->AddAttachment($file, $name);

            }

:)))))))))))))))))))))))))))))))))))))))))

Works like a charm, just checked the PHPMailer Documentation and the AddAttachment Method needs two parameters,

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