SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2003
    Location
    London
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Advanced mail: Attachment?!!?! (I read Yanks article)

    Hello, I hope somebody maybe able to help. I am trying to attach files to an email sent using the php mail function. I've read Kevin Yanks article and used the code:

    // Obtain file upload vars
    $fileatt = $_FILES['fileatt']['tmp_name'];
    $fileatt_type = $_FILES['fileatt']['type'];
    $fileatt_name = $_FILES['fileatt']['name'];

    if (is_uploaded_file($fileatt)) {
    // Read the file to be attached ('rb' = read binary)
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);

    // Generate a boundary string
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    // Add the headers for a file attachment
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    // Add a multipart boundary above the plain message
    $message1 = "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $message1 . "\n\n";

    // Base64 encode the file data
    $data = chunk_split(base64_encode($data));
    ///base64
    // Add file attachment to the message
    $message1 .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";

    How ever when I then receive the email in my yahoo account the attachment is all numbers and symbols:

    MIME-Version: 1.0
    Content-Type: multipart/mixed;
    boundary="==Multipart_Boundary_x10ba5951a29b6a4a526623f1c42271e6x"

    Dear GROUP1

    This is a multi-part message in MIME format.

    --==Multipart_Boundary_x10ba5951a29b6a4a526623f1c42271e6x
    Content-Type: text/plain; charset="iso-8859-1"
    Content-Transfer-Encoding: 7bit

    zzzzzzzzzzz

    --==Multipart_Boundary_x10ba5951a29b6a4a526623f1c42271e6x
    Content-Type: image/pjpeg;
    name="cov.jpg"
    Content-Disposition: attachment;
    filename="cov.jpg"
    Content-Transfer-Encoding: base64

    /9j/4AAQSkZJRgABAgEBLAEsAAD/4RN0RXhpZgAASUkqAAgAAAALAA4BAgALAAAAkgAAAA8BAgAG
    AAAAnQAAABABAgAGAAAAowAAABIBAwABAAAAAQAAABoBBQABAAAAqQAAABsBBQABAAAAsQAAACgB
    AwABAAAAAgAAADEBAgAUAAAAuQAAADIBAgAUAAAAzQAAABMCAwABAAAAAgAAAGmHBAABAAAA5AAA
    AFQDAAAgICAgICAgICAgAE5JS09OAEUzMTAwACwBAAABAAAALAEAAAEAAABBZG9iZSBQaG90b3No
    b3AgNy4wADIwMDQ6MTA6MDggMTY6NDU6NTkAAAAAIQCaggUAAQAAAHYCAACdggUAAQAAAH4CAAAi
    iAMAAQAAAAIAAAAniAMAAQAAAGQAAAAAkAcABAAAADAyMjADkAIAFAAAAIYCAAAEkAIAFAAAAJoC
    AAABkQcABAAAAAECAwACkQUAAQAAAK4CAAAEkgoAAQAAALYCAAAFkgUAAQAAAL4CAAAHkgMAAQAA
    AAUAAAAIkgMAAQAAAAAAAAAJkgMAAQAAABAAAAAKkgUAAQAAAMYCAACGkgcAfQAAAM4CAAAAoAcA (IT GOES ON!!!!!!!!!!!!!!!!!)


    -----------------

    Please advise me as to what alterations are needed in the code to make the attachment be recognised as what it is (picture/word file/etc).

    Thanks for your time and help!

    Lindsay

  2. #2
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What kind of file are you trying to attach? I just used Kevin's script with both .txt and Word document attachments, and it worked just fine for me. Both attachments were readable.

    I sent mail to my own Yahoo account, just to make sure there wasn't something funky with the particular mail server it's being sent to.

    I assume you're using this script in conjunction with the HTML form that goes with it?

    Try it again and see if you get the same result.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  3. #3
    SitePoint Member
    Join Date
    Jun 2003
    Location
    London
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Vihyl-junkie... thanks for your message. My form was just on one page, however when I tried the script as is (on the two pages (the form and the php mail)) it worked! So now I've managed to work it in to my mailing list script... CRISIS OVER!!!!!

    Many thanks for your reply!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •