Zde funkcni kod (posilam JPEGy), v tele staci odkomentovat verzi pro PDF a zakomentovat JPG (radek 16 a 18).
Do logu si zapisuje podarilo li se odeslat mail ci nikoli.
Kód:
<?php
//define path and filename
$dir = './images/';
$file = 'picture.jpg';
//define the receiver/sender of the email
$mail_to1 = "receiver@mail.com";
$mail_from = "sender@mail.com";
$sender_name = "SENDER NAME";
//define the subject of the email
$subject = 'subject';
//define file format
$format = 'image/jpeg;';
//use this for PDF document:
//$format = 'application/pdf;';
//define text
$text = "test";
//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: $sender_name <$mail_from>\r\nReply-To: $mail_from\r\nMIME-Version: 1.0";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"".$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($dir.$file)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
This is a multi-part message in MIME format.
--<?php echo $random_hash; ?>
Content-type:text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
<?php echo $text; ?>
--<?php echo $random_hash; ?>
Content-Type: <?php echo $format; ?>; name="<?php echo $file; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="<?php echo $file; ?>"
<?php echo $attachment; ?>
--<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
//write log
$fp = fopen('maillog.txt', 'a');
fwrite($fp, "\nTrying to sent \n");
if (mail( $mail_to1, $subject, $message, $headers )) {
echo "<br>Sent ".$pole[0]." to ".$mail_to1;
fwrite($fp, "Send OK ".date("Y-m-d H:i:s")." to ".$mail_to1);
fwrite($fp, "\n");
} else {
echo "<br>Error1";
fwrite($fp, "Send ERROR".date("Y-m-d H:i:s")." to ".$mail_to1);
fwrite($fp, "\n");
}
fclose($fp);
?>