PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
bogdan
Forum Commoner
Posts: 27 Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro
Post
by bogdan » Mon Jun 19, 2006 5:27 am
Hi, I am trying to make one , simple, but of my own PHP to send e-mail and also have attachment:
This is my code:
Code: Select all
echo <<<mailu
<form method=post action="">
Enter yer message and arth be done with it hit yer old parrot:
<br>
<table>
<tr>
<td>To:</td> <td><input type="text" name="rec"></td>
</tr>
<tr>
<td>From:</td> <td><input type="text" name="from"></td>
</tr>
<tr>
<td>Cc:</td> <td><input type="text" name="cc"></td>
</tr>
<tr>
<td>Bcc:</td> <td><input type="text" name="bcc"></td>
</tr>
<tr>
<td>Subject:</td> <td><input type="text" name="subj"></td>
</tr>
<tr>
<td>Attachment:</td> <td><input type="file" name="att"></td>
</tr>
</table>
<textarea name="body" cols="50" rows="10"></textarea><br>
<input type="submit" value="Send !!!">
</form>
mailu;
$email_to = $_REQUEST["rec"];
$email_from = $_REQUEST["from"];
$email_subject = $_REQUEST["subj"];
$email_cc = $_REQUEST["cc"];
$email_bcc = $_REQUEST["bcc"];
$fileatt = $_REQUEST["att"];
$email_txt = $_REQUEST["body"];
$fileatt_type = "application/octet-stream";
$headers = "";
if ($email_to == "")
{
print "Please enter a destination mail address <br>";
}
if ($email_from == "")
{
print "From whom to send this mail? <br>";
}
else
{
$headers = "From: ".$email_from."\r\n";
}
if (! $email_cc == "")
{
$headers .= "Cc: ".$email_cc."\r\n";
}
if (! $email_bcc == "")
{
$headers .= "Bcc: ".$email_bcc."\r\n";
}
if ($email_subject == "")
{
print "Subject may not be empty! <br>";
}
if ($file = fopen($fileatt,'rb'))
{
$data = fread($file,filesize($fileatt));
fclose($file);
}
$boundary = "==Multipart_Boundary_x_x_x_x_x";
$email_message .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$boundary}\"";
$email_message .="This is a multi-part message in MIME format.\n\n" .
"--{$boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_txt . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .="--{$boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
"Content-Disposition:attachment; filename=\"$fileatt\"".
$data . "\n\n" .
"--{$boundary}--\n";
mail($email_to, $email_subject, $email_message, $headers);
And this is what I receive in my inbox:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_x_x_x_x_x"This is a multi-part message in MIME format.
--==Multipart_Boundary_x_x_x_x_x
Content-Type:text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
WSWSWSWSWSSW AAAAAAAAAAAAAAAAAAAAAAAAAA
--==Multipart_Boundary_x_x_x_x_x
Content-Type: application/octet-stream;
name="001.jpg"
Content-Transfer-Encoding: base64
Content-Disposition:attachment; filename="001.jpg"/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAZAAA/+4ADkFkb2JlAGTAAAAAAf/b
AIQAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQICAgICAgICAgIC
AwMDAwMDAwMDAwEBAQEBAQECAQECAgIBAgIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMD
AwMDAwMDAwMDAwMDAwMDAwMD/8AAEQgBJQGQAwERAAIRAQMRAf/EAIcAAAICAwEBAQEBAAAAAAAA
AAgJBgcABQoEAwIBCwEBAAAAAAAAAAAAAAAAAAAAABAAAQUBAAEDBAEDAgQDBQERBQIDBAYHAQgR
EhMAFBUJFiEiFzEjQVEyJEIlGGFxgVIzkWJDNfCh0fFyUzQnCmODJjZGEQEAAAAAAAAAAAAAAAAA
AAAA/9oADAMBAAIRAxEAPwDv4+gz6DPoM+gz6DPoM+gz6DPoM+gz6D5OPIaS4pzvEIbQpxa1qShC
W089ylqWrvEpQjn+ve/059BTWj+QeVZVOyYZcLNGjE9x0MHmGXDoPWyU622c7FkTmui4sVxb0kMN
HxVyJ05vi48Nj0W4rnFJ9QuT50f2f699/fRPP6e719vV+32evu4r2c9e8/4fQf1TvE+vqlfOc6rn
VdT32c9Oc/r1X/BPf+f0H1+gz6DPoM+gz6DPoM+gz6DPoM+gz6DPoM+gz6DPoM+gz6DPoM+gz6DP
oM+gz6DPoM+gz6DPoM+gz6DPoM+gz6DPoM+gz6DPoM+gz6DPoM+gz6DPoM+gz6DPoPASnxBkCYSn
zog6APiyJs8hNkMxYUCFEZXIlzJkl9SGI8WNHbU444tXEoSnqu/0536Dn18sNo2HytPCK5npOXWc
.......................................................................................................
z093/t+g98D/ABh8kL8Z/Bfk+d/8d9l+A933X2jX3H2fw/3fcfYez3+z+74PT1/s9PoJWv8AD+5f
yfjvd7Xvk+T7f3ez2/8AcfJ7/wCvs9v/AFev9PT/AF+g/Yr8V+PifhPsPxPwo+w/F/B+O+39P7Ps
vtf+1+D0/wBPj/t+g//Z
--==Multipart_Boundary_x_x_x_x_x--
"we have a problem"
Regards, Bogdan
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Mon Jun 19, 2006 6:59 am
What are you trying to get in your inbox. Cause right now you have it set to give you what its giving you.
bogdan
Forum Commoner
Posts: 27 Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro
Post
by bogdan » Mon Jun 19, 2006 7:13 am
I was trying to send an attachment, but instead of receiving a link, like when I get one in gmail or something, I receive what you see.
This is what I was expectin:
From:
joe@joe.com
...
Subject: hi there
--------------------------------
Hello Joe, how are you?
I have sent you this cool picture attached.
pic01.jpg
----------------------------------
What I get now should have been the source of the page I think.....
Regards, Bogdan
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Mon Jun 19, 2006 10:01 am
I'd suggest using a library like
Swiftmail or
PHPMail
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
bogdan
Forum Commoner
Posts: 27 Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro
Post
by bogdan » Tue Jun 20, 2006 4:26 am
Thanks, I have found the errors in my logic / script.
This is the script that does (in most part) what I want, came up with it this morning couse yesterday my brain was jelly
:
Code: Select all
// creating a html form to send the data throuh the browser
echo <<<the_form
<html>
<head>
<title> PHP MIME E-Mail </title>
</head>
<body>
<form name="Mail_submision" action="a_mail_fresh_start.php" method="post">
<table>
<tr>
<td>To:</td> <td> <input type="text" name="To"> </td>
</tr>
<tr>
<td>From:</td> <td> <input type="text" name="From"> </td>
</tr>
<tr>
<td>CC:</td> <td> <input type="text" name="CC"> </td>
</tr>
<tr>
<td>BCC:</td> <td> <input type="text" name="BCC"> </td>
</tr>
<tr>
<td>Subject:</td> <td> <input type="text" name="Subject"> </td>
</tr>
<tr>
<td>Attachment:</td> <td> <input type="file" name="Attachment"> </td>
</tr>
<tr>
<td>Here goes the text message:</td>
</tr>
</table>
<textarea name="Message" cols="50" rows="20"></textarea>
<input type="submit" value="Send E-Mail">
</form>
the_form;
// The actual PHP code for sending e-mail
$a_receiver = $_POST["To"]; // mail recipient
$a_sender = $_POST["From"]; // mail sender
$a_cc = $_POST["CC"]; // mail co-receiver (visible)
$a_bcc = $_POST["BCC"]; // mail co-receiver (blind)
$a_subject = $_POST["Subject"]; // mail subject
$a_att = $_POST["Attachment"]; // mail attachement (archive, picture, file, et caetera)
$a_body = $_POST["Message"]; // mail text message
if (! isset($a_receiver))
{
echo "<b>Please supply a receiver for the e-mail. Thank You!</b>";
}
elseif (! isset($a_sender))
{
echo "From field is mandatory.";
}
elseif (! isset($a_subject))
{
echo "Subject field is mandatory. Please enter a Subject.";
}
else
{
$boundary = "0123456789 -- A-Z / a-z";
$a_headers = "From: ".$a_sender."\n";
$a_headers .= "MIME-Version: 1.0\n";
$a_headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\";\n";
$a_headers .= "Content-Transfer-Encoding: 7bit\n";
//$a_headers .= "--".$boundary;//."\n\n";
/*
$a_headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$a_headers .= "Content-Transfer-Encoding: 7bit\n";
$a_headers .= $a_message."\n\n";
$a_headers .= $boundary."\n\n";
$a_headers .= "Content-Type: $a_att_type; name=\"$a_att\";\n";
$a_headers .= "Content-Transfer-Encoding: base64\n\n";
$a_headers .= "Content-Disposition: attachment\n\n";
$a_headers .= "--".$boundary."--";
*/
$a_message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$a_message .= "Content-Transfer-Encoding: 7bit\n\n";
$a_message .= "--".$boundary."\n\n";
$a_message .= $a_body."\n\n";
if (! $a_att == "")
{
$a_file = fopen($a_att, "rb");
$a_handler = fread($a_file, filesize($a_att));
fclose($a_file);
//$a_att_type = filetype($a_att);
$a_message .= "--".$boundary."\n";
$a_message .= "Content-Type: image/jpeg; name=\"$a_att\";\n";
$a_message .= "Content-Transfer-Encoding: base64\n\n";
$a_message .= "Content-Disposition: attachment\n\n";
$a_message .= chunk_split(base64_encode($data))."\n\n";
}
$a_message .= "--".$boundary."--";
}
$result = mail($a_receiver, $a_subject, $a_message, $a_headers);