Click to See Complete Forum and Search --> : add attachment from folder in email form
davidjmorin
January 5th, 2008, 11:34 PM
hey everyone im trying to accomplish something that i feel is easy. heres the setup
the files and folders
-------------------------------------------------------------
images/uploads
images/uploads/tree.jpg
images/download_tree.html
-------------------------------------------------------------
in the download_tree.html i would like to have one form line that a user inputs there email and a send button. after they hit send it shoots and email off with the tree.jpg attached automatically. or maybe even a radio button that user clicks that selects different content like tree.jpg, dog.jpg, cat.jpg and sends based on what is selected
how would i achieve this?
T'hanks everyone
ps:
if its also possible to have a dropdown in the email form that has a list of popular email clients like gmail,hotmail,etc. that the user can select that way all they have to input is there username thanks again
username@hotmail.com
PeejAvery
January 6th, 2008, 10:19 AM
Well, what do you have so far? I have an example on my website of how to send html attachments with PHP (http://www.peejavery.com/coding/php/emailattach.php) if that is with what you are struggling.
davidjmorin
January 6th, 2008, 11:21 AM
yeah i know how to get a form with and upload form in it but what i want is to have a hidden field with a predefined file on my server. if possible could you show me an example script that does this. i have googled everything i can think of and cannot seem to find what im looking for and when i do find sometihng that i think will work i cannot get it to work. i tried phpmailer but cannot figure it out!!
Im pulling my hair out on this
using your script how would i include that in my form and specify the file location?
what i want is a single form field that someone inputs there email address. and a send button. thats it. no other info is needed.
im not sure how to include the attachment in that email that is sent
thanks for your help!!
BT great website. its so clean. love it
i have this code.
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$attachment = $_REQUEST['attach'] ;
mail( "someone@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
<input name='attach' type='hidden' value="uploads/myfile.jpg'>
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
but it doesnt work.....
PeejAvery
January 6th, 2008, 05:11 PM
Please don't post 4 times back to back. Simply click the Edit button and edit your previous post. We don't need all that clutter in the forums.
Of course it doesn't work. You haven't done anything with the variable $attachment. Use the code from my website to attach the image (using the variable) to the variable $message.
davidjmorin
January 6th, 2008, 05:37 PM
ok sorry about that. im being really dumb right now.
is this what i would do?
class.mailer.php
<?php
$to = ' i want the users email here;
$from = 'from@address.com';
$subject = 'Here is the subject';
$boundary = 'Message-Boundary-' . date('YmdHis');
$headers = 'From: ' . $from . "\n";
$headers .= 'Reply-To: ' . $from . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: Multipart/Mixed; boundary=' . $boundary . "\n\n";
$message = '--' . $boundary . "\n";
$message .= 'Content-Transfer-Encoding: 7BIT' . "\n";
$message .= 'Content-Type: text/html; charset=US-ASCII' . "\n\n";
$message .= 'This is the body of the message in HTML.';
$attach = false;
if($attach){
$arrfile = array('file1.jpg', 'file2.jpg'); //// do i change this to my files?
foreach($arrfile as $f){
$file = $f;
$attach = fopen($file, 'rb');
$data = fread($attach, filesize($file));
fclose($attach);
$data = chunk_split(base64_encode($data));
$ext = strtolower(substr(strrchr($file, '.'), 1));
switch($ext){
case 'pdf': $type = 'application/pdf'; break;
case 'exe': $type = 'application/octet-stream'; break;
case 'zip': $type = 'application/zip'; break;
case 'doc': $type = 'application/msword'; break;
case 'xls': $type = 'application/vnd.ms-excel'; break;
case 'ppt': $type = 'application/vnd.ms-powerpoint'; break;
case 'gif': $type = 'image/gif'; break;
case 'png': $type = 'image/png'; break;
case 'jpe':
case 'jpeg':
case 'jpg': $type = 'image/jpg'; break;
default: $type = 'application/force-download';
}
$message .= "\n\n" . '--' . $boundary . "\n";
$message .= 'Content-Transfer-Encoding: base64' . "\n";
$message .= 'Content-Type: ' . $type . '; name="' . $file . '";' . "\n";
$message .= 'Content-Disposition: inline; filename="' . $file . '"' . "\n\n" . $data;
}
}
mail($to, $subject, $message, $headers);
echo 'E-mail sent!';
?>
form.php
echo "<form method='post' action='class.mailer.php'>
Email: <input name='email' type='text' /><br />
<input name="attach" type="hidden">
<input type='submit' />
</form>";
}
?>
sorry if im stupid with this im just fried from searching for this
PeejAvery
January 6th, 2008, 05:51 PM
Yes, you change those files in the array to whatever files you want to be attached.
davidjmorin
January 6th, 2008, 05:55 PM
what about the form? is that ok or can i get rid of
<input name="attach" type="hidden">
will it auto attach by calling class.mailer.php the files?
PeejAvery
January 6th, 2008, 06:13 PM
Can't you do a little research? Testing? Trying?
Just use my script and add what you need. It is all straight forward.
davidjmorin
January 6th, 2008, 06:24 PM
jeesh sorry. im jst at work and dont have access to my page **** firewalls.
just trying to tie it down before i get home.
thanks though.!
youve been a big help
davidjmorin
January 6th, 2008, 08:30 PM
ok peejavery i cannot figure it out.
can you please help me figure this out. im using your script that you gave me but when i submit it i dont get an email even though it says email sent. ive even put my email in the $to field just for test. what i want is for that field to populate what is inputted in thr form!!
please help!
Here is what i have
<?php
$to = $_POST['email'];
$from = 'test@gmail.com';
$subject = 'Here is the subject';
$boundary = 'Message-Boundary-' . date('YmdHis');
$headers = 'From: ' . $from . "\n";
$headers .= 'Reply-To: ' . $from . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: Multipart/Mixed; boundary=' . $boundary . "\n\n";
$message = '--' . $boundary . "\n";
$message .= 'Content-Transfer-Encoding: 7BIT' . "\n";
$message .= 'Content-Type: text/html; charset=US-ASCII' . "\n\n";
$message .= 'Here is your tone and pic';
$attach = true;
if($attach){
$arrfile = array('jaimieadDONOTREMOVE.jpg','test_1.mp3');
foreach($arrfile as $f){
$file = $f;
$attach = fopen($file, 'rb');
$data = fread($attach, filesize($file));
fclose($attach);
$data = chunk_split(base64_encode($data));
$ext = strtolower(substr(strrchr($file, '.'), 1));
switch($ext){
case 'pdf': $type = 'application/pdf'; break;
case 'exe': $type = 'application/octet-stream'; break;
case 'zip': $type = 'application/zip'; break;
case 'doc': $type = 'application/msword'; break;
case 'xls': $type = 'application/vnd.ms-excel'; break;
case 'ppt': $type = 'application/vnd.ms-powerpoint'; break;
case 'gif': $type = 'image/gif'; break;
case 'png': $type = 'image/png'; break;
case 'jpe':
case 'jpeg':
case 'jpg': $type = 'image/jpg'; break;
default: $type = 'application/force-download';
}
$message .= "\n\n" . '--' . $boundary . "\n";
$message .= 'Content-Transfer-Encoding: base64' . "\n";
$message .= 'Content-Type: ' . $type . '; name="' . $file . '";' . "\n";
$message .= 'Content-Disposition: inline; filename="' . $file . '"' . "\n\n" . $data;
}
}
mail($to, $subject, $message, $headers);
echo 'E-mail sent!';
?>
and the form:
<form method='post' action='class.mailer.php'>
Email: <input name='email' type='text' />
<input type='submit' />
</form>
its located here davidmorin.net/mailer.php
PeejAvery
January 7th, 2008, 10:48 PM
The e-mail sent message is just an echo command. Even if you put it dependant upon the mail() function, that would only check to see if PHP sent the message, it would not check to see if the message was processed and sent by the SMTP server.
Have you checked to make sure that you can even send mail?
davidjmorin
January 8th, 2008, 07:32 AM
yes i have. i have used other send mail scripts in the past.
PeejAvery
January 8th, 2008, 08:48 AM
I testing the exact code you posted. It works. Make sure the paths to the attachments are correct. The way you have it written means that the attachments are in the same folder as the class.mailer.php.
One suggestion...You should add an if statement based on isset($_POST['email']) to enclose all the code in the mailer PHP file. That way, if a person types that in the URL, it won't execute. This way it would require POST interaction.
davidjmorin
January 8th, 2008, 09:20 AM
Hey PJ
is there a way to test my server to see if my email is working? i tried another script this mornign before work one that i used before and tha tis not working either.
as for the attachments yes they are in the same folder. its just my testing folder.
Thanks for all your help
PeejAvery
January 8th, 2008, 09:32 AM
Is it your own personal web server or does someone else host it? If someone else hosts it, then you will have to get on the phone with their tech support. If it is your own, you will have to go through your configuration file (php.ini) to make sure you have set up SMTP correctly. Then you will have to check your SMTP server.
davidjmorin
January 8th, 2008, 10:20 AM
One suggestion...You should add an if statement based on isset($_POST['email']) to enclose all the code in the mailer PHP file. That way, if a person types that in the URL, it won't execute. This way it would require POST interaction.
im not really sure how to do that. But ill look into it. THanks.
davidjmorin
January 8th, 2008, 10:45 AM
so would i use this for the form PJ?
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
<input type='submit' />
</form>";
}
?>
PeejAvery
January 8th, 2008, 11:06 AM
Yes, except for one MAJOR change. You need to stop using $_REQUEST. That will get both GET, POST, and COOKIE variables. This can be a security risk at times because of the ability to pass variables through the URL line. It can also be confusing keeping track of exactly which type of variable it truly is.
davidjmorin
January 8th, 2008, 12:00 PM
<?php
if (isset($_post['email']))
//if "email" is filled out, send email
{
//send email
$email = $_post['email'] ;
}
so this instead?
PeejAvery
January 8th, 2008, 01:09 PM
Correct.
davidjmorin
January 8th, 2008, 02:02 PM
thanks buddy.
you have been awesome.
davidjmorin
January 8th, 2008, 07:18 PM
ok i got it to work when i input my email in the script but it doesnt work with the email that is supplied in the form? any suggestion on how to fix that? haha atleast i got it up and running all i did was put the php.ini file in the same folder.
PeejAvery
January 8th, 2008, 08:39 PM
ok i got it to work when i input my email in the script but it doesnt work with the email that is supplied in the form?
Are you saying when you put your e-mail in the form and submit it? Or when you hardcode it in?
all i did was put the php.ini file in the same folder.
That should have no effect whatsoever. PHP doesn't look in each individual folder. It has the set php.ini configuration. If you want to change that, you have to edit the original.
davidjmorin
January 8th, 2008, 08:51 PM
well i got it to work completely now. can you verify that? stupid song in there but heres the link
http://davidmorin.net/rings/hiphop/mailer.php
PeejAvery
January 8th, 2008, 08:55 PM
Worked for me.
davidjmorin
January 8th, 2008, 09:05 PM
Great!!. Thanks
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.