Ive got the email program itself working fine. It interacts with a database to get the "subscriber list" as the people it sends it too.

My problem now is i need to fix it to be able to send .pdf files. I have found a couple of scripts that work like this one here for example: Working Script.

Here is the code for the email sender that allows attachments as well.

<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Read POST request params into global vars
$to      = $_POST['to'];
$from    = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);

  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  // Add a multipart boundary above the plain message
  $message = "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" .
             $message . "\n\n";

  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent! Yay PHP!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
</body>
</html>

basically my issue comes with how to integrate the script from the link above into my already working script.

Here is my current code:

<!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" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Send Email</title>
  <link rel="stylesheet" type="text/css" href="style_send.css" />
</head>
<body>
<div id ="outside_container">
		<div id="wrapper">
		<div id="head">
		</div>
		<div id = "content">
  <p><strong>Private:</strong> For Staff use ONLY<br />
  Write and send an email to mailing list members.</p>

<?php
  if (isset($_POST['submit'])) {
    $from = '[noreply]dorcasshopmanager@cca-cary.org';
    $subject = $_POST['subject'];
    $text = $_POST['elvismail'];
    $output_form = false;
	
	

    if (empty($subject) && empty($text)) {
      // We know both $subject AND $text are blank 
      echo 'You forgot the email subject and body text.<br />';
      $output_form = true;
    }

    if (empty($subject) && (!empty($text))) {
      echo 'You forgot the email subject.<br />';
      $output_form = true;
    }

    if ((!empty($subject)) && empty($text)) {
      echo 'You forgot the email body text.<br />';
      $output_form = true;
    }
  }
  else {
    $output_form = true;
  }

  if ((!empty($subject)) && (!empty($text))) {
	  $dbc = mysqli_connect('65.175.112.230', 'dmwimbley0606', '6sknvn', 'example')
      or die('Error connecting to MySQL server.');

    $query = "SELECT * FROM email_list";
    $result = mysqli_query($dbc, $query)
      or die('Error querying database.');

    while ($row = mysqli_fetch_array($result)){
      $to = $row['email'];
      $first_name = $row['first_name'];
      $last_name = $row['last_name'];
      $msg = "Dear $first_name $last_name,\n\n$text";
      mail($to, $subject, $msg, 'From:' . $from);
      echo 'Email sent to: ' . $to . '<br />';
	  
    } 

    mysqli_close($dbc);
  }

  if ($output_form) {
?>

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="subject">Subject of email:</label><br />
    <input id="subject" name="subject" type="text" value="<?php echo $subject; ?>" size="30" /><br />
    <label for="elvismail">Body of email:</label><br />
    <textarea id="elvismail" name="elvismail" rows="8" cols="40"><?php echo $text; ?></textarea><br />
	File Attachment: <input type="file" name="fileatt"/>
	<br/>
    <input type="submit" name="submit" value="Submit" />

  </form>

<?php
  }
?>

</div>

<div id="bottom">
	<?php
 echo '<ul id ="list">
 				<li><a href="/email_list/">Home</a></li>
				<li><a href="/email_list/add_email.php">Add Email Address</a></li>
				<li><a href="/email_list/send_email.php">Send Email</a></li>
				<li><a href="/email_list/remove_email.php">Remove Email Address</a></li>
				<li><a href="/email_list/faq.php">FAQ</a></li>';
 ?>
</div>

</div>
</div>
</body>
</html>

If anyone could provide any guidance on how to integrate the first script into the second script that would be great.

Thanks in advance for the help!

David

Recommended Answers

All 2 Replies

Here is the code (not tested)

<!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" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Send Email</title>
  <link rel="stylesheet" type="text/css" href="style_send.css" />
</head>
<body>
<div id ="outside_container">
		<div id="wrapper">
		<div id="head">
		</div>
		<div id = "content">
  <p><strong>Private:</strong> For Staff use ONLY<br />
  Write and send an email to mailing list members.</p>

