#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Multipart Email. not sending mail


    Hey guys,

    I cant seem to find any errors in my code. what i am trying to do is send an email with an attachment. please have a look at my code and tell me know what you think.


    PHP Code:
    <?php
        $cname                 
    $_POST['cname'];
        
    $caddress1             $_POST['caddress1'];
        
    $curl                 $_POST['curl'];
        
    $caddress2             $_POST['caddress2'];
        
    $cphone             $_POST['cphone'];
        
    $cphoneext             $_POST['cphoneext'];
        
    $ccity                 $_POST['ccity'];
        
    $cstate             $_POST['pwphone'];
        
    $cstate             $_POST['cstate'];
        
    $mphone             $_POST['mphone'];
        
    $czip                 $_POST['czip'];
        
    $fax                 $_POST['fax'];
        
    $fname                 $_POST['fname'];
        
    $lname                 $_POST['lname'];
        
    $namemi             $_POST['namemi'];
        
    $email                 $_POST['email'];
        
    $cbsb                 $_POST['cbsb'];
        
    $cbna                 $_POST['cbna'];
        
    $plicense             $_POST['plicense'];
        
    $plstate             $_POST['plstate'];
        
    $plnum                 $_POST['plnum'];
        
    $plexpdate             $_POST['plexpdate'];
        
    $plyexp             $_POST['plyexp'];
        
    $slicense             $_POST['slicense'];
        
    $slstate             $_POST['slstate'];
        
    $slnum                 $_POST['slnum'];
        
    $slexpdate             $_POST['slexpdate'];
        
    $slyexp             $_POST['slyexp'];
        
    $specials            $_POST['specials'];
        
    $comments             $_POST['comments'];
        
    $zipcodes             $_POST['zipcodes'];
        
    $counties             $_POST['counties'];
        
    $milesfromoffice     $_POST['milesfromoffice'];
        
    $cbrpm                 $_POST['cbrpm'];
        
    $cbres                 $_POST['cbres'];
        
    $cbss                 $_POST['cbss'];
        
    $cbrl                 $_POST['cbrl'];
        
    $cbec                 $_POST['cbec'];
        
    $cbloc                 $_POST['cbloc'];
        
    $cbrack             $_POST['cbrack'];
        
    $cbinsp             $_POST['cbinsp'];
        
    $cbpps                 $_POST['cbpps'];
        
    $cbpcr                 $_POST['cbpcr'];
        
    $cbbpo                 $_POST['cbbpo'];
        
    $cboc                 $_POST['cboc'];
        
    $cbsrs                 $_POST['cbsrs'];
        
    $cblm                 $_POST['cblm'];
        
    $cbvara             $_POST['cbvara'];
        
    $cbcust1             $_POST['cbcust1'];
        
    $tbcust1             $_POST['tbcust1'];
        
    $cbcust2             $_POST['cbcust2'];
        
    $tbcust2             $_POST['tbcust2'];
        
    $cbcust3             $_POST['cbcust3'];
        
    $tbcust3             $_POST['tbcust3'];
        
    $cbcust4             $_POST['cbcust4'];
        
    $tbcust4             $_POST['tbcust4'];
        
    $cbcust5             $_POST['cbcust5'];
        
    $tbcust5             $_POST['tbcust5'];
        
    $cbcust6             $_POST['cbcust6'];
        
    $tbcust6             $_POST['tbcust6'];
        
    $cbcust7             $_POST['cbcust7'];
        
    $tbcust7             $_POST['tbcust7'];
        
    $cbcust8             $_POST['cbcust8'];
        
    $tbcust8             $_POST['tbcust8'];
        
    $heard                 $_POST['heard'];
        
    $numprops             $_POST['numprops'];
        
        
    $htmlbody "Company Name: ".$cname."<br/>";
        
    $htmlbody .= "Company Address 1: ".$caddress1."<br/>";
        
    $htmlbody .= "Company Address 2: ".$caddress2."<br/>";
        
    $htmlbody .= "Company URL: ".$curl."<br/>";
        
    $htmlbody .= "Company Phone: ".$cphone."<br/>";
        
    $htmlbody .= "Company Phone Ext: ".$cphoneext."<br/>";
        
    $htmlbody .= "Company City: ".$ccity."<br/>";
        
    $htmlbody .= "Company State: ".$cstate."<br/>";
        
    $htmlbody .= "Company Work Phone: ".$pwphone."<br/>";
        
    $htmlbody .= "Company Mobile Phone: ".$mphone."<br/>";
        
    $htmlbody .= "Company Zip: ".$czip."<br/>";
        
    $htmlbody .= "Company Fax: ".$fax."<br/>";
        
    $htmlbody .= "First Name: ".$fname."<br/>";
        
    $htmlbody .= "Last Name: ".$lname."<br/>";
        
    $htmlbody .= "Middle Initial: ".$namemi."<br/>";
        
    $htmlbody .= "Email: ".$email."<br/>";
        
    $htmlbody .= "Sponsoring Broker: ".$cbsb."<br/>";
        
    $htmlbody .= "Agent needing Broker Approval: ".$cbna."<br/>";
        
    $htmlbody .= "License type - Primary: ".$plicense."<br/>";
        
    $htmlbody .= "State of Issuance: ".$plstate."<br/>";
        
    $htmlbody .= "License Number: ".$plnum."<br/>";
        
    $htmlbody .= "Expiration Date: ".$plexpdate."<br/>";
        
    $htmlbody .= "Years of Experience: ".$plyexp."<br/>";
        
    $htmlbody .= "License type - Secondary: ".$slicense."<br/>";
        
    $htmlbody .= "State of Issuance: ".$slstate."<br/>";
        
    $htmlbody .= "License Number: ".$slnum ."<br/>";
        
    $htmlbody .= "Expiration Date: ".$slexpdate ."<br/>";
        
    $htmlbody .= "Years of Experience: ".$slyexp ."<br/>";
        
    $htmlbody .= "Professional Designations and Specializations: ".$specials."<br/>";
        
    $htmlbody .= "Comments Regarding Services: ".$comments ."<br/>";
        
    $htmlbody .= "Zip Codes: ".$zipcodes ."<br/>";
        
    $htmlbody .= "Counties: ".$counties ."<br/>";
        
    $htmlbody .= "Miles from Office: ".$milesfromoffice ."<br/>";
        
    $htmlbody .= "Residential Property Management: ".$cbrpm ."<br/>";
        
    $htmlbody .= "Real Estate Sales: ".$cbres ."<br/>";
        
    $htmlbody .= "Short Sales: ".$cbss ."<br/>";
        
    $htmlbody .= "Residential Leasing: ".$cbrl ."<br/>";
        
    $htmlbody .= "Eviction Coordination: ".$cbec ."<br/>";
        
    $htmlbody .= "Lock-Out Coordination: ".$cbloc ."<br/>";
        
    $htmlbody .= "Relocation Assistance -- Cash for Keys: ".$cbrack ."<br/>";
        
    $htmlbody .= "Property/Occupancy Inspections: ".$cbinsp ."<br/>";
        
    $htmlbody .= "Property Preservation Services: ".$cbpps ."<br/>";
        
    $htmlbody .= "Property Condition Reports: ".$cbpcr ."<br/>";
        
    $htmlbody .= "Broker Price Opinion (BPO) and Appraisal Coordination: ".$cbbpo ."<br/>";
        
    $htmlbody .= "Occupancy Checks: ".$cboc ."<br/>";
        
    $htmlbody .= "Securing and Rekeying Services: ".$cbsrs ."<br/>";
        
    $htmlbody .= "Loss Mitigation: ".$cblm ."<br/>";
        
    $htmlbody .= "Value Added Repair Analysis: ".$cbvara ."<br/>";
        
    $htmlbody .= "Other : ".$cbcust1 ."<br/>";
        
    $htmlbody .= "Other : ".$tbcust1  ."<br/>";
        
    $htmlbody .= "Other : ".$cbcust2 ."<br/>";
        
    $htmlbody .= "Other : ".$tbcust2 ."<br/>";
        
    $htmlbody .= "Other : ".$cbcust3 ."<br/>";
        
    $htmlbody .= "Other : ".$tbcust3 ."<br/>";
        
    $htmlbody .= "Other : ".$cbcust4 ."<br/>";
        
    $htmlbody .= "Other : ".$tbcust4 ."<br/>";
        
    $htmlbody .= "Other : ".$cbcust5 ."<br/>";
        
    $htmlbody .= "Other : ".$tbcust5 ."<br/>";
        
    $htmlbody .= "Other : ".$cbcust6 ."<br/>";
        
    $htmlbody .= "Other : ".$tbcust6 ."<br/>";
        
    $htmlbody .= "Other : ".$cbcust7 ."<br/>";
        
    $htmlbody .= "Other : ".$tbcust7 ."<br/>";
        
    $htmlbody .= "Other : ".$cbcust8 ."<br/>";
        
    $htmlbody .= "Other : ".$tbcust8 ."<br/>";
        
    $htmlbody .= "Other : ".$heard ."<br/>";
        
    $htmlbody .= "Other : ".$numprops ."<br/>";
        
        
        
    //Set Recipient Address
        
    $to "email@hotmail.com";
        
    //Set Recipient Address
        
    $from "email@rmsnav.com";
        
    //Set Email Subject
        
    $subject "HTML email with attachment";
        
        
        
        
    //read the atachment file contents into a string,
        //encode it with MIME base64,
        //and split it into smaller chunks for sending
        
        
    if(!empty($numprops)){
        
            
    $file_name $_FILES['w9file ']['name'];
            
    $temp_name $_FILES['w9file ']['temp_name'];
            
    $file_type $_FILES['w9file ']['type'];
            
            
    //get the extenstion of the file
            
    $base basename($file_name);
            
    $extension substr($basestrlen($base)-4strlen($base));
            
            
    // only these files types will be allowed
            
    $allowed_extensions = array(".doc","docx",".pdf",".zip",".png",".xls",".jpg",".jpeg");
            
            
    //this is just for namesing
            
    $file $file_name;
            
    $contents chunk_split(base64_encode(file_get_contents($file)));
            
    $uid md5(uniqid(time()));
            
            
    //define the from reply to headers
            
    $headers "From: ".$from."\r\n";
            
    $headers .= "Reply-To: ".$to."\r\n";
            
    $headers .= "MIME-Version: 1.0\r\n";
            
            
    //define the body of the message.
            
    $headers .="Content-Type: multipart/alternative; boundary=\"".$uid."\"r\n\r\n";
            
    $headers .= "This is a multi-part message in MIME format. \r\n";
            
    $headers .= "Content-type:text/plain; charset=iso-8859-1\r\n";
            
    $headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
            
    $headers .= $htmlbody."\"r\n\r\n";
            
            
    //file attachment
            
    $headers .= "--".$uid."\r\n";
            
    $headers .= "Content-Type: ".$file_type."; name=\"".$file_name."\"r\n\r\n";
            
    $headers .= "Content-Transfer-Encoding: base64\r\n";
            
    $headers .= "Content-Disposition: attachment; filename=\"".$file_name."\"r\n\r\n";
            
    $headers .= $content."\"r\n\r\n";
            
            
            
            
    //send the email
            
    if(mail$to$subject ,""$headers));
             {
                echo 
    "Mail sent!".$headers;
            }
            
        }
        
        else{
            echo 
    "Mail not sent!";
            }
    ?>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    1. The [name] is just the name of the file as it was on the user's computer. It is not where the file is actually located (temporarily) on the server.
    2. The [tmp_name], which is where the file is located, only has one "e" in it.
    3. You have some funky \"r\n\r\n things in there.
    4. The headers to mail() are the headers to the overall message. The message content is where you put the HTML and the multipart stuff.

    5. Since you aren't fully aware of how the whole thing works, don't roll your own solution: go with something like PHPMailer instead.
    Last edited by ManiacDan; December 12th, 2012 at 09:40 AM.

IMN logo majestic logo threadwatch logo seochat tools logo