1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with PHP mail()!!

Discussion in 'PHP' started by jorgy, Dec 2, 2008.

  1. #1
    I am trying to use the php mail() function, something I am fairly familiar with, to send an attachment. I've tried to do this using multiple different methods, and each time the mail simply never reaches its destination. Here is the code I currently have:

    
    <?php
    $to = "Eric <myemail@gmail.com>";
    $from = "Eric <myemail@gmail.com>";
    $subject = "Attach Me NT";
    
    $fileatt = "MyFile.exe";
    $fileatttype = "application/exe";
    $fileattname = "newname.exe";
    
    $headers = "From: $from";
    
    $file = fopen($fileatt, 'rb' );
    $data = fread($file, filesize($fileatt));
    $fclose;
    
           $semi_rand = md5( time() ); 
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
        
            $headers .= "\nMIME-Version: 1.0\n" . 
                        "Content-Type: multipart/mixed;\n" . 
                        " boundary=\"{$mime_boundary}\"";
        
            $message = "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" . 
                    $message . "\n\n";
        
            $data = chunk_split( base64_encode( $data ) );
                     
            $message .= "--{$mime_boundary}\n" . 
                     "Content-Type: {$fileatttype};\n" . 
                     " name=\"{$fileattname}\"\n" . 
                     "Content-Disposition: attachment;\n" . 
                     " filename=\"{$fileattname}\"\n" . 
                     "Content-Transfer-Encoding: base64\n\n" . 
                     $data . "\n\n" . 
                     "--{$mime_boundary}--\n"; 
                     
    if(mail($to, $subject, $message, $headers)) {
        echo "Done";
    }
    else {
        echo "Not done.";
    }
    ?> 
    
    PHP:
    Please help!
     
    jorgy, Dec 2, 2008 IP
  2. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I don't mean to sway from your original question, but would it not be easier to get the script to upload it on your server and then mail a link?
     
    qualityfirst, Dec 2, 2008 IP
  3. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's quite possible the email is getting sent, but the address you are sending it to get is blocking it, most likely due to the .exe file you are trying to attach. Google (and most other programs) will see that as spam and automatically delete it.
     
    Christian Little, Dec 2, 2008 IP
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4
    What Christian said is very true. All the hosts that I have had to date block all emails containing anything that remotely looks like an executable.

    Can you convert the file to a zip file and try sending that?
     
    Colbyt, Dec 2, 2008 IP