this is my code.i m trying send mail with attachment(word,excel,pdf,etc....any attachment)plz help me how to attach file...mailing list from database(mysql)...thanks in advance

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Send e-mails</title>

<?php
$from = "mail@mail.com";

$mesa = $_POST['mess'];
$subje = $_POST['subj'];
$mails = $_POST['mail'];



?>

<link rel="stylesheet" href="stylesheet.css"> <!-- Main Stylesheet -->

</head>

<body style="margin: 1px; font-family: arial, verdana, san-serif; font-size: 14px;">
<table width="80%" border="1" align="center">
<tr>
<td><div align="right">
Add new e-mail <a href="index1.php"><font color=#005aee>Here</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='logout.php'>Logout</a></div>
</td>
</tr>

<tr>
<td>

<table>
<tr>
<td>

<table>
<tr>
<td>

<form name=mailids>
<select name=test multiple size=26 style="width: 200px;">

<?php

include("connect.php");
        $query="SELECT email FROM emails ORDER BY email";
        $rs = mysql_query($query);

        while($row = mysql_fetch_array($rs))
            {

            echo"<option>".$row['email']."</option>";
            }

?>


</select>
</form>

</td>
</tr>
</table>
</td>

<!-- To add required code to make the milids selected after sending mail -->
<!--script language=javascript>
document.mailids.test[0].selected = true;
</script-->

<td>
<table>
<tr>
<td>
<form name=message method=post action="<?php $PHP_SELF?>">
<br>
FROM :<br>
 <input name=from value=<?php echo "$from";?> type=text size=59 readonly>
<br><br>
Subject:<br>
<input name=subj value="<?php echo($subje); ?>" type=text size=59><br><br>
Message:<br>
<textarea name="mess"  rows=16 cols=50 wrap=physical><?php echo($mesa); ?></textarea>
<input type=hidden name=mail>
<br><br>
Attach File:
<input type="file" name="uploaded_file">
<br><br>
<input type=submit value="Send Mail" onClick="submitForm()">
</form>

<script language=javascript>
function submitForm()
{
var subject = document.message.subj.value;

if(subject.length == "")
{
alert("Please give a proper subject");
return false;
}

var message = document.message.mess.value;

if(message.length == "")
{
alert("Please give a proper message");
return false;
}


var tot = document.mailids.test.length;
var maildis = "";
for(i=0; i<tot; i++)
{
    if(document.mailids.test[i].selected == true)
    {
        var ssr = document.mailids.test[i].value;
        maildis = maildis+"||"+ssr;
        document.message.mail.value = maildis;
    }
}

var maaa = document.message.mail.value;
echo(maaa);
document.message.submit();
}
</script>

</td>
</tr>
</table>
</td>

</tr>
</table>

</td>
</tr>

</table>
<table align="center">
<tr>
<td>

<?php
$tok = strtok($mails, " ||");
echo("Mail sent to:");


$message1 = str_replace("<br />", "\n", $mesa);
$message1 = str_replace("\\\\","",$message1);
$message1 = str_replace("\'","'",$message1);
$message1 = str_replace("\\\"","\"",$message1);
$message1 = nl2br($message1);

while ($tok)
{
    /* additional headers */
    /* To send HTML mail, you can set the Content-type header. */
    $random_hash = md5(date('r', time())); 
    $headers1  = "MIME-Version: 1.0\r\n";
    $headers1 .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers1 .= "To: ".$tok."\r\n";
    $headers1 .= "From: ".$from."\r\n";
    $headers1 .= "Reply-To: ".$from."\r\n";
    $headers1 .= "Cc: \r\n";

    echo("<font color=green> $tok </font>");
    if(@mail($tok,$subje,$message1,$headers1))
    {
        echo("-<font color=#005aee> Mail Sent Successfully </font>,");
    }
    else
    {
        echo("-<font color=red>Mail Not Sent </font>,");
    }
    $tok = strtok("||");
}
?>
</td>
</tr>
</table>


</body>
</html>

try this. let me know of any errors

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Send e-mails</title>

<?php
$from = "mail@mail.com";

$mesa = $_POST['mess'];
$subje = $_POST['subj'];
$mails = $_POST['mail'];

$uploaded_file      = $_FILES['uploaded_file']['tmp_name'];
$uploaded_file_type = $_FILES['uploaded_file']['type'];
$uploaded_file_name = $_FILES['uploaded_file']['name'];

?>