<?php
  if (isset($_POST['submit'])) {
    $from = '[noreply]dorcasshopmanager@cca-cary.org';
    $subject = $_POST['subject'];
    $text = $_POST['elvismail'];
    $output_form = false;
	

    $fileatt      = $_FILES['fileatt']['tmp_name'];
    $fileatt_type = $_FILES['fileatt']['type'];
    $fileatt_name = $_FILES['fileatt']['name'];

    $headers = "From: $from";

  if (is_uploaded_file($fileatt)) {
     // Read the file to be attached ('rb' = read binary)
      $file = fopen($fileatt,'rb');
     $data = fread($file,filesize($fileatt));
     fclose($file);

     // Generate a boundary string
     $semi_rand = md5(time());
     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
      // Add the headers for a file attachment
     $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  // Add a multipart boundary above the plain message
     $message = "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" .
             $message . "\n\n";

  // Base64 encode the file data
    $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
          }
	

    if (empty($subject) && empty($text)) {
      // We know both $subject AND $text are blank 
      echo 'You forgot the email subject and body text.<br />';
      $output_form = true;
    }

    if (empty($subject) && (!empty($text))) {
      echo 'You forgot the email subject.<br />';
      $output_form = true;
    }

    if ((!empty($subject)) && empty($text)) {
      echo 'You forgot the email body text.<br />';
      $output_form = true;
    }
  }
  else {
    $output_form = true;
  }

  if ((!empty($subject)) && (!empty($text))) {
	  $dbc = mysqli_connect('65.175.112.230', 'dmwimbley0606', '6sknvn', 'example')
      or die('Error connecting to MySQL server.');

    $query = "SELECT * FROM email_list";
    $result = mysqli_query($dbc, $query)
      or die('Error querying database.');

    while ($row = mysqli_fetch_array($result)){
      $to = $row['email'];
      $first_name = $row['first_name'];
      $last_name = $row['last_name'];
      $msg = "Dear $first_name $last_name,\n\n$text";
//      mail($to, $subject, $msg, 'From:' . $from);

$ok = @mail($to, $subject, $msg, $headers);
if ($ok) {
  echo "<p>Mail sent! Yay PHP!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
      echo 'Email sent to: ' . $to . '<br />';
	  
    } 

    mysqli_close($dbc);
  }

  if ($output_form) {
?>

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="subject">Subject of email:</label><br />
    <input id="subject" name="subject" type="text" value="<?php echo $subject; ?>" size="30" /><br />
    <label for="elvismail">Body of email:</label><br />
    <textarea id="elvismail" name="elvismail" rows="8" cols="40"><?php echo $text; ?></textarea><br />
	File Attachment: <input type="file" name="fileatt"/>
	<br/>
    <input type="submit" name="submit" value="Submit" />

  </form>

<?php
  }
?>

</div>

<div id="bottom">
	<?php
 echo '<ul id ="list">
 				<li><a href="/email_list/">Home</a></li>
				<li><a href="/email_list/add_email.php">Add Email Address</a></li>
				<li><a href="/email_list/send_email.php">Send Email</a></li>
				<li><a href="/email_list/remove_email.php">Remove Email Address</a></li>
				<li><a href="/email_list/faq.php">FAQ</a></li>';
 ?>
</div>

</div>
</div>
</body>
</html>

Here is the code (not tested)

<!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" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Send Email</title>
  <link rel="stylesheet" type="text/css" href="style_send.css" />
</head>
<body>
<div id ="outside_container">
		<div id="wrapper">
		<div id="head">
		</div>
		<div id = "content">
  <p><strong>Private:</strong> For Staff use ONLY<br />
  Write and send an email to mailing list members.</p>

<?php
  if (isset($_POST['submit'])) {
    $from = '[noreply]dorcasshopmanager@cca-cary.org';
    $subject = $_POST['subject'];
    $text = $_POST['elvismail'];
    $output_form = false;
	

    $fileatt      = $_FILES['fileatt']['tmp_name'];
    $fileatt_type = $_FILES['fileatt']['type'];
    $fileatt_name = $_FILES['fileatt']['name'];

    $headers = "From: $from";

  if (is_uploaded_file($fileatt)) {
     // Read the file to be attached ('rb' = read binary)
      $file = fopen($fileatt,'rb');
     $data = fread($file,filesize($fileatt));
     fclose($file);

     // Generate a boundary string
     $semi_rand = md5(time());
     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
      // Add the headers for a file attachment
     $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  // Add a multipart boundary above the plain message
     $message = "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" .
             $message . "\n\n";

  // Base64 encode the file data
    $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
          }
	

    if (empty($subject) && empty($text)) {
      // We know both $subject AND $text are blank 
      echo 'You forgot the email subject and body text.<br />';
      $output_form = true;
    }

    if (empty($subject) && (!empty($text))) {
      echo 'You forgot the email subject.<br />';
      $output_form = true;
    }

    if ((!empty($subject)) && empty($text)) {
      echo 'You forgot the email body text.<br />';
      $output_form = true;
    }
  }
  else {
    $output_form = true;
  }

  if ((!empty($subject)) && (!empty($text))) {
	  $dbc = mysqli_connect('65.175.112.230', 'dmwimbley0606', '6sknvn', 'example')
      or die('Error connecting to MySQL server.');

    $query = "SELECT * FROM email_list";
    $result = mysqli_query($dbc, $query)
      or die('Error querying database.');

    while ($row = mysqli_fetch_array($result)){
      $to = $row['email'];
      $first_name = $row['first_name'];
      $last_name = $row['last_name'];
      $msg = "Dear $first_name $last_name,\n\n$text";
//      mail($to, $subject, $msg, 'From:' . $from);

$ok = @mail($to, $subject, $msg, $headers);
if ($ok) {
  echo "<p>Mail sent! Yay PHP!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
      echo 'Email sent to: ' . $to . '<br />';
	  
    } 

    mysqli_close($dbc);
  }

  if ($output_form) {
?>

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="subject">Subject of email:</label><br />
    <input id="subject" name="subject" type="text" value="<?php echo $subject; ?>" size="30" /><br />
    <label for="elvismail">Body of email:</label><br />
    <textarea id="elvismail" name="elvismail" rows="8" cols="40"><?php echo $text; ?></textarea><br />
	File Attachment: <input type="file" name="fileatt"/>
	<br/>
    <input type="submit" name="submit" value="Submit" />

  </form>

<?php
  }
?>

</div>

<div id="bottom">
	<?php
 echo '<ul id ="list">
 				<li><a href="/email_list/">Home</a></li>
				<li><a href="/email_list/add_email.php">Add Email Address</a></li>
				<li><a href="/email_list/send_email.php">Send Email</a></li>
				<li><a href="/email_list/remove_email.php">Remove Email Address</a></li>
				<li><a href="/email_list/faq.php">FAQ</a></li>';
 ?>
</div>

</div>
</div>
</body>
</html>

Thanks for taking a look at it for me, but it doesnt work. It will send the email but the attachment does not go with it.

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.