4

I am learning to use SwiftMailer and I am having trouble getting the attachment to work from a input type. Shouldn't I only need:

<input name="attachment" id="attachment" type="file">

then

$message = Swift_Message::newInstance()
->attach(new Swift_Message_Attachment(new Swift_File($file), $filename,));

Here is my full code

<?php
if (!empty($_POST)) {

$success = $error = false;

$post = new stdClass;

$file = $_FILES["attachment"]["tmp_name"]; //"attachment" is the name of your input field, "tmp_name" gets the temporary path to the uploaded file.

$filename = $_FILES["attachment"]["name"]; //"name" gets the filename of the uploaded file.         

foreach ($_POST as $key => $val)
    $post->$key = trim(strip_tags($_POST[$key]));

    if (empty($post->name) OR empty($post->email))
    $error = true;

else {

    $dir = dirname(__FILE__);

    ob_start();
    require_once($dir.'/pdf.php');
    $pdf_html = ob_get_contents();
    ob_end_clean();

    require_once($dir.'/dompdf/dompdf_config.inc.php');

    $dompdf = new DOMPDF();
    $dompdf->load_html($pdf_html);
    $dompdf->render();
    $pdf_content = $dompdf->output();

    ob_start();
    require_once($dir.'/html.php');
    $html_message = ob_get_contents();
    ob_end_clean();

    require_once($dir.'/swift/swift_required.php');

    $mailer = new Swift_Mailer(new Swift_MailTransport());

    $message = Swift_Message::newInstance()
                   ->setSubject('Order Entry') // Message subject
                   ->setTo(array('ehilse@paifashion.com' => 'Eric Hilse')) // Array of people to send to
                   ->setFrom(array('no-reply@paifashion.com' => 'PAi Order Entry'))
                   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                   ->attach(Swift_Attachment::newInstance($pdf_content, 'design.pdf', 'application/pdf')); // Attach the generated PDF from earlier
                   ->attach(new Swift_Message_Attachment(new Swift_File($file), $filename));
    // Send the email, and show user message
    if ($mailer->send($message))
        $success = true;
    else
        $error = true;

}

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>PAi Cap &amp; Tee Art Pack Order Form</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="container">
            <div id="logo">
                <img src="images/pailogo.png" id="logo" />
            </div>
        <h1><abbr title="Paramount Apparel International">PAi</abbr> Cap &amp; Tee Art Pack Order Form</h1>

        <?php if ($success) { ?>
            <div class="message success">
                <h4>Congratulations! It worked! Your order has been sent.</h4>
            </div>
        <?php } elseif ($error) { ?>
            <div class="message error">
                <h4>Sorry, an error occurred. Try again!</h4>
            </div>
        <?php } ?>

        <form method="post" action="" enctype="multipart/form-data">
            <fieldset>
                <legend>Please fill in the following form:</legend>
                    <label for="name">Your Name:<span class="required">*</span></label>
                    <input type="text" name="name" id="name" class="input" />

                    <label for="email">Your Email:<span class="required">*</span></label>
                    <input type="text" name="email" id="email" class="input" />     

        <br />

                    <label for="artpacknumber">Art Pack Number:<span class="required">*</span></label>
                    <input type="text" name="artpacknumber" id="artpacknumber" class="input" />         

                    <label for="language">New Or Revise?<span class="required">*</span></label>
                    <select name="language" id="language">
                        <option value="NEW">New</option>
                        <option value="REVISE">Revise</option>
                    </select>

        <br />  

                    <label for="duedate">Due Date:<span class="required">*</span></label>
                    <input type="text" name="duedate" id="duedate" class="input" />

                    <label for="dateentered">Date Entered:<span class="required">*</span></label>
                    <input type="text" name="dateentered" id="dateentered" class="input" />

        <br />

                    <label for="designname">Design Name:<span class="required">*</span></label>
                    <input type="text" name="designname" id="designname" class="input" />

        <br />

                    <label for="customer">Customer:<span class="required">*</span></label>
                    <input type="text" name="customer" id="customer" class="input" />

                    <label for="account">Account:<span class="required">*</span></label>
                    <input type="text" name="account" id="account" class="input" />

        <br />

                    <label for="salesrep">Sales Rep:<span class="required">*</span></label>
                    <input type="text" name="salesrep" id="salesrep" class="input" />

                    <label for="extension">Extension:<span class="required">*</span></label>
                    <input type="text" name="extension" id="extension" class="input" />

        <br />

                    <label class="commentslabel" for="comments">Comments:<span class="required">*</span></label>
                    <textarea name="comments" id="comments" rows="4" cols="40"></textarea>

        <br />

                    <label for="attachment">File Upload<br />.</label>
                    <input name="attachment" id="attachment" type="file">

        <br />          

                    <input type="submit" class="submit" id="submit" value="Submit" />

        </fieldset>

    </form>

</div>

Here is my new errors [27-Dec-2011 14:51:58] PHP Deprecated: Function set_magic_quotes_runtime() is deprecated in /Applications/MAMP/htdocs/paipdf2/dompdf/lib/class.pdf.php on line 4332

[27-Dec-2011 14:51:58] PHP Deprecated: Function set_magic_quotes_runtime() is deprecated in /Applications/MAMP/htdocs/paipdf2/dompdf/lib/class.pdf.php on line 4348

[27-Dec-2011 14:51:58] PHP Warning: require_once(/Applications/MAMP/htdocs/paipdf2/dompdf/include/swift_message_attachment.cls.php) [function.require-once]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/paipdf2/dompdf/dompdf_config.inc.php on line 194

[27-Dec-2011 14:51:58] PHP Fatal error: require_once() [function.require]: Failed opening required '/Applications/MAMP/htdocs/paipdf2/dompdf/include/swift_message_attachment.cls.php' (include_path='.:/Applications/MAMP/bin/php/php5.3.6/lib/php') in /Applications/MAMP/htdocs/paipdf2/dompdf/dompdf_config.inc.php on line 194

ehilse
  • 129
  • 1
  • 2
  • 10
  • 3
    possible duplicate of [Swift Mailer attachments](http://stackoverflow.com/questions/4667822/swift-mailer-attachments) - to understand where an uploaded file goes to, please see: http://php.net/manual/en/features.file-upload.php – hakre Dec 27 '11 at 17:58

2 Answers2

1

well, there is a problem when including DOMpdf before you include Swiftmailer.

I have not found out why it's wrong, but I do have a solution.

Change this peace of code in 'dompdf_config.inc.php':

function DOMPDF_autoload($class) {
  $filename = mb_strtolower($class) . ".cls.php";
  require_once(DOMPDF_INC_DIR . "/$filename");
}

to this:

function DOMPDF_autoload($class) {
  $filename = mb_strtolower($class) . ".cls.php";
  if (file_exists(DOMPDF_INC_DIR . "/$filename")) {
    require_once(DOMPDF_INC_DIR . "/$filename");
  }
}

Afther that change it works and you can mail your pdf with swiftmailer.

extra information. How I start DomPDF:

require_once("lib/dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload'); 
$dompdf = new DOMPDF();
$dompdf->load_html($pdfhtml);
$dompdf->set_paper('a4', 'portrait');
$dompdf->render();
$dompdf->stream("invoice", array('Attachment'=>0));
Bo Pennings
  • 945
  • 1
  • 10
  • 20
1

I'm guessing $file and $filename don't carry the actual value of the file path and name.

Assuming the form posting is done correctly, to access the path of an uploaded file you have to use the $_FILES superglobal array, like so:

$file = $_FILES["attachment"]["tmp_name"]; //"attachment" is the name of your input field, "tmp_name" gets the temporary path to the uploaded file.
$filename = $_FILES["attachment"]["name"]; //"name" gets the filename of the uploaded file.
$message = Swift_Message::newInstance()
->attach(new Swift_Message_Attachment(new Swift_File($file), $filename));

Documentation: http://www.php.net/manual/en/reserved.variables.files.php

Telmo Marques
  • 5,066
  • 1
  • 24
  • 34
  • I added my full code with what you said and for some reason it broke it completely. Can you tell what I did wrong? – ehilse Dec 27 '11 at 19:24
  • Are you sure your code is moving past by the second `if` statement? Also, if any error's or warning are being shown please share those too. – Telmo Marques Dec 27 '11 at 19:47
  • This is the only error I received PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /Applications/MAMP/htdocs/paipdf2/form.php on line 49 – ehilse Dec 27 '11 at 20:05
  • You have an extra semicolon at line 49 that shouldn't be there. Delete it and see what happens. – Telmo Marques Dec 27 '11 at 20:34
  • Alright I took it out and now it gave me 2 new errors PHP Notice: Undefined variable: success in /Applications/MAMP/htdocs/paipdf2/form.php on line 77 and Undefined variable: error in /Applications/MAMP/htdocs/paipdf2/form.php on line 81 – ehilse Dec 27 '11 at 20:42
  • I figured out why I got those errors and it was because my file i tested was too large. After I tried a different file I get different errors now. Which looks like its coming from the part that creates the pdf... I added them to my original post – ehilse Dec 27 '11 at 20:57
  • If the error's that are thrown are those you added in the question above then it's failing to load files that are being required in your application... Read the errors carefully, see where they are being "required" (included) and make sure the included path's are right. – Telmo Marques Dec 27 '11 at 21:28
  • I dont understand how it would work before without the upload input and now that I added it wont work. Also I dont know why that attachment would be affected by the one it creates with the rest of the form. – ehilse Dec 27 '11 at 21:48
  • I got the file to attach now but for some reason when its emailed to me its corrupted. Do you know what could cause that? – ehilse Dec 27 '11 at 22:00
  • Re-check the documentation for the Swiftmailer library you're using, and also re-check if the file is being uploaded and if it's being accessed correctly with the $_FILES superglobal. If it isn't a bug in Swiftmailer then there must be something wrong with the code. – Telmo Marques Dec 27 '11 at 22:11