Unable To Send Attachment Mail Using PHP Intermidiate Version

Status
Not open for further replies.

miworld

New Member
Messages
16
Reaction score
0
Points
0
Hello, In this script, the send() function is not working. It is showing that mail has been sent. But originally mail did not send. This problem only happens for send() function, I mean for sending mail with attachment. Please help me.


This is the script :


<html>
<head>
<title>
Send Mail
</title>
</head>
<body>
<center>
<?php
if(($_POST['replyto']=="@") || ($_POST['replyto']==""))
{
$replyto=$_POST['from'];
}
else
{
$replyto=$_POST['replyto'];
}
$copy=$_POST['copy'];
$copytosender=$_POST['copytosender'];
function mailAttachment($to, $subject, $message, $name, $from, $replyto, $file)
{
$udata = explode("/",$file);
$udata = array_reverse($udata);
copy("$file", "upload/$udata[0]");
$file_name = $udata[0];
$boundary = strtoupper(md5(uniqid(time())));
$mail_header = "From: $name <$from>\n";
$mail_header .= "Reply-To: $replyto\n";
$mail_header .= "MIME-Version: 1.0";
$mail_header .= "\nContent-Type: multipart/mixed; boundary=$boundary";
$mail_header .= "\n--$boundary";
$mail_header .= "\n\n$message";
$file_content = fread(fopen("upload/$udata[0]","r"),filesize("upload/$udata[0]"));
$file_content = chunk_split(base64_encode($file_content));
$mail_header .= "\n--$boundary";
$mail_header .= "\nContent-Type: application/octetstream; name=\"$file_name\"";
$mail_header .= "\nContent-Transfer-Encoding: base64";
$mail_header .= "\nContent-Disposition: attachment; filename=\"$file_name\"";
$mail_header .= "\n\n$file_content";
$mail_header .= "\n--$boundary--";
if(mail($to, $subject, "", $mail_header)) unlink("upload/$udata[0]");
}
function send($to, $subject, $message, $name, $from, $replyto, $file, $copy, $copytosender)
{
if(eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$",$to))
{
for($i=0;$i<$copy;$i++)
{
mailAttachment($to, $subject, $message, $name, $from, $replyto, $file);
}
echo "Your Mail Has Been Successfully Sent To $to With Attachment";
if($copytosender=="y")
{
if(eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$",$from))
{
mailAttachment($from, $subject, $message, $name, $from, $replyto, $file);
echo "<br>A Copy Of Your Mail Has Been Successfully Sent To $from With Attachment";
}
else
{
echo "<br>Unable To Send A Copy Of Your Mail To Sender";
}
}
}
else
{
echo "Unable To Send Mail";
}
}
if(($_POST['file']=="") || ($_POST['file']=="http://"))
{
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_POST['from'];
$mail_header = "From: ".$_POST["name"]. "<".$_POST["from"].">\r\n";
$mail_header .= "Reply-To: $replyto\r\n";
if(eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$",$to))
{
for($i=0;$i<$copy;$i++)
{
mail($to, $subject, $message, $mail_header);
}
echo "Your Mail Has Been Successfully Sent To $to";
if($copytosender=="y")
{
if(eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$",$from))
{
mail($from, $subject, $message, $mail_header);
echo "<br>A Copy Of Your Mail Has Been Successfully Sent To $from";
}
else
{
echo "<br>Unable To Send A Copy Of Your Mail To Sender";
}
}
}
else
{
echo "Unable To Send Mail";
}

}
else
{
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$name = $_POST['name'];
$from = $_POST['from'];
$file = $_POST['file'];
send($to, $subject, $message, $name, $from, $replyto, $file, $copy, $copytosender);
}
?>
<br><br><a href="index.php">BACK</a>
</center>
</body>
</html>
 

Corey

I Break Things
Staff member
Messages
34,553
Reaction score
204
Points
63
Are you getting an error? How big is the attachment? It's sending regular mail fine, only not sending mail with attachments?
 

bugfinder

Retired
Messages
2,260
Reaction score
0
Points
0
Only thing that sticks out for me is $mail_header .= "\n--$boundary--";

You almost certainly want a \n at the end.

As Corey says, is any mail being sent? is anything happening, any error codes?
 

miworld

New Member
Messages
16
Reaction score
0
Points
0
Hi Corey and Bugfinder,

First, I want to say that Bugfinder's help was not helpful. I tried what you said but it is still not working. However, Thanks for the help.

Now I want to say to Corey that I am not getting an error using this script. Attachment is not the matter because it is working neither for 1 KB or 2 KB file [e.g. http://db.wen.su/images/1.jpg or http://miworld.uni.cc/analog.swf] nor for 1 MB or 2 MB file. Yeah Corey, you are absolutely right, it's sending regular mail fine but not sending mail with attachment.

Now I am telling you the whole story. When I am trying to send mail with attachment using this script, php is executing correctly and says "Your Mail Has Been Successfully Sent To $to With Attachment", originally mail did not send to the destination. This script was totally working till my last use when I sent mail with attachment using this in February. But now I am seeing that it is not working.

Before posting this message, I added this code -
else echo "sorry!!!!!!!!!!!";
after this line -
if(mail($to, $subject, "", $mail_header)) unlink("upload/$udata[0]");
then I again tried to execute this script for sending mail with attachment, I saw that this message is showing - sorry!!!!!!!!!!!
It means that this line is not working -
if(mail($to, $subject, "", $mail_header)) unlink("upload/$udata[0]");
After doing many experiment, I discovered when I am ignoring these two lines in script like this then this message - sorry!!!!!!!!!!! is not showing which means the script is working and I saw mail had been sent to the destination without body and with 0 KB attached file. -

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

AND

// $mail_header .= "\n\n$file_content";

But I must have to add $file_content to $mail_header section. Otherwise I don't know any way to attach the file originally.

My CPanel username is miworld and this script is uploaded here with PASSWORD PROTECTION : http://miworld.uni.cc/mail

I need help about this particular matter.

Thanks!!!!!!!!!!!!!!!!!

With best regards,
BARUN SINGH.
 
Last edited:

Corey

I Break Things
Staff member
Messages
34,553
Reaction score
204
Points
63
I'm really not sure what is wrong with it, I do not have time to debug it right now. Where did you get the script? Are you able to get support from the script creator? Also check your error_log in cPanel and see if it is throwing any errors there.

-Corey
 

miworld

New Member
Messages
16
Reaction score
0
Points
0
Hi Corey,

If you have time problem, I will try to debug it. I did not get the whole script. I got only a part of this script from a site. There is nothing in my error_log. I don't think this script has a bug because this script was working properly before one month and I did not change anything after that. Then why it is not working now? You just tell me this.

Thanks!
 

Corey

I Break Things
Staff member
Messages
34,553
Reaction score
204
Points
63
I'm not able to debug it so I'm not sure why. It's possible it could be trying to use a restricted function.. I'm not really sure.

-Corey
 

miworld

New Member
Messages
16
Reaction score
0
Points
0
Ok. Wait. I will experiment with the script and will tell you the result.
 

miworld

New Member
Messages
16
Reaction score
0
Points
0
Hi Corey,

I have got another script which is able to send mail with attachment properly. But this script should also work because it is working in another server properly. So you should check the matter.

Thanking You!!!!!!
 

Fedlerner

Former Adm & Team Manager
Community Support
Messages
12,934
Reaction score
6
Points
38
I'll move this to "Level Two Support".
Please upload both of the scripts (the one that works and the one that doesn't) on a .rar and PM the link to me. I'll check this.
 
Status
Not open for further replies.
Top