Forum Moderators: coopster
I have written a php form e-mail script, just a basic contact form. I would like to add a option inside the form so that my users can browse and select the image that they want and have my e-mail script send the image along with the variables. I have the form part done, and can send the text from the form just fine, but I can't get the php script to send the image along with the info. Any Idea on how I Can have the script send a file? I know how to have the file upload, but I would rather have the image just sent to my e-mail. Any Ideas? Thanks
Would it be possible for you to just give me an example script as to how to do this? I am teaching myself PHP and alot of this confuses me. Although the Zend article will make a good read in a few weeks, I would like to get this script done today. Nothing to in depth, Like I said, I already have the email script in place and working, just need to tell php to send the file from the browse file input box in the form. Thanks
$HTTP_POST_VARS.
$contact_email = $HTTP_POST_VARS['contactemail'];
$paid = $HTTP_POST_VARS['paid'];
$lovedby = $HTTP_POST_VARS['lovedby'];
$caption = $HTTP_POST_VARS['caption'];
$city = $HTTP_POST_VARS['city'];
$state = $HTTP_POST_VARS['state'];
$month = $HTTP_POST_VARS['month'];
$day = $HTTP_POST_VARS['day'];
$year = $HTTP_POST_VARS['year'];
//this is the name of my browse file input box
$picture = $HTTP_POST_VARS['picture'];
$email= "example@example.com" ;
$subject = "Caption/Picture Added $day $month $year";
$message = "$contact_email
Loved By:$lovedby
Caption:$caption
City:$city
State:$state
Month:$month
Day:$day
Year:$year
$picture";
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}
elseif (mail($email,$subject,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
//end email script
What do I need to change?
Again SORRY if I'm not suppose to do this.
<snipped url>
<edit>
coopster says:
First, you're forgiven, RogueDogg ;) You aren't stepping out of line, just trying to be helpful and we recognize that. Although we don't mind linking to other scripts, we do prefer that the scripts be open source and/or reputable sources. Thanks!
</edit>
[edited by: coopster at 7:01 pm (utc) on June 9, 2006]
[edit reason] removed url [/edit]
I will look at what is in the script you suggested...Lets hope it is HEAVILY commented :)
In the meantime, can anyone help me figure this out? Is there something simple I can plug in there to get this to work, or do I need to start from square one?
I'll be reloading this page obsessivly awaiting your replys
Thanks
PHP Mailer seems much much much more complicated than what I need.
Or is it that sending an e-mail attachment though a php form is more complicated than I think?
It can't be that hard? Can it?
Could somebody please tell me what needs to be added to the script to send an e-mail attachment?
<snipped code snippet, please see same code earlier>
In the 3 hours since i started this post i have been to over 50 sites, nothing seems to be explaining what I need or they are far to complicated to disect and insert into my script. Thank you all for your help. Even though I have not found a solution yet, I am much closer than I would be without this forum.
[edited by: coopster at 7:56 pm (utc) on June 9, 2006]
[edit reason] Charter [webmasterworld.com] [/edit]
edit - ok I will look at those lines, thankyou
[edited by: ineedmoney at 8:03 pm (utc) on June 9, 2006]
I can understand your frustration, but using a more secure script, and as coopster mentioned, a cleaner, safer system is really what you should be using.
PHP mailer really isn`t that hard to implement. Check out the example page:
[phpmailer.sourceforge.net...]
Give it a try and see how you go.
dc
given your post in msg 3, it makes what you said in msg 17 a little contrary
every link so far has given you an answer. I have a difficult time with you asking us to add to your script and then when members offer suggestions you say you want to write yourself. There are more than enough links and solutions offered so far.
surf the php.net site starting with mail(), read the comments, then move on to other references from there.
The real problem here is not processing the variables form your form or using the mail() function.
The challenges you face, imo, are understanding MIME types and setting mail headers correctly.
Like jatar_k said, you have 2 options:
a)Follow the links coopster posted and learn. I can assure you once you've understood those topics you will have no trouble writing your own script.
b)Use one of the scripts already available. That is why they are there.
My personal advice: read up on the links, understand MIME types and mail headers and then use php Mailer ;)
"given your post in msg 3, it makes what you said in msg 17 a little contrary "
How so? I stated clearly that the zend article would make good reading and that I planned on doing so?
My Request for an example of what would work with my script, is to get the specific code that I need to accomplish my goal, if an example script is given, I can then figure out what needs to be done to accomplish my goal, and further develop my script. What is wrong with having a script that works, then reading up on the subject more? As important as learning the backend is to me, time is also a concern.
Although many examples of similar scripts producing similar results have been given and I appriciate the help, The examples are Huge Scripts with thousands of lines of code, quite hard for someone as unskilled in php as me to decipher, let alone figure out how integrate into a diffrently set up code seemlessly. They are all good examples of php which I will enjoy to learn, but it will take me weeks if not months to figure out the few lines of code that I would need to plug into my script to make it fully functional, where as if I ask for an example of what would work in my code it could take only a few minutes and whats wrong with that? If someone could tell me precisely what code needs to be put in where, I would know where to concentrate my efforts and make a better use of my time.
"every link so far has given you an answer. I have a difficult time with you asking us to add to your script and then when members offer suggestions you say you want to write yourself. There are more than enough links and solutions offered so far. "
Perhaps the answer has been given, can I help it if I still can't figure it out? I simply want to continue to work on a script that I have spent a large amount of time on. I don't understand whats wrong with that? I'm not asking someone to write an entire script for me? Just what specifically I need to put in there to make it work.
I apologize If I have offended anyone in any of my post, I just think it is easier to study something when you know what it is that you need to study, it just seems ridiculus to spend the next week figuring this out when someone can give me the answer in a few minutes, and then I can spend an hour figuring out why it works.
I don't like getting off the point in forums, but I feel like I needed to explain myself, perhaps I have not been clear?
Thankyou all for your help, and your comments,
BTW does anyone know what code to put into my script to make it functional? Thanks
I believe I already gave you what you need to do it yourself. In these lines of code you can find all you need. The only difference is I did not write it myself but if it makes you feel any better, I can tell you I did. I wrote this specifically for you. Now can you use it?
I sort of integrated both scripts (very roughly). It will not work as is. You need to modify a few things and take into consideration that this script expects variables in the $_FILES array and can accept one, two or none attachents (not only images).
There is room for a lot of improvement, for example:
*Set the script to accept only images and display an error mesage if someone attempts to send anything else
$HTTP_POST_VARS.
$contact_email = $HTTP_POST_VARS['contactemail'];
$paid = $HTTP_POST_VARS['paid'];
$lovedby = $HTTP_POST_VARS['lovedby'];
$caption = $HTTP_POST_VARS['caption'];
$city = $HTTP_POST_VARS['city'];
$state = $HTTP_POST_VARS['state'];
$month = $HTTP_POST_VARS['month'];
$day = $HTTP_POST_VARS['day'];
$year = $HTTP_POST_VARS['year'];
//this is the name of my browse file input box
//$picture = $HTTP_POST_VARS['picture'];
$email= "example@example.com" ;
$subject = "Caption/Picture Added $day $month $year";
$files = array(); //files (field names) to attach in mail
if (count($_FILES)){
$files = array_keys($_FILES);
}
// build mail
$date_time = date('Y-m-d H:i:s');
$mime_delimiter = "----=_NextPart_000_0001_".md5(time());
$message =
"This is a multi-part message in MIME format.
--$mime_delimiter
Content-type: text/plain
Content-Transfer-Encoding: 8bit
Content-Disposition: inline
$contact_email
Loved By:$lovedby
Caption:$caption
City:$city
State:$state
Month:$month
Day:$day
Year:$year
$picture
--------------------
";
if (count($files)){
foreach ($files as $file){
$file_name = $_FILES[$file]['name'];
$file_type = $_FILES[$file]['type'];
$file_tmp_name = $_FILES[$file]['tmp_name'];
$file_cnt = "";
$f=@fopen($file_tmp_name, "rb");
if (!$f)
continue;
while($f &&!feof($f))
$file_cnt .= fread($f, 4096);
fclose($f);
if (!strlen($file_type)) $file_type="applicaton/octet-stream";
if ($file_type == 'application/x-msdownload')
$file_type = "applicaton/octet-stream";
$message .= "\n--$mime_delimiter\n";
$message .= "Content-Type: $file_type;\n name=\"$file_name\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n filename=\"$file_name\"\n\n";
$message .= chunk_split(base64_encode($file_cnt));
}
}
$mail .= "\n--$mime_delimiter--";
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}
elseif (mail($email,$subject,$message, )) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
I have been sincere with every post I have made.
I know when things are typed, they can be taken the wrong way, dictation always helps when speaking, but when reading the tone is up to the person reading. English is not my first language and It seems as though I have said the wrong thing once again.
I looked for the forum here entitled "PHP Server Side Scripting for Dummies" but sadly there was no such place. I was not asking someone to specificaly write a special code just for me, just for help with a script I wrote. I have seen many such post on this forum without the member posting taking such abuse. But the blame lies squarely on my shoulders.
I apologize.
Doesn't anyone remember what it was like to learn PHP, write at the very begining? Wasn't it hard for you to pick up on? Or have things always been so easy? Maybe I am just slow.
I'm sorry I have wasted so much time for so many people. I just was hoping for some help, and I appear to have overstayed my welcome.
Please excuse me.
I apologize for us getting our wires crossed
>> apologized for not being smart enough
don't sell yourself short my friend, take a look at alce's example there and see if you can get that working, it looks like it should be close, though you will need to tweak a few things.
if you have trouble with it, try to to isolate the issue and we can see if we can figure it out
attachments are kind of a pain though they aren't too complicated, as alce mentioned, understanding MIME types and mail headers is the trickiest bit.
take a shot and see if you can get it closer, we'll be here
and let's all not worry about the miscommunication, it happens, we're adults, we don't need to get our feelings hurt, we can talk it all out between us. :)
Thank you for helping me as well as everyone else who took the time out of their day to point me in the right direction.
From your post I now know exactly where to place my studies. For that I am nothing but grateful.
When I have completed my script (to its intended usefullness as described in my original post atleast) I will repost in a new thread in the hopes that the next person with this question will have a new resource, Though I agree that this script once complete should not be used "as is" for any commercial purposes, I do feel that it can provide nicely as a learning aid to people like me.
I Hope from that you can gain some sense of well being, and that your post were not for nothing and did have some greater meaning in the long run.
I will be sure to reference all sources, as well as the places to find the more usefull and complete scripts.
I would just like to add that PHP Scripting has become a sort of passion for me, and although I am as close to a "newbie" as one could be, I like to think that with hard work and study, I someday can have the capacity for PHP that many on this forum already have in their possesion.
Thankyou
[devshed.com...]
ineedmoney,
Please take no offence. I realize I posted a few rather acid remarks and for that I sincerely apologize. They ment to be funny, not offensive but having no physical cues, email, forums and IM tend to escalate conflicts.
I am, just like you, very new to PHP. I am also, just like you, not a native english speaker and understand how hard the learning process can be.
The problem here was that it seemed to me you were
unclear as to what you wanted members of the forum to do for you but I realize now it was just a misunderstanding.
I hope the resources posted in this thread help you achieve your goal. If you have any further questions please do not hesitate to post them.