PHP email form with attachments?

123578

Comments

  • LincLinc Owner Detroit Icrontian
    edited December 2007
    "new" is a keyword for instantiating classes... that's the error killing your script. All you need is the word "array" there. I'm also not sure that substr can handle arrays as input anyway... I don't see anything in the documentation about it.

    You want the pathinfo function.

    [php]
    $path = pathinfo($_FILES);
    if($path!="jpg" && $path!="gif" && $path!="png") {
    //unlink, die
    }
    [/php]As an aside, I also recommend either allowing or converting "jpeg" since sometimes that crops up as an alternate jpg extension.
  • edited December 2007
    :D Thanks Keebler, I got that to work!
  • LincLinc Owner Detroit Icrontian
    edited December 2007
    Sure thing :) Glad to hear.
  • edited January 2008
    Kwitko wrote:
    You're welcome, MMN.


    Hi, I am a novice at web-design and recently created a web site for a cat-rescue organization. In order to help our volunteers send me information about the cats we have for adoption, I need an e-mail form in which they can send me brief information as well as a picture of the cat/kitten. I have the form working, except the image is showing up as text. I read previous posts from people who had this issue, but since I am new to all of this, especially to PHP, I really just got even more confused. Can anyone help me please? Below is the code I am currently using.

    Thanks,
    Lauren


    [HTML]
    <form name="Adoptables" method="post" action="Adoptables.php" enctype="multipart/form-data">
    <input type="hidden" name="To" value="lauren@luckycatrescue.com">

    ******** language='javascript'>
    function verifyMe(){
    var msg='';

    if(document.getElementById('Volunteer').value==''){
    msg+='- Your Name\n\n';}

    if(document.getElementById('Volunteer_Email').value==''){
    msg+='- Your E-mail Address\n\n';}

    if(document.getElementById('Cat_or_Kitten').value==''){
    msg+='- Are you submitting a cat or kitten?\n\n';}

    if(document.getElementById('Name').value==''){
    msg+='- Name of Cat or Kitten\n\n';}

    if(document.getElementById('Gender').value==''){
    msg+='- Gender\n\n';}

    if(document.getElementById('Age').value==''){
    msg+='- Age\n\n';}

    if(document.getElementById('Color').value==''){
    msg+='- Color\n\n';}

    if(document.getElementById('Personality').value==''){
    msg+='- Brief description of personality\n\n';}

    if(document.getElementById('Story').value==''){
    msg+='- Story\n\n';}

    if(document.getElementById('Special_Needs').value==''){
    msg+='- Is this a special needs cat?\n\n';}

    if(document.getElementById('Picture').value==''){
    msg+='- Picture of Cat or Kitten\n\n';}

    if(msg!=''){
    alert('The following fields are empty or invalid:\n\n'+msg);
    return false
    }else{
    return true }

    }
    *********>
    <form name='Adoptables' action='action=\"Adoptables.php\"' method='POST' enctype='application/x-www-form-urlencoded' onsubmit='return verifyMe();'>
    <table class='table_form_1' id='table_form_1' cellspacing='0'>
    <tr>
    <td class='ftbl_row_1' ><LABEL for='Volunteer' ACCESSKEY='none' ><b style='color:red'>*</b>Your Name
    </td>
    <td class='ftbl_row_1a' ><input type='text' name='Volunteer' id='Volunteer' size='45' value=''>
    </td>
    </tr>

    <tr>
    <td class='ftbl_row_2' ><LABEL for='Volunteer_Email' ACCESSKEY='none' ><b style='color:red'>*</b>Your E-mail Address
    </td>
    <td class='t3r2c1' ><input type='text' name='Volunteer_Email' id='Volunteer_Email' size='45' value=''>
    </td>
    </tr>
    <tr>
    <td class='ftbl_row_1' ><LABEL for='Cat_or_Kitten' ACCESSKEY='none' ><b style='color:red'>*</b>Are you submitting a cat or kitten?
    </td>

    <td class='ftbl_row_1a' ><select name='Cat_or_Kitten' id='Cat_or_Kitten'>
    <option value='---'>---</option>
    <option value=' Kitten (less than 1 year old)'> Kitten (less than 1 year old)</option>
    <option value=' Cat (more than 1 year old)'> Cat (more than 1 year old)</option>
    </select>
    </td>

    </tr>
    <tr>
    <td class='ftbl_row_2' ><LABEL for='Name' ACCESSKEY='none' ><b style='color:red'>*</b>Name of Cat or Kitten
    </td>
    <td class='t3r2c1' ><input type='text' name='Name' id='Name' size='45' value=''>
    </td>
    </tr>
    <tr>
    <td class='ftbl_row_1' ><LABEL for='Gender' ACCESSKEY='none' ><b style='color:red'>*</b>Gender
    </td>

    <td class='ftbl_row_1a' ><select name='Gender' id='Gender'>
    <option value='---'>---</option>
    <option value=' Male'> Male</option>
    <option value=' Female'> Female</option>
    </select>
    </td>

    </tr>
    <tr>
    <td class='ftbl_row_2' ><LABEL for='Age' ACCESSKEY='none' ><b style='color:red'>*</b>Age
    </td>
    <td class='t3r2c1' ><input type='text' name='Age' id='Age' size='45' value=''>
    </td>
    </tr>
    <tr>
    <td class='ftbl_row_1' ><LABEL for='Color' ACCESSKEY='none' ><b style='color:red'>*</b>Color
    </td>

    <td class='ftbl_row_1a' ><input type='text' name='Color' id='Color' size='45' value=''>
    </td>
    </tr>
    <tr>
    <td class='ftbl_row_2' ><LABEL for='Personality' ACCESSKEY='none' ><b style='color:red'>*</b>Brief description of personality
    </td>
    <td class='t3r2c1' ><input type='text' name='Personality' id='Personality' size='45' maxlength='200' value=''>
    </td>
    </tr>

    <tr>
    <td class='ftbl_row_1' ><LABEL for='Story' ACCESSKEY='none' >Story
    </td>
    <td class='ftbl_row_1a' ><input type='text' name='Story' id='Story' size='45' maxlength='300' value=''>
    </td>
    </tr>
    <tr>
    <td class='ftbl_row_2' ><LABEL for='Special_Needs' ACCESSKEY='none' ><b style='color:red'>*</b>Is this a special needs cat?
    </td>

    <td class='t3r2c1' ><select name='Special_Needs' id='Special_Needs'>
    <option value='---'>---</option>
    <option value=' Yes'> Yes</option>
    <option value=' No'> No</option>
    </select>
    </td>

    </tr>
    <tr>
    <td class='ftbl_row_1' ><LABEL for='Picture' ACCESSKEY='none' ><b style='color:red'>*</b>Picture of Cat or Kitten
    </td>
    <td class='ftbl_row_1a' ><input type='file' name='Picture' id='Picture' size='45' value=''>
    </td>
    </tr>
    <tr>
    <td colspan='2' align='center'><input type='submit' name='submit' value='Submit'> <input type='reset' name='reset' value='Reset'><br />

    </td>
    </tr>
    </table>
    </form>[/HTML]



    [PHP]


    <?php
    // FORM MAILER

    $domain_name = "LuckyCatRescue.com";
    $today = getdate();
    $month = $today;
    $mday = $today;
    $year = $today;

    /* recipients */
    $to = "lauren@luckycatrescue.com";

    /* subject */
    $subject = "Web Update - ($month $mday, $year)";

    /* message */
    $message = "The following information was submitted to $domain_name on $month $mday, $year.\n\n";
    while(list($key, $value) = each($HTTP_POST_VARS)) {
    if (strlen($value) > 0) {
    $title = strtr(strtoupper($key),"_"," ");
    $message .= "$title: $value\n";
    }
    }

    $message .= "\n";
    $message .= "\n";

    /* headers */
    $headers = "From: $domain_name <automailer@$domain_name>\n";
    $headers .= "Cc: Sally <>\n";

    /* and now mail it */
    mail($to, $subject, $message, $headers);
    ?>
    <html>
    <center><img src="http://luckyc.fatcow.com/graphics/webupdatethanks.jpg"&gt;
    </html>[/PHP]
  • edited January 2008
    [php]


    // FORM MAILER

    $domain_name = "LuckyCatRescue.com";
    $today = getdate();
    $month = $today;
    $mday = $today;
    $year = $today;

    /* recipients */
    $to = "lauren@luckycatrescue.com";

    /* subject */
    $subject = "Web Update - ($month $mday, $year)";

    /* message */
    $message = "The following information was submitted to $domain_name on $month $mday, $year.\n\n";
    while(list($key, $value) = each($HTTP_POST_VARS)) {
    if (strlen($value) > 0) {
    $title = strtr(strtoupper($key),"_"," ");
    $message .= "$title: $value\n";
    }
    }

    $message .= "\n";
    $message .= "\n";

    /* headers */
    $headers = "From: $domain_name \n";
    $headers .= "Cc: Sally <>\n";

    /* and now mail it */
    mail($to, $subject, $message, $headers);
    ?>
    [/php]

    The problem you have here is

    [php]
    while(list($key, $value) = each($HTTP_POST_VARS))
    [/php]loops through all the post variables including the picture but nowhere do you tell the email client that one of these variables is a file so it treats them all as text.

    you need email headers to differentiate the file from the message body, you can use the ones I took from Grimm, you might want to look back at my version of his form where he base64 encoded the file: http://icrontic.com/forum/showthread.php?t=7019&page=6
  • edited April 2008
    Hi,
    anyone? i tried this on my server but the attachment i tried to send with my form mail does not comes in base64 which should turn out to be a file instead of a string of codes inside the mail.
    thanks everyone in advance.

    [php]
    <?php
    $email = $_REQUEST;
    $message = $_REQUEST;
    $msg = $_REQUEST;
    $subject = $_REQUEST;
    $coname = $_REQUEST;
    $user = $_REQUEST;
    $reg = $_REQUEST;
    $ctc = $_REQUEST;
    $xbrldoc = $_REQUEST;
    $doc = $_REQUEST;
    $ver = $_REQUEST;
    $dnt = $_REQUEST;
    $agent = $_REQUEST;
    $attachment = $_FILES;
    $attachment_name = $_FILES;
    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
    $fp = fopen($attachment, "rb"); //Open it
    $data = fread($fp, filesize($attachment)); //Read it
    $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
    fclose($fp);
    }
    mail( "to@mail.com", "$subject", "Hi Daniel, \n\nProblem Description: $msg \n\n\n\nUser's Name: $user \nCo Regn No: $reg \nCompany Name: $coname \nContact No: $ctc \nEmail Address: \n\n\nXBRL Document: $xbrldoc \nText Source Document: $doc \nWeb Browser Version: $ver \nDate/Time Error occured: $dnt \n\n\n\n\nRegards,\n$agent \nHelpdesk \n$data", "From: me@mail.com");
    header( "Location: thankyou.htm" );
    ?>
    [/php]
  • edited June 2008
    Hey guys,

    I was wondering if someone could help me out.

    Using the script provided (1st page), I am able to send emails with attachments and I get the email and attachment intact. However, no body (no message!) shows up on webmail OR Pop3 when I check it.

    This is stock script with minimal modifications. Has anyone experienced this?.. or know of a work-around?
  • edited June 2008
    maybe you want to paste your code here?
  • edited June 2008
    Like I said, its pretty stock. After grabbing it and modifying it didnt work... I kept deleting my modifactions all the way back until I have damn nearly the exact file I pulled from the post on page 1. I upload it, populate, send, it works and sends emails just no body-text output at all.

    Basically, just a static from and a static to email address. Being used as a basic contact form with resume attachment.

    Noticed a couple other people had the same problem earlier on in the thread but never seemed to have gotten a reply.

    [html]



    PHP Code:
    <?php
    /* Mailer with Attachments */
    $action = $_REQUEST;
    global $action;
    function showForm() {
    ?>
    <FORM name=send action="<?=$_SERVER?>" method=post encType=multipart/form-data>
    <INPUT type=hidden value=send name=action>
    <INPUT type=hidden value=10000000 name=MAX_FILE_SIZE>

    Name: <INPUT size=50 name=name>
    Email: <INPUT size=50 name=from_email>
    Subject: <INPUT size=50 name=subject>
    Message: <TEXTAREA name=body rows=10 cols=50></TEXTAREA>
    Attachment: <INPUT type=file size=50 name=attachment>
    <INPUT type=submit value="Send Email">


    <?php
    }
    function sendMail() {
    $to_name = "";
    $from_name = stripslashes($_POST);
    $subject = stripslashes($_POST);
    $body = stripslashes($_POST);
    $to_email = "williamschott@comcast.net";
    $from_email = stripslashes($_POST);
    $phone = stripslashes($_POST);
    //Let's start our headers
    $headers = "From: $from_name <" . $_POST . ">\n";
    $headers .= "Reply-To: <" . $_POST . ">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
    $headers .= "X-Sender: $from_name <" . $_POST . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST . ">\n";
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "
    =MIME_BOUNDRY_main_message \n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
    $message = "
    =MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: quoted-printable\n";
    $message .= "\n";
    $message .= "$body\n";
    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_message_parts--\n";
    $message .= "\n";
    foreach($_FILES as $file => $value) {
    $_tmpname = $_FILES[$file];
    $_filename = $_FILES[$file];
    if (is_uploaded_file($_tmpname)) { //Do we have a file uploaded?
    $fp = fopen($_tmpname, "rb"); //Open it
    $data = fread($fp, filesize($_tmpname)); //Read it
    $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
    $message .= "
    =MIME_BOUNDRY_main_message\n";
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $_filename . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $_filename . "\"\n\n";
    $message .= $data; //The base64 encoded message
    $message .= "\n\n";
    fclose($fp);
    }
    }

    $message .= "
    =MIME_BOUNDRY_main_message--\n";

    // send the message

    $ok = mail("$to_name <$to_email>", $subject, $body, $headers);

    if ($ok == 1) {
    print "Mail sent to $to_name ($to_email).";
    } else {
    print "Mail did not send. Please press the <b>back</b> button and re-submit the e-mail.";
    }
    }
    ?>


    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;


    <HTTP-EQUIV="CONTENT-TYPE" content="text/html; charset=iso-8859-1" />
    <STYLE="CSS" type="text/css">
    <!--
    body {
    margin: 0px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 12px;
    }
    a {color: #0000ff}
    -->
    </STYLE>


    <?php
    switch ($action) {
    case "send":
    sendMail();
    showForm();
    break;
    default:
    showForm();
    }
    ?>






    </FORM>

    [/html]
  • edited June 2008
    did you name the textfield as 'body'?
  • edited June 2008
    ..
  • edited June 2008
    hi Drenkhahn, did u get your attacment work or not?
    cause i m also using same, but nothing for the attacment
    can u explain?
    tks
  • edited June 2008
    Attachment works for me and always works for me.
  • a2jfreaka2jfreak Houston, TX Member
    edited June 2008
    So you do receive the email, but there's nothing in the body? Does the subject show?

    Paste the code you're using inside [ php ] [ /php ] tags (don't have the spaces)
  • edited June 2008
    a2jfreak wrote:
    So you do receive the email, but there's nothing in the body? Does the subject show?

    Paste the code you're using inside [ php ] [ /php ] tags (don't have the spaces)

    Its odd. Tested using outlook 2003 AND webmail. Problem is the same, regarless.

    I installed the same script on two remote hosting sites. My godaddy hosting runs it perfectly. The other no-name fischer-price webhost is the one I have the problems with, so I am sure its not the script's fault. The email from the problematic host shows a working attachment, but no message is displayed.

    So I've given up on it. Unless you know a work around.
    Similarly, I'll end up remote hosting the script and cross linking to it. But i need to work out a redirect to get me back to the origonal site after the script runs.


    header( "Location: back@mysiteafterscriptruns.com" );

    craps out.

    BTW, Im running Kwitko's script pretty much stock (shown partially above). Again, I'm fing out now I think the hosting is just cheap. Script is verified worker elsehwere for me.
  • edited June 2008
    Drenkhahn, not so sure about you but i seperated my process into 2 parts. one the form in html the other the processing in php as below.

    form in html:
    [html]

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Form2Mail</title>
    ****** http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body bgcolor="#ffffff" text="#000000">
    <div>
    <center>
    <form method="post" enctype="multipart/form-data" name="send" action="formprocess.php">
    <table border="0" bgcolor="#ececec" cellspacing="5">
    <tr><td>Name</td><td><input type="text" size="30" name="name"></td></tr>
    <tr><td>Email address</td><td><input type="text" size="30" name="emailaddr"></td></tr>
    <tr><td>Message</td><td><textarea name="message" rows="6" cols="30"></textarea></td></tr>
    <tr><td>Attachment</td><td><input type="file" name="attachment" size="50" /><br /></td></tr>
    <tr><td> </td><td><input type="submit" value="Send"></td></tr>
    </table>
    </form>
    </center>
    </div>
    </body>
    </html>

    [/html]

    here goes the php:
    [php]
    <?php //information processed from form submitted.
    $msg = $_REQUEST;
    $email = $_REQUEST;$name = $_REQUEST;
    $attachment = $_FILES;
    $attachment_name = $_FILES;
    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
    $fp = fopen($attachment, "rb"); //Open it
    $data = fread($fp, filesize($attachment)); //Read it
    $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
    fclose($fp);
    } //Let's start our headers
    $headers = "From: $email\n";
    $headers .= "Reply-To: $email\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
    $headers .= "X-Sender: $name<" . $_POST . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST . ">\n";
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "
    =MIME_BOUNDRY_main_message \n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; $message = "
    =MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: quoted-printable\n";
    $message .= "\n";
    //* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
    $message .= "$msg\n";
    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_message_parts--\n";
    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_main_message\n";
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
    $message .= $data; //The base64 encoded message
    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_main_message--\n"; //final code to send the message
    mail("mymail@my.com", "mailtoform", $message, $headers);
    //the redirection page.
    header("Location: maildone.php");
    ?>
    [/php]
  • edited June 2008
    Yes, of course. I have recently done so as well.

    Uploaded to my site, the script runs perfectly. Uploaded to client's site, the email craps out and arrives w/ attachment and no message.

    So I know the code is correct.
  • edited June 2008
    hello....mr Kwitko can you pls help me..ur code works very fine..but i need to attach multiple files....and i try to use foreach...but i cant get the right one..am lost...could u pls show me the code..pls?tnx
  • a2jfreaka2jfreak Houston, TX Member
    edited July 2008
    xysa wrote:
    hello....mr Kwitko can you pls help me..ur code works very fine..but i need to attach multiple files....and i try to use foreach...but i cant get the right one..am lost...could u pls show me the code..pls?tnx

    That functionality was added some time back. Look through this thread again and you'll see the code for it.
  • edited July 2008
    Very new to php and have tried this script and had no success so after messing around with it and some other basic forms i have come up with a script that somewhat works. As of right now i can get it to send the e-mail but the attachment is showing up in the body area of the email and is showing up as what i am guessing to be the base 64 code as it looks like this


    PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMDEgVHJhbnNpdGlvbmFs
    Ly9FTiINImh0dHA6Ly93d3cudzMub3JnL1RSL2h0bWw0L2xvb3NlLmR0ZCI+DTxodG1sPg08aGVh
    ZD4NPG1ldGEgaHR0cC1lcXVpdj0iQ29udGVudC1UeXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNo
    YXJzZXQ9aXNvLTg4NTktMSI+DTx0aXRsZT5Bbm5lJ3MgUmVudGFsczwvdGl0bGU+DTxzdHlsZSB0
    eXBlPSJ0ZXh0L2NzcyI+DTwhLS0NLnN0eWxlMSB7DQlmb250LWZhbWlseTogVmVyZGFuYSwgQXJp
    YWwsIEhlbHZldGljYSwgc2Fucy1zZXJpZjsNCWZvbnQtc2l6ZTogMjRweDsNfQ0uc3R5bGUyIHsN
    CWZvbnQtc2l6ZTogMTJweDsNCWZvbnQtZmFtaWx5OiBWZXJkYW5hLCBBcmlhbCwgSGVsdmV0aWNh

    the code i am using is as follows:

    [php]


    ****** http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />




    $emailadd = 'me@mymail.com';
    $url = 'http://www.mydomain.com/confirmation.htm';
    $req = '1';
    $attachment = $_FILES;
    $attachment_name = $_FILES;
    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
    $fp = fopen($attachment, "rb"); //Open it
    $data = fread($fp, filesize($attachment)); //Read it
    $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
    fclose($fp);
    }
    mail($emailadd, $message, $data);
    echo '****** HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    ?>

    [/php]any help with this would be great and I would like to extend my thanks in advance.
  • edited July 2008
    scratch that last post i got it figured out thank you all for the work you have all put into this code
  • edited November 2008
    Kwitko wrote:
    As promised. Now I didn't add any error checking or formatting. My goal was simply to make sure the MIME encoding was done right. MIME headers are *very* finicky. If you want to add more attachments, you could foreach or while the chunk routine.

    Without further ado, I give you:

    [php]
    //EDIT: added some additional comments. I realized after posting that it's kind of silly to add the MIME boundary if you're not neccessarily sending an attachment, so the encoding routine plus the boundaries for the attachment should really be conditional. Then again, the whole purpose of this script is so you can send attachments, right?














    here is a better upgrade with error checking or formatting to your upload form and mime headers. requires little php and html knowledge


    ---enjoy







    =============================================




    <?php
    $fileatt = ""; // Path to the file
    $fileatt_type = "application/octet-stream"; // File Type
    $fileatt_name = ""; // Filename that will be used for the file as the attachment
    $email_from = ""; // Who the email is from
    $email_subject = ""; // The Subject of the email
    $email_txt = ""; // Message that the email has in it
    $email_to = ""; // Who the email is too
    $headers = "From: ".$email_from;
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    $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}\"";
    $email_message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_message . "\n\n";
    $data = chunk_split(base64_encode($data));
    $email_message .= "--{$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";
    $ok = @mail($email_to, $email_subject, $email_message, $headers);
    if($ok) {
    echo "The file was successfully sent!";
    } else {
    die("Sorry but the email could not be sent. Please go back and try again!");
    }
    ?>










    </FORM>
  • nsanitynsanity New
    edited November 2008
    Just read this last page... but is there any reason you can't use this:

    http://phpmailer.codeworxtech.com/

    I use it every time I need to send attachments.
  • edited May 2009
    Hello all,
    I won't hold my breath waiting for a reply on a 6 month old thread, but if anyone sees this and can offer some help, it would be very much appreciated!

    I've used the original code, and modified it to have 5 slots for attachments... It works perfectly when either 4 or 5 attachments are included, but if fewer than 4 are attached, it doesn't send. It prints out the 'Mail sent' message, but no email actually arrives.

    I originally tried using the dynamic javascript code to allow users to attach as many files as they would like, but that didn't work out too well as I have very minimal experience with javascript. I was then told that users can't be able to submit more than 5 files anyway, so abandoned that idea.

    This is the file with the form:
    [HTML]
    <!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"&gt;
    <head>
    ****** http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>The Alliance of Dental Care Professionals</title>******** type="text/javascript">
    <!--
    function MM_preloadImages() { //v3.0
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}
    }
    //-->
    </script>
    ******** src="scripts/AC_RunActiveContent.js" type="text/javascript"></script>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    </head>

    <body style= "margin:0">

    <table width="708" border="0" cellpadding="10" cellspacing="0">
    <tr>
    <td align="left" valign="top" background="images/tblbck.jpg" class="bodytext"><span class="headline">Create Your

    Profile</span><br />
    <img src="images/divider.jpg" width="670" height="2" /><br />
    <table width="100%" border="0" cellpadding="10" cellspacing="0">
    <tr>
    <td align="center" valign="top" class="bodytext">
    <form enctype="multipart/form-data" name="send" method="post" action="formproc.php">

    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    <input type=hidden name="subject" value="Membership Profile Form">
    <input type=hidden name="require" value="email,phone_no,PracticeName,DrName">
    <table width="511" border="0" cellspacing="10" cellpadding="0">
    <tr>
    <td width="179"><div align="right">Practice Name:</div></td>
    <td width="302"><input name ="PracticeName" type="text" class="bodytext" id="PracticeName" /></td>
    </tr>
    <tr>
    <td><div align="right">Dr Name:</div></td>
    <td><input name="DrName" type="text" class="bodytext" id="DrName" /></td>
    </tr>
    <tr>
    <td><div align="right">Street:</div></td>
    <td><input name="textfield5" type="text" class="bodytext" id="textfield5" /></td>
    </tr>
    <tr>
    <td><div align="right">City:</div></td>
    <td><input name="textfield6" type="text" class="bodytext" id="textfield6" /></td>
    </tr>
    <tr>
    <td><div align="right">Province</div></td>
    <td><input name="textfield7" type="text" class="bodytext" id="textfield7" /></td>
    </tr>
    <tr>
    <td><div align="right">Postal Code:</div></td>
    <td><input name="zip_code" type=text class="bodytext" /></td>
    </tr>
    <tr>
    <td><div align="right">Phone::</div></td>
    <td><input name="phone_no" type=text class="bodytext"></td>
    </tr>
    <tr>
    <td><div align="right">Fax:</div></td>
    <td><input name="fax_no" type=text class="bodytext"></td>
    </tr>
    <tr>
    <td valign="top"><div align="right">Email:</div></td>
    <td><input name="from_email" type=text class="bodytext"></td>
    </tr>
    <tr>
    <td valign="top"><div align="right">
    <div align="right">Website:</div>
    </div></td>
    <td><input name="textfield12" type="text" class="bodytext" id="textfield12" /></td>
    </tr>
    <tr>
    <td valign="top"><div align="right">
    <div align="right">
    <div align="right">Hours of operation:</div>
    </div>
    </div></td>
    <td><table width="283" border="0" cellpadding="0" cellspacing="0" class="bodytext">
    <tr>
    <td width="109" valign="top">Sunday</td>
    <td width="28" valign="top"><label>from</label></td>
    <td width="31"><div align="center">
    <input name="SunFrom" type="text" id="SunFrom" size="2" />
    </div></td>
    <td width="22" valign="top"><div align="center">to</div></td>
    <td width="27"><div align="center">
    <label></label>
    <input name="SunTo" type="text" id="SunTo" size="2" />
    </div></td>
    <td width="20"><input type="checkbox" name="ClosedSun" id="ClosedSun" value= "Closed" /></td>
    <td width="46">Closed</td>
    </tr>
    <tr>
    <td valign="top">Monday</td>
    <td valign="top"><label>from</label></td>
    <td><div align="center">
    <input name="MonFrom" type="text" id="MonFrom" size="2" />
    </div></td>
    <td><div align="center">to</div></td>
    <td><div align="center">
    <label></label>
    <input name="MonTo" type="text" id="MonTo" size="2" />
    </div></td>
    <td><input type="checkbox" name="ClosedMon" id="ClosedMon" value= "Closed" /></td>
    <td>Closed</td>
    </tr>
    <tr>
    <td valign="top">Tuesday</td>
    <td valign="top"><label>from</label></td>
    <td><div align="center">
    <input name="TuesFrom" type="text" id="TuesFrom" size="2" />
    </div></td>
    <td><div align="center">to</div></td>
    <td><div align="center">
    <label></label>
    <input name="TuesTo" type="text" id="TuesTo" size="2" />
    </div></td>
    <td><input type="checkbox" name="ClosedTues" id="ClosedTues" value= "Closed" /></td>
    <td>Closed</td>
    </tr>
    <tr>
    <td valign="top">Wednesday</td>
    <td valign="top"><label>from</label></td>
    <td><div align="center">
    <input name="WedFrom" type="text" id="WedFrom" size="2" />
    </div></td>
    <td><div align="center">to</div></td>
    <td><div align="center">
    <label></label>
    <input name="WedTo" type="text" id="WedTo" size="2" />
    </div></td>
    <td><input type="checkbox" name="ClosedWed" id="ClosedWed" value= "Closed" /></td>
    <td>Closed</td>
    </tr>
    <tr>
    <td valign="top">Thursday</td>
    <td valign="top"><label>from</label></td>
    <td><div align="center">
    <input name="ThurFrom" type="text" id="ThurFrom" size="2" />
    </div></td>
    <td><div align="center">to</div></td>
    <td><div align="center">
    <label></label>
    <input name="ThurTo" type="text" id="ThurTo" size="2" />
    </div></td>
    <td><input type="checkbox" name="ClosedThur" id="ClosedThur" value= "Closed" /></td>
    <td>Closed</td>
    </tr>
    <tr>
    <td valign="top">Friday</td>
    <td valign="top"><label>from</label></td>
    <td><div align="center">
    <input name="FriFrom" type="text" id="FriFrom" size="2" />
    </div></td>
    <td><div align="center">to</div></td>
    <td><div align="center">
    <label></label>
    <input name="FriTo" type="text" id="FriTo" size="2" />
    </div></td>
    <td><input type="checkbox" name="ClosedFri" id="ClosedFri" value= "Closed" /></td>
    <td>Closed</td>
    </tr>
    <tr>
    <td valign="top">Saturday</td>
    <td valign="top"><label>from</label></td>
    <td><div align="center">
    <input name="SatFrom" type="text" id="SatFrom" size="2" />
    </div></td>
    <td><div align="center">to</div></td>
    <td><div align="center">
    <label></label>
    <input name="SatTo" type="text" id="SatTo" size="2" />
    </div></td>
    <td><input type="checkbox" name="ClosedSat" id="ClosedSat" value= "Closed" /></td>
    <td>Closed</td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td valign="top"><div align="right">Request 4 photos and logo:</div></td>
    <td>

    <input type="file" name="attachment" size="50" />
    <br />
    <input type="file" name="attachment2" size="50" />
    <br />
    <input type="file" name="attachment3" size="50" />
    <br />
    <input type="file" name="attachment4" size="50" />
    <br />
    <input type="file" name="attachment5" size="50" />
    </td>
    </tr>
    <tr>
    <td valign="top"> </td>
    <td><label>
    <input type="submit" name="button" id="button" value="Submit" />
    </label></td>
    </tr>
    </table>
    <input type="hidden" name="recipients" value="abUSP6anjudesign.com" />
    </form>
    </td>
    </tr>
    </table>
    <p> </p></td>
    </tr>
    </table>
    </body>
    </html>
    [/HTML]

    and this is the formproc.php file:

    [PHP]
    <?php

    $action = $_REQUEST;
    global $action;

    function sendMyMail() {

    $to_name = "My name";
    $to_email = "My email address";
    $PracticeName = stripslashes($_POST);
    $DrName = stripslashes($_POST);
    $Street = stripslashes($_POST);
    $City = stripslashes($_POST);
    $Province = stripslashes($_POST);
    $PostalCode = stripslashes($_POST);
    $Phone = stripslashes($_POST);
    $Fax = stripslashes($_POST);
    $from_email = stripslashes($_POST);
    $Website = stripslashes($_POST);
    $SunFrom = stripslashes($_POST);
    $SunTo = stripslashes($_POST);
    $ClosedSun = stripslashes($_POST);
    $MonFrom = stripslashes($_POST);
    $MonTo = stripslashes($_POST);
    $ClosedMon = stripslashes($_POST);
    $TuesFrom = stripslashes($_POST);
    $TuesTo = stripslashes($_POST);
    $ClosedTues = stripslashes($_POST);
    $WedFrom = stripslashes($_POST);
    $WedTo = stripslashes($_POST);
    $ClosedWed = stripslashes($_POST);
    $ThurFrom = stripslashes($_POST);
    $ThurTo = stripslashes($_POST);
    $ClosedThur = stripslashes($_POST);
    $FriFrom = stripslashes($_POST);
    $FriTo = stripslashes($_POST);
    $ClosedFri = stripslashes($_POST);
    $SatFrom = stripslashes($_POST);
    $SatTo = stripslashes($_POST);
    $ClosedSat = stripslashes($_POST);

    $body= "
    Practice Name: $PracticeName
    Dr Name: $DrName
    Address: $Street, $City, $Province, $PostalCode
    Phone: $Phone
    Fax: $Fax
    Email: $from_email
    Website: $Website
    Hours of Operation:

    Sunday: $SunFrom to $SunTo $ClosedSun
    Monday: $MonFrom to $MonTo $ClosedMon
    Tuesday: $TuesFrom to $TuesTo $ClosedTues
    Wednesday: $WedFrom to $WedTo $ClosedWed
    Thursday: $ThursFrom to $ThursTo $ClosedThurs
    Friday: $FriFrom to $FriTo $ClosedFri
    Saturday: $SatFrom to $SatTo $ClosedSat
    ";

    $subject= "Membership Profile Form";

    $filecount = 0;
    foreach($_FILES as $file => $value) {
    $attachment[(int)$filecount] = $_FILES[$file];
    $attachment_name[(int)$filecount] = $_FILES[$file];
    if (is_uploaded_file($attachment[(int)$filecount])) { //Do we have a file uploaded?
    $fp = fopen($attachment[(int)$filecount], "rb"); //Open it
    $data[(int)$filecount] = fread($fp, filesize($attachment[(int)$filecount])); //Read it
    $data[(int)$filecount] = chunk_split(base64_encode($data[(int)$filecount])); //Chunk it up and encode it as base64 so it can emailed
    fclose($fp);
    $filecount++;
    }
    }

    //Let's start our headers

    $headers = "From: $PracticeName<" . $_POST . ">\n";
    $headers .= "Reply-To: <" . $_POST . ">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";


    $headers .= "X-Sender: $from_name<" . $_POST . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST . ">\n";
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "

    =MIME_BOUNDRY_main_message \n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
    $message = "
    =MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: quoted-printable\n";
    $message .= "\n";
    /* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
    $message .= "$body\n";
    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_message_parts--\n";
    $message .= "\n";

    for ($i = 0, $filecount = (int) count($data); $i < $filecount; $i++) {
    $message .= "
    =MIME_BOUNDRY_main_message\n";
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name[$i] . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name[$i] . "\"\n\n";
    $message .= $data[$i]; //The base64 encoded message
    $message .= "\n\n";
    }

    $message .= "
    =MIME_BOUNDRY_main_message--\n";

    // send the message

    $ok = mail("$to_name <$to_email>", $subject, $message, $headers);

    if($ok) {
    $result= "Mail sent.";
    } else {
    $result= "There was a problem sending your message. Please go back and try again.";
    }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
    ****** http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>The Alliance of Dental Care Professionals</title>******** type="text/javascript">
    <!--
    function MM_preloadImages() { //v3.0
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}
    }
    //-->
    </script>
    ******** src="scripts/AC_RunActiveContent.js" type="text/javascript"></script>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    </head>

    <body style= "margin:0">


    <table width="708" border="0" cellpadding="10" cellspacing="0">
    <tr>
    <td align="left" valign="top" background="images/tblbck.jpg" class="bodytext"><span class="headline">Create Your Profile</span><br />
    <img src="images/divider.jpg" width="670" height="2" /><br />

    <table width="100%" border="0" cellpadding="10" cellspacing="0">
    <tr>
    <td><? echo $result; ?></td>
    </tr>
    </table>

    <p> </p>

    </td>
    </tr>
    </table>
    </body>
    </html>

    <?

    }

    if ($action == "send") {
    sendMyMail();
    }

    ?>
    [/PHP]

    Essentially, I just need users to be able to send any number of attachments between 0 and 5. This is the first time I've worked with headers and attachments and the like, so forgive me if the solution is obvious!

    Thanks in advance for any assistance anyone can offer.
  • LincLinc Owner Detroit Icrontian
    edited May 2009
    Are you able to turn on error reporting? The easiest first step in a debug is always to see what sort of Notices or Warnings might be cropping up.

    I'm going to go out on a limb and say it's something wrong with the foreach($_FILES as $file => $value) loop. I don't know if blindly looping through the $_FILES element like that is a good idea. The only way I've done it is to individually check each possible expected field ($_FILES, $_FILES, etc) and go from there.

    If it wigs out from 3 or less, I'd say that when it finds a second empty file field in the loop it's running into an error. That's my best intuitive guess from your description of the problem, anyway.
  • edited June 2009
    Thanks, Lincoln! I couldn't get error checking turned on, but I tried separating the foreach loops into 5 individual sections and that seemed to do the trick! Much appreciated. :)
  • LincLinc Owner Detroit Icrontian
    edited June 2009
    Great, glad you got it working :) Thanks for letting us know.
  • edited June 2009
    Q1. How do you get the original script to work with sending to multiple email addresses (multiple recipients)?

    Q2. A follow up to Q1, how can the script be adjusted so that only the "username" is required and the email sent would go to the username's account with Yahoo, Gmail, etc... as in below. One would only enter the username in the to_email field and the mailer would send emails to all the listed addresses in the array for that username.
    $username = 'to_ recipient';
    $to_email = array('to_ recipient@yahoo.com', 'to_ recipient@gmail.com');

    Sorry if the syntax is wrong since I am a PHP/JS newbie. Any help would be greatly appreciated. Thanks much in advance.
  • MiracleManSMiracleManS Chambersburg, PA Icrontian
    edited June 2009
    1) I'm going to go out on a limb and say use a comma separated list i.e. ("a@ randomdomainhere.com", "b@randomdomainhere.com")

    2) You'd likely want to do one of two things, have an open dataset/array that has all of the recipients information (email, name, etc) although I'm not all THAT familiar with php at this moment. The other option is to build a multi-dimensional array of [username][email] to loop through. Personally, I'd use the unique ID of the user and not the name...otherwise you can run into some stupid stuff happening.[/email]
  • LincLinc Owner Detroit Icrontian
    edited June 2009
    You almost wrote it yourself:

    $username = 'to_ recipient';
    $to_email = array( $username.'@yahoo.com', $username.'@gmail.com');
Sign In or Register to comment.