<link rel="stylesheet" href="stylesheet.css"> <!-- Main Stylesheet -->

</head>

<body style="margin: 1px; font-family: arial, verdana, san-serif; font-size: 14px;">
<table width="80%" border="1" align="center">
<tr>
<td><div align="right">
Add new e-mail <a href="index1.php"><font color=#005aee>Here</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='logout.php'>Logout</a></div>
</td>
</tr>

<tr>
<td>

<table>
<tr>
<td>

<table>
<tr>
<td>

<form name=mailids>
<select name=test multiple size=26 style="width: 200px;">

<?php

include("connect.php");
$query="SELECT email FROM emails ORDER BY email";
$rs = mysql_query($query);

while($row = mysql_fetch_array($rs))
{

echo"<option>".$row['email']."</option>";
}

?>


</select>
</form>

</td>
</tr>
</table>
</td>

<!-- To add required code to make the milids selected after sending mail -->
<!--script language=javascript>
document.mailids.test[0].selected = true;
</script-->

<td>
<table>
<tr>
<td>
<form name=message method=post action="<?php $PHP_SELF?>">
<br>
FROM :<br>
<input name=from value=<?php echo "$from";?> type=text size=59 readonly>
<br><br>
Subject:<br>
<input name=subj value="<?php echo($subje); ?>" type=text size=59><br><br>
Message:<br>
<textarea name="mess" rows=16 cols=50 wrap=physical><?php echo($mesa); ?></textarea>
<input type=hidden name=mail>
<br><br>
Attach File:
<input type="file" name="uploaded_file">
<br><br>
<input type=submit value="Send Mail" onClick="submitForm()">
</form>

<script language=javascript>
function submitForm()
{
var subject = document.message.subj.value;

if(subject.length == "")
{
alert("Please give a proper subject");
return false;
}

var message = document.message.mess.value;

if(message.length == "")
{
alert("Please give a proper message");
return false;
}


var tot = document.mailids.test.length;
var maildis = "";
for(i=0; i<tot; i++)
{
if(document.mailids.test[i].selected == true)
{
var ssr = document.mailids.test[i].value;
maildis = maildis+"||"+ssr;
document.message.mail.value = maildis;
}
}

var maaa = document.message.mail.value;
echo(maaa);
document.message.submit();
}
</script>

</td>
</tr>
</table>
</td>

</tr>
</table>

</td>
</tr>

</table>
<table align="center">
<tr>
<td>

<?php
$tok = strtok($mails, " ||");
echo("Mail sent to:");


$message1 = str_replace("<br />", "\n", $mesa);
$message1 = str_replace("\\\\","",$message1);
$message1 = str_replace("\'","'",$message1);
$message1 = str_replace("\\\"","\"",$message1);
$message1 = nl2br($message1);

while ($tok)
{
/* additional headers */
/* To send HTML mail, you can set the Content-type header. */
$random_hash = md5(date('r', time()));
$headers1 = "MIME-Version: 1.0\r\n";
$headers1 .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers1 .= "To: ".$tok."\r\n";
$headers1 .= "From: ".$from."\r\n";
$headers1 .= "Reply-To: ".$from."\r\n";
$headers1 .= "Cc: \r\n";

if (is_uploaded_file($$uploaded_file)) {
  $file = fopen($$uploaded_file,'rb');
  $data = fread($file,filesize($$uploaded_file));
  fclose($file);

  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  $message2 = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message1 . "\n\n";

  $data = chunk_split(base64_encode($data));

  $message2 .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .

              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}




echo("<font color=green> $tok </font>");
if(@mail($tok,$subje,$message2,$headers1))
{
echo("-<font color=#005aee> Mail Sent Successfully </font>,");
}
else
{
echo("-<font color=red>Mail Not Sent </font>,");
}
$tok = strtok("||");
}
?>
</td>
</tr>
</table>


</body>
</html>

Normally I would suggest building it and learning it and making it work because you'll get the most out of it, but email especially html and attachments are one of the things in PHP I'd suggest referring to a library built specifically for it. In the long run you'll save yourself a lot of headaches and many long nights.

I highly suggest you look at something like PHP Mailer (http://phpmailer.worxware.com/index.php?pg=phpmailer) It is a great standalone library that is actively developed and designed specifically for handling these things.

In my experience, delivery rates for mail() can vary person to person, but in all scenarios I've had very few if any problems using SMTP instead. I would suggest you look into the same, also part of php mailers functionality.

try this. let me know of any errors

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Send e-mails</title>

<?php
$from = "mail@mail.com";

$mesa = $_POST['mess'];
$subje = $_POST['subj'];
$mails = $_POST['mail'];

$uploaded_file      = $_FILES['uploaded_file']['tmp_name'];
$uploaded_file_type = $_FILES['uploaded_file']['type'];
$uploaded_file_name = $_FILES['uploaded_file']['name'];

?>

<link rel="stylesheet" href="stylesheet.css"> <!-- Main Stylesheet -->

</head>

<body style="margin: 1px; font-family: arial, verdana, san-serif; font-size: 14px;">
<table width="80%" border="1" align="center">
<tr>
<td><div align="right">
Add new e-mail <a href="index1.php"><font color=#005aee>Here</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='logout.php'>Logout</a></div>
</td>
</tr>

<tr>
<td>

<table>
<tr>
<td>

<table>
<tr>
<td>

<form name=mailids>
<select name=test multiple size=26 style="width: 200px;">

<?php

include("connect.php");
$query="SELECT email FROM emails ORDER BY email";
$rs = mysql_query($query);

while($row = mysql_fetch_array($rs))
{

echo"<option>".$row['email']."</option>";
}

?>


</select>
</form>

</td>
</tr>
</table>
</td>

<!-- To add required code to make the milids selected after sending mail -->
<!--script language=javascript>
document.mailids.test[0].selected = true;
</script-->

<td>
<table>
<tr>
<td>
<form name=message method=post action="<?php $PHP_SELF?>">
<br>
FROM :<br>
<input name=from value=<?php echo "$from";?> type=text size=59 readonly>
<br><br>
Subject:<br>
<input name=subj value="<?php echo($subje); ?>" type=text size=59><br><br>
Message:<br>
<textarea name="mess" rows=16 cols=50 wrap=physical><?php echo($mesa); ?></textarea>
<input type=hidden name=mail>
<br><br>
Attach File:
<input type="file" name="uploaded_file">
<br><br>
<input type=submit value="Send Mail" onClick="submitForm()">
</form>

<script language=javascript>
function submitForm()
{
var subject = document.message.subj.value;

if(subject.length == "")
{
alert("Please give a proper subject");
return false;
}

var message = document.message.mess.value;

if(message.length == "")
{
alert("Please give a proper message");
return false;
}


var tot = document.mailids.test.length;
var maildis = "";
for(i=0; i<tot; i++)
{
if(document.mailids.test[i].selected == true)
{
var ssr = document.mailids.test[i].value;
maildis = maildis+"||"+ssr;
document.message.mail.value = maildis;
}
}

var maaa = document.message.mail.value;
echo(maaa);
document.message.submit();
}
</script>

</td>
</tr>
</table>
</td>

</tr>
</table>

</td>
</tr>

</table>
<table align="center">
<tr>
<td>

<?php
$tok = strtok($mails, " ||");
echo("Mail sent to:");


$message1 = str_replace("<br />", "\n", $mesa);
$message1 = str_replace("\\\\","",$message1);
$message1 = str_replace("\'","'",$message1);
$message1 = str_replace("\\\"","\"",$message1);
$message1 = nl2br($message1);

while ($tok)
{
/* additional headers */
/* To send HTML mail, you can set the Content-type header. */
$random_hash = md5(date('r', time()));
$headers1 = "MIME-Version: 1.0\r\n";
$headers1 .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers1 .= "To: ".$tok."\r\n";
$headers1 .= "From: ".$from."\r\n";
$headers1 .= "Reply-To: ".$from."\r\n";
$headers1 .= "Cc: \r\n";

if (is_uploaded_file($$uploaded_file)) {
  $file = fopen($$uploaded_file,'rb');
  $data = fread($file,filesize($$uploaded_file));
  fclose($file);

  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  $message2 = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message1 . "\n\n";

  $data = chunk_split(base64_encode($data));

  $message2 .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .

              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}




echo("<font color=green> $tok </font>");
if(@mail($tok,$subje,$message2,$headers1))
{
echo("-<font color=#005aee> Mail Sent Successfully </font>,");
}
else
{
echo("-<font color=red>Mail Not Sent </font>,");
}
$tok = strtok("||");
}
?>
</td>
</tr>
</table>


</body>
</html>

thanx for your reply.....but its not working...........only subject text sending...there is no headers,attachment and message.............plz help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.