-1

I have some code I like. It works perfectly, has for years, on any server I put it on. What I like about it is it is only a few lines of code, I can send the email to anybody, they always get it, and I don't have to bother with SMTP user/pass.

For my purposes, I don't need elaborate scripts. I have downloaded and installed all the mailers (PHPmailer) but compared to my code of ~10 lines that does for me the same thing 100,000 lines of code attempts to do but doesn't work because the zip files off the sites are missing files, files are located in the wrong folders, or certain functions 'disabled' for security reasons, where none of the examples work, which means I have to dig in, wade through all the code and fix the examples, etc...

I was hoping there was a simpler solution. The code I have looks like this:

<?
$to = "to@mail.com";
$from = "from@mail.com";
$email_subject = "Test";
$email_message .= "Hello, \n\n";

$headers = 'From: '.$from."\r\n". 'Reply-To: '.$to."\r\n" . 'X-Mailer: PHP/' . phpversion();
@mail($to, $email_subject, $email_message, $headers);
?>

That is six lines of code.

I understand that I could spend the next 18 hours searching Google for this answer, but I thought if it wouldn't be too much trouble to come here and ask this question:

Let's say I had the exact same 6 lines of code, but I also needed to attach a file that was located in the same folder as the PHP file to send the email.

$file = dirname(__FILE__).'doc.pdf';

Is there a fast, economical way to modify the original six lines of code that works to send the message, to add to the PDF file as an attachment?

halfer
  • 19,824
  • 17
  • 99
  • 186
user3053462
  • 39
  • 1
  • 4
  • See also http://stackoverflow.com/questions/1498219/how-to-send-email-attachments-in-php-with-attachments?rq=1 – Thom Wiggers Dec 01 '13 at 01:59
  • I hope I can be forgiven for wondering whether this is a [re-ask of this closed question](http://stackoverflow.com/questions/20307247/attach-small-pdf-to-email-with-phps-mail), but from a brand new user account. I added a link to how to do what you want on the other question: you need to create all the MIME boundaries and base64 encoding manually. – halfer Dec 01 '13 at 02:26

1 Answers1

0

I checked all the answers posted here in re this question, but none of them are that good.

This will work for you:

<?

// fill in the basics here

$to = 'anyone@gmail.com';
$from = 'nobody@gmail.com';
$subject = 'Top Secret';
$message = 'Emails R Us';

// THIS is where you reference the file(s) to be attached: the first part is the     location and file name, the next part is the title in the email for the attachment; you can have more than one file (separate by comma). avoid large files (over 1MB)

$attachments = array(
'basic.pdf' => 'The File Title', // can be any extension
);

$headers = array(
'Reply-to' => 'rto@x.com',
'CC' => 'cc@y.com'
'BCC' => 'bcc@z.com'
    );

// end basics

// now the function that assembles the email and sends it


function mailAttachments($to, $from, $subject, $message, $attachments = array(), $headers = array(), $additional_parameters = '') {
$headers['From'] = $from;
$mime_boundary = '==MIME_BOUNDARY_' . md5(time());
$headers['MIME-Version'] = '1.0';
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $mime_boundary . '"';
$headers_string = '';
foreach($headers as $header_name => $header_value) {
    if(!empty($headers_string)) {
        $headers_string .= "\r\n";
    }
    $headers_string .= $header_name . ': ' . $header_value;
}

$message_string  = '--' . $mime_boundary;
$message_string .= "\r\n";
$message_string .= 'Content-Type: text/plain; charset="iso-8859-1"';
$message_string .= "\r\n";
$message_string .= 'Content-Transfer-Encoding: 7bit';
$message_string .= "\r\n";
$message_string .= "\r\n";
$message_string .= $message;
$message_string .= "\r\n";
$message_string .= "\r\n";

foreach($attachments as $local_filename => $attachment_filename) {
    if(is_file($local_filename)) {
        $message_string .= '--' . $mime_boundary;
        $message_string .= "\r\n";
        $message_string .= 'Content-Type: application/octet-stream;     name="' . $attachment_filename . '"';
        $message_string .= "\r\n";
        $message_string .= 'Content-Description: ' . $attachment_filename;
        $message_string .= "\r\n";

        $fp = @fopen($local_filename, 'rb'); 
        $file_size = filesize($local_filename); 
        $data = @fread($fp, $file_size); 
        $data = chunk_split(base64_encode($data)); 

        $message_string .= 'Content-Disposition: attachment; filename="' . $attachment_filename . '"; size=' . $file_size.  ';';
        $message_string .= "\r\n";
        $message_string .= 'Content-Transfer-Encoding: base64';
        $message_string .= "\r\n\r\n";
        $message_string .= $data;
        $message_string .= "\r\n\r\n";
    }
}


$message_string .= '--' . $mime_boundary . '--';


return mail($to, $subject, $message_string, $headers_string, $additional_parameters);
}

// end function

?>