0

I am trying to send a email with attachment my code is below let me know if i am doing missing something or doing something wrong

<?php
    function mail_attachment( $filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message ) {
        $file = $path. "/" .$filename;
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));
        $separator = md5(uniqid(time()));
        $eol = PHP_EOL;

        $header = "From: ".$from_name." <usman_86@rocketmail.com>".$from_mail. $eol;
        $header .= "Reply-To: ".$replyto.$eol;
        $header .= "MIME-Version: 1.0".$eol;
        $header .= "Content-Type: multipart/mixed; boundary=\"".$separator. "\"" . $eol . $eol;
        $header .= "Content-Transfer-Encoding: 7bit" . $eol;
        $header .= "--".$separator. $eol;
        $header .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
        $header .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
        $header .= $message. $eol . $eol;
        $header .= "--".$separator. $eol;
        $header .= "Content-Type: application/octet-stream; name=\"".$filename. $eol; // use different content types here
        $header .= "Content-Transfer-Encoding: base64".$eol;
        $header .= "Content-Disposition: attachment; filename=\"".$filename. $eol;
        $header .= $content. $eol;
        $header .= "--".$separator."--";
        ob_start(); //Turn on output buffering 

        if( mail( $mailto, $subject, "", $header ) ) {
            echo "mail send ... OK"; // or use booleans here
        } else {
            echo "mail send ... ERROR!";
        }
    }

    if( isset($_REQUEST['FindDealer']) ){

        $my_file = "pdf.pdf";
        $my_path = "/";
        $my_name = " Find Dealer @ flowsleeve";
        $my_mail = "stifstone@gmail.com";
        $my_replyto = "my_reply_to@flowsleeve.com";
        $my_subject = "Find Dealer @ flowsleeve";
        $my_message = "Hallo,\r\ndoPlease find Attached PDF For Dealers";

        mail_attachment( $my_file, $my_path, $my_mail, $my_name, $my_replyto, $my_subject, $my_message );
        header("Location: index.php#section8");
        exit();
    }
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
usman
  • 63
  • 10

2 Answers2

0

There is lots here to try to digest but the following line looks wrong.

$header = "From: ".$from_name." <usman_86@rocketmail.com>".$from_mail. $eol;

When resolved, this would appear as:

 "From: my_reply_to@flowsleeve.com <usman_86@rocketmail.com>Find Dealer @ flowsleeve\r\n"

The reply-to address is also wrong, see the full printout of the header to see what I mean.

Tip: To make it easier I'd suggest using variables that are named the same as the parameters supplied to the function mail_attachment ~ I keep having to count which parameter refers to which variable.

The entire header appears like this:

From: my_reply_to@flowsleeve.com <usman_86@rocketmail.com>Find Dealer @ flowsleeve
Reply-To: Find Dealer @ flowsleeve
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="2642aff44ba290c53e0574e15444e12f"

Content-Transfer-Encoding: 7bit
--2642aff44ba290c53e0574e15444e12f
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit



--2642aff44ba290c53e0574e15444e12f
Content-Type: application/octet-stream; name="pdf.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="pdf.pdf


--2642aff44ba290c53e0574e15444e12f--

From previous experience of dealing with attachments the formatting is critical - it MUST be exactly right in terms of line endings or it will fail. Some pointers that might help:

  • There should be only one new line before a boundary.
  • There should be 2 dashes only after the last boundary.
  • There are 2 places where Content-Transfer-Encoding needs to have a new line before.

Also, I forgot to mention that you call the function mail_attachment with 7 parameters yet the function takes 8 parameters. The one that appears to not be supplied in the function call is $from_mail

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

use $mail->addAttachment($path); It is better to use this.

you can get phpmailer file from https://github.com/PHPMailer/PHPMailer

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
?>
Lahar Shah
  • 7,032
  • 4
  • 31
  • 39