Forum Moderators: coopster

Message Too Old, No Replies

Email Image Script

help please

         

ineedmoney

4:08 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



Hello,

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

coopster

4:30 pm on Jun 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to send a multi-part mime email attachment. There are examples all over the web including a Zend article [zend.com] that can be found on the PHP mail() [php.net] manual page. The PEAR Mime Classes [pear.php.net] mentioned there work well.

ineedmoney

4:51 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



Thankyou for the reply

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

ineedmoney

5:02 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



This is the script I am using:

//email start

$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

I didn't think it would be this easy, but it was worth a shot!

What do I need to change?

RogueDogg

5:22 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



I'm not sure if I'm suppose to do this ( forgive me if I'm stepping out of line ) but This should be what your looking for, and if you like it and use it, I'd try to figure out a way to support the people who wrote it.

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]

ineedmoney

5:35 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



RogueDogg

Does this allow me to send an e-mail attachment?

I didn't see anything about it on the page.

I can already e-mail from a form, I need the PHP code to process sending an attachment along with it. Thanks for helping, but I don't think that can solve my problem. Any Other Ideas?

RogueDogg

5:42 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



Yes if I read the history of the upgrades/fixes correctly you should be able to use the upload/attachment feature.

[edited by: coopster at 7:01 pm (utc) on June 9, 2006]
[edit reason] removed url [/edit]

dreamcatcher

5:51 pm on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try the PHP Mailer:

[phpmailer.sourceforge.net...]

dc

ineedmoney

6:06 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



thanks for all the replys, but these all take me back to square one.

I would think that I just have to add a few lines of code into what I already have posted above. I just need a way to pass the file they select into the email. Is This not possible with my code?

RogueDogg

6:18 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



I'm sure it is... but maybe take a look at other code and see if you can pick out what your missing. I'm pretty good at that stuff but building from scratch is my downfall.

Good Luck with it.

ineedmoney

6:32 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



Thanks RogueDogg

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

coopster

6:45 pm on Jun 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would go with the open source project that dreamcatcher mentioned or use the PEAR package. Both are well-documented and open source projects. They are much cleaner, much safer. I can personally say this for certain about the PEAR package as I have reviewed and used it myself on occasion.

ineedmoney

7:19 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



Thanks everybody for trying to help, I know everyone is as busy as I am but,

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]

alce

7:51 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



google for aformmail

lines 219-241 might be what you are looking for.
the block of code that starts with
if (count($files)){

ineedmoney

8:02 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



Thankyou for your suggestion, I would like to do this with out a "pre-built" solution, rather integrate it into the script I have created, thus learning something in the process. Can you help me with the code above alce?

edit - ok I will look at those lines, thankyou

[edited by: ineedmoney at 8:03 pm (utc) on June 9, 2006]

dreamcatcher

8:02 pm on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ineedmoney,

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

ineedmoney

8:18 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



dreamcatcher, thankyou for your suggestion, and I agree that for most purposes that using PHP mailer would be the way to go, but I am trying to learn how to do this, and simply integrating a prebuilt solution into my site won't really do that for me. I understand that it has many desirable features, and is much better consructed, but I would like to have a sense of knowing what is going on with the scripts I use, as well as the feeling of some sort of accomplishment when the script is completed and working as intended. Using a prebuilt solution won't do that for me, if anything it will make me lazy and instead of trying to create my own scripts to solve problems, it will have me searching the web for someone elses solutions. Or even worse simply patching bits of script together not knowing what any of them are for or how they function. I am new to PHP and The simple script I have created and would like to use has taken me many hours of trouble shooting, and study. For that alone I feel it is worth trying to stay with my script and find a way to edit it so that it works as I wish it to. Thanks again everyone for your suggestions, but I would really like to find a way to impliment this into the script I have already worked so hard on.

jatar_k

8:57 pm on Jun 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well then read up on the links coop posted in msg 2
or
look at the script alce posted and understand what they do and then use that knowledge to implement a similar thing into your script

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.

alce

9:52 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



ineedmoney,

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 ;)

ineedmoney

10:29 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



jatar_k

"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

alce

11:29 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



Well, the way I see It, having someone else write the code for you and using someone else's script are exactly the same thing. But maybe its just me.

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>";
}

ineedmoney

11:46 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



ok, I already apologized for not being smart enough to be able to figure out what I needed from the great suggestions provided for me from the many people nice enough to help out somebody who apparently offends them a great deal.

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.

jatar_k

1:05 am on Jun 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



don't be too hard on yourself ineedmoney

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. :)

ineedmoney

1:45 am on Jun 10, 2006 (gmt 0)

10+ Year Member



alce

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

jatar_k

4:04 am on Jun 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when most of us came here we didn't know much either and I think you are coming along just fine

I came here 5 yrs ago and used to research and answer posts to give me different things to learn, that is still true today

ineedmoney

2:07 pm on Jun 12, 2006 (gmt 0)

10+ Year Member



In my quest to get this script working I have found a tutorial on the subject that explains the details greatly

[devshed.com...]

alce

7:52 pm on Jun 12, 2006 (gmt 0)

10+ Year Member



Sorry I did not reply sooner. Every now and then I try to stay offline for a couple of days. It is hard but the world cup makes a great excuse ;)

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.

ineedmoney

1:51 am on Jun 13, 2006 (gmt 0)

10+ Year Member



ok, group hug :)

thank you all