php email function not sending attachment -
i trying send email attachment code below let me know if doing missing or doing 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 dealers"; mail_attachment( $my_file, $my_path, $my_mail, $my_name, $my_replyto, $my_subject, $my_message ); header("location: index.php#section8"); exit(); } ?>
use $mail->addattachment($path); better use this.
you can phpmailer file https://github.com/phpmailer/phpmailer
<?php require("class.phpmailer.php"); $mail = new phpmailer(); $mail->issmtp(); // set mailer use smtp $mail->host = 'smtp1.example.com;smtp2.example.com'; // specify main , 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` accepted $mail->port = 587; // tcp port connect $mail->setfrom('from@example.com', 'mailer'); $mail->addaddress('joe@example.net', 'joe user'); // add recipient $mail->addaddress('ellen@example.com'); // name 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 html $mail->subject = 'here subject'; $mail->body = 'this html message body <b>in bold!</b>'; $mail->altbody = 'this body in plain text non-html mail clients'; ?>
Comments
Post a Comment