Essentially what I'm trying to do is attach a file to an email I'm sending out. Simple enough, right? For some reason or another it does not like the following code (presumably because of the headers). Can anyone help?
Thanks in advance!!
$subject = "File ".date("Ymd");
$message = "NONE";
$filename = "test.csv";
$content = chunk_split(base64_encode(file_get_contents($filename)));
$uid = md5(uniqid(time()));
$name = basename($file);
$header .= "MIME-Version: 1.0\r\n";
$header .= "From: noreply#x.com\r\n";
$header .= "Reply-To: noreply#x.com\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/csv; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."\r\n";
//echo $header;
if (mail($to_email, $subject, $message, $header)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR!";
}
And the error:
Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent.
Please please please don't build your own MIME emails. Use PHPMailer or Swiftmailer, which do almost everything for you. You can replace you entire script with about 5 or 6 lines of code.
And best of all, they'll give you far better error messages/diagnostics than the pathetically stupid mail() function ever will.
If you insist on building your own header, I would suggest doing so with the aid of your output buffer - also I noticed that you were failing to close up your content boundaries. Pasted below is how I would edit the header generating part of your script.
ob_start();
?>
MIME-Version: 1.0
From: noreply#x.com
Reply-To: noreply#x.com
Content-Type: multipart/mixed; boundary="<?php echo $uid; ?>"
This is a multi-part message in MIME format.
--<?php echo $uid; ?>
Content-Type:text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit
<?php echo $message; ?>
--<?php echo $uid; ?>--
--<?php echo $uid; ?>
Content-Type: text/csv; name="<?php echo $filename; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="<?php echo $filename; ?>"
<?php echo $content; ?>
--<?php echo $uid; ?>--
<?php
$header = trim(ob_get_clean());
The Geekmail PHP library makes it easy to add attachments to emails (and to send emails in general):
$geekMail = new geekMail();
$geekMail->setMailType('text');
$geekMail->from("noreply#x.com");
$geekMail->to($to_email);
$geekMail->subject($subject);
$geekMail->message($message);
$geekMail->attach($filename);
if (!$geekMail->send()){
//an error occurred sending the email
$errors = $geekMail->getDebugger();
}
You don't seem to populate destination address (in the code sample) and you have your message both in headers (that definitely extends further than headers) and in body…
Related
This question already has answers here:
Send attachments with PHP Mail()?
(16 answers)
Closed 3 years ago.
i have an email attachment script that does work and sends me a file with the correct name...however the file is 0 bytes.
here is the php:
$namer = $_FILES["cv_upload"]["name"];
$file ="/home2/deserul7/public_html/nkaccounting/"."temp_cv/"."".$namer."";
$contenttype = $_FILES["cv_upload"]['type'];
$handle = fopen($file, "rb");
$file_size = filesize($file);
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$eol = PHP_EOL;
// Basic headers
$header = "From: NK Accounting <sal#desertsunstudio.com>".$eol;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
// Put everything else in $message
$message = "--".$uid.$eol;
$message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $msg2."<br><br><br>".$eol;
$message .= "--".$uid.$eol;
$message .= "Content-Type: ".$contenttype."; name=\"".$name."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$name."\"".$eol;
$message .= $content.$eol;
$message .= "--".$uid."--";
if (#mail($ouremail, $subject2, $message, $header)){
echo "sent";
}
when i send it to my gmail it works fine but when i try to get it thru my mail app in my desktop it comes in at 0 bytes...please help
There's supposed to be a blank line between the attachment header and the attachment contents. You only have one $eol there, so there's no blank line (you did it correctly for the part with $msg2). Change the Content-Disposition line to:
$message .= "Content-Disposition: attachment; filename=\"".$name."\"".$eol.$eol;
You can see things like this more easily if you use a here-string instead of concatenation.
$message = <<<EOF
--$uid
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
$msg2<br><br><br>
--$uid
Content-Type: $contenttype; name="$name"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$name"
$content
--$uid
EOF;
I am trying to email a text file as an attachment from a PHP script using the code from here: http://webcheatsheet.com/php/send_email_text_html_attachment.php#attachment
<?
$subject = 'Requested File';
$random_hash = md5(date('r', time()));
$headers = "From: email#email.com\r\nReply-To: email#email.com";
$headers .= "\r\nContent-Type: myltipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents('path/test.txt')));
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
I'm not a PHP developer and have very limited experience using it, I am not getting any errors when this script is executed but I am also not receiving the email...any ideas why? Or what I should be looking at specifically?
Thank you for any tips!
EDIT:
As per the comments, I tried using SwiftMailer but I cannot get it to work using this code:
$message = Swift_Message::newInstance();
// Give the message a subject
$message->setSubject('Your subject');
// Set the From address with an associative array
$message->setFrom(array('email#email.com' => 'From Name'));
// Set the To addresses with an associative array
$message->setTo(array('email#email.com', 'email#email.com' => 'Name'));
// Give it a body
$message->setBody('Here is the message itself');
// And optionally an alternative body
$message->addPart('<q>Here is the message itself</q>', 'text/html');
// Optionally add any attachments
$message->attach(Swift_Attachment::fromPath('path/test.csv'));
Again, this code executes without any errors but no email is sent...what am I missing?
That tutorial has errors for its file attachment, I remember it now and I never could modify it to work. (In the past)
Here is a working copy from my own library that you're welcome to use.
Just change all instances of test.txt to the file you wish to attach.
<html>
<head>
<title>Send file attachments using PHP</title>
</head>
<body>
<?php
$to = "email#example.com";
$subject = "This is the subject";
$message = "This is the test message.";
# Open a file
$file = fopen( "test.txt", "r" );
if( $file == false )
{
echo "Error in opening file";
exit();
}
# Read the file into a variable
$size = filesize("test.txt");
$content = fread( $file, $size);
# encode the data for safe transit
# and insert \r\n after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));
# Get a random 32 bit number using time() as seed.
$num = md5( time() );
# Define the main headers.
$header = "From:email#example.com\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
# Define the message section
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$num\r\n";
# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"test.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"test.txt\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
# Send email now
$retval = mail ( $to, $subject, "", $header );
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
</body>
</html>
I am trying to send a multipart mail that contains both html and plain text. This is also one of the ways to get through spam filters and to allow more people to read the mail in case of not supporting HTML. After spending long hours googling, I have found some examples. I made my code, which sends the mail but it displays the text with the html tags, code, string etc.
<?php
$boundary=md5(uniqid(rand()));
$header .= "From:My Name<something#something.com>\n";
$header .= "Reply-To: something#something.com \n";
$header .= 'MIME-Version: 1.0'."\r\n";
$header .= 'Content-type: multipart/alternative;boundary=$boundary '."\n";
$adres = "something#gmail.com";
$subject = "subject";
$message = "This is multipart message using MIME\n";
$message .= "--" . $boundary . "\n";
$message .= "Content-type: text/plain;charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .= "Plain text version\n\n";
$message .="--" . $boundary . "\n";
$message .="Content-type: text/html;charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .="<html>
<body>
<center>
<b>HTML text version</b>
</center>
</body>
</html>\n\n";
$message .= "--" . $boundary . "--";
if(mail($adres, $subject, $message, $header))
{
print'message sent';
}
else
{
print'message was not sent';
}
?>
This is the result:
This is multipart message using MIME
--c071adfa945491cac7759a760ff8baeb
Content-type: text/plain;charset=iso-8859-1
Content-Transfer-Encoding: 7bit
Plain text version
--c071adfa945491cac7759a760ff8baeb
Content-type: text/html;charset=iso-8859-1
Content-Transfer-Encoding: 7bit
<html>
<body>
<center>
<b>HTML text version</b>
</center>
</body>
</html>
--c071adfa945491cac7759a760ff8baeb--
As you can see it displays the coding instead of the message alone. I have tried many solutions posted like:
adding/removing \r\n;
changing \r\n to \n;
changing content type from alternative to mixed;
I am learning PHP and all I know is all I have read and done so far. I have still much to learn so please if you could tell me where is the problem. I would be very thankful.Best regards.
The line:
$header .= 'Content-type: multipart/alternative;boundary=$boundary '."\n";
Has the wrong quotes, so $boundary won't be expanded. Change to:
$header .= "Content-type: multipart/alternative;boundary=$boundary\n";
And like I said in the comments, in the message headers and the content section headers you should be using \r\n as the line break since that's what is defined in the RFC. Most MTAs will allow simply \n, but some will choke on the message, and some spam filters will count every RFC violation as a point towards your spam score.
Using something like PHPMailer is a much better option because it formats everything perfectly by default, and abides by just about every single obscure, boring RFC.
I think you need quotes around the boundary string.
try this:
$header .= 'Content-type: multipart/alternative; boundary="' . $boundary . '"\r\n';
Try this example https://github.com/breakermind/PhpMimeParser/blob/master/PhpMimeClient_class.php
$m = new PhpMimeClient();
// Add to
$m->addTo("email#star.ccc", "Albercik");
$m->addTo("adela#music.com", "Adela");
// Add Cc
$m->addCc("zonk#email.au");
// Add Bcc
$m->addBcc("boos#domain.com", "BOSS");
// Add files inline
$m->addFile('photo.jpg',"zenek123");
// Add file
$m->addFile('sun.png');
// create mime
$m->createMime("Witaj!",'<h1>Witaj jak się masz? <img src="cid:zenek123"> </h1>',"Wesołych świąt życzę!","Heniek Wielki", "hohoho#domain.com");
// get mime
// $m->getMime();
// Show mime
echo nl2br(htmlentities($m->getMime()));
Here is the complete script without errors:
<?php
error_reporting(-1);
ini_set("display_errors", "1");
$mailto = "email#enter-domainname-here.com";
$subject = "subject";
$boundary=md5(uniqid(rand()));
$header = "From:Info<".$mailto.">\n";
$header .= "Reply-To: ".$mailto."\n";
$header .= "MIME-Version: 1.0"."\n";
$header .= "Content-type: multipart/alternative; boundary=\"----=_NextPart_" . $boundary . "\"";
$message = "This is multipart message using MIME\n";
$message .= "------=_NextPart_" . $boundary . "\n";
$message .= "Content-Type: text/plain; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .= "Plain text version\n\n";
$message .="------=_NextPart_" . $boundary . "\n";
$message .="Content-Type: text/html; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .="<html>
<body>
<center>
<b>HTML text version</b>
</center>
</body>
</html>\n\n";
$message .= "------=_NextPart_" . $boundary . "--";
if(#mail($mailto, $subject, $message, $header))
{
print'message sent';
}
else
{
print"message was not sent";
}
?>
how to attach multiple files to email and send it to multiple emailid dynamically php
i have one form and i want to send multiple files dynamically to multiple email id on button click.i have tried this code but it only takes zip files. i want all kind of files seperatelly attached.
<?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));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/mixed; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
$my_file = "1.rar";
$my_path = $_SERVER['DOCUMENT_ROOT']."schedulemgt/upload/";
$my_name = "tech";
$my_mail = "my#mail.com";
$my_replyto = "my_reply_to#mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,Your report is here";
mail_attachment($my_file, $my_path, "recipient#mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>
<?php
$to = 'youraddress#example.com';
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
I want to email a PDF as an attachment that was created using FPDF. My code looks like this, but the attachment never comes through.
<?php
require('lib/fpdf/fpdf.php');
$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddFont('Georgiai','','georgiai.php');
$pdf->AddPage();
$pdf->Image('lib/fpdf/giftcertificate.jpg',0,0,500);
$pdf->SetFont('georgiai','',16);
$pdf->Cell(40,10,'Hello World!');
$doc = $pdf->Output('test.pdf', 'S');
//define the receiver of the email
$to = 'myemail#example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: reply#test.com\r\nReply-To: reply#test.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($doc)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Anyone familiar with doing this? I'm hoping to use the PHP mail() function.
This ended up working for me:
<?php
require('lib/fpdf/fpdf.php');
$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddFont('Georgiai','','georgiai.php');
$pdf->AddPage();
$pdf->Image('lib/fpdf/image.jpg',0,0,500);
$pdf->SetFont('georgiai','',16);
$pdf->Cell(40,10,'Hello World!');
// email stuff (change data below)
$to = "myemail#example.com";
$from = "me#example.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
mail($to, $subject, $body, $headers);
?>
if you use PHPMailer
$attachment= $pdf->Output('attachment.pdf', 'S');
$mailer->AddStringAttachment($attachment, 'attachment.pdf');
Enjoy
I think you have a superfluous command there. You are using the string variant of the Output() command:
$doc = $pdf->Output('test.pdf', 'S');
Then you are performing a file_get_contents() on it:
$attachment = chunk_split(base64_encode(file_get_contents($doc)));
It is not a file, it is a file in a string, as file_get_contents() would return if $doc was a filename.
Just reduce that down to:
$attachment = chunk_split(base64_encode($doc));
Then see if any more errors occur.
Create one pdf file name as testing.pdf
<?php
//define the receiver of the email
$to = 'elangovan2me#gmail.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: elangovan2me#gmail.com\r\nReply-To: meeetcity#gmail.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('testing.pdf')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Email Pdf File Attachements</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/pdf; name="attachment.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
None of these worked for me...but were close. This is what worked for me and within a WordPress Plugin
function mmd_CreatePDFTest()
{
require(plugin_dir_path( __FILE__ ).'pdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");
$pdfdoc = $pdf->Output("", "S");
$content = chunk_split(base64_encode($pdfdoc));
$filename = "test.pdf";
$file_name = "test.pdf";
$uid = md5(uniqid(time()));
$mailto = "recipient#gmail.com";
$from_mail = "office#yourdomain.com";
$replyto = $from_mail = "office#yourdomain.com";
$from_name = "YOUR COMPANY NAME | PDF Test";
$subject = "send email with pdf attachment";
$message = "<Please see the attachment.";
// header
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";
$returnpath = "-f" . $from_mail;
if (mail($mailto, $subject, $nmessage, $header, $returnpath))
return true;
else
return false;
} // function mmd_CreatePDFTest()