Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //define the receiver of the email
- $to = 'myemail@domain.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 rn
- $headers = "From: webmaster@example.comrnReply-To: webmaster@example.com";
- //add boundary string and mime type specification
- $headers .= "rnContent-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($_FILES['curriculum_vitae']['tmp_name'])));
- //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";
- Content-Type: application/zip; name="attachment.zip"
- Content-Type: application/zip; name="<?php echo $_FILES['curriculum_vitae']['name']; ?>"
- $to = 'myemail@domain.com';
- $subject = 'Test email with attachment';
- $random_hash = md5(date('r', time()));
- $headers = "From: webmaster@example.comrnReply-To: webmaster@example.com";
- $headers .= "rnContent-Type: multipart/mixed; boundary="PHP-mixed-".$random_hash.""";
- if(isset($_FILES['curriculum_vitae']['name']) && $_FILES['curriculum_vitae']['name'] != ''){ $attachment = chunk_split(base64_encode(file_get_contents($_FILES['curriculum_vitae']['tmp_name']))); }
- 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 if(isset($_FILES['curriculum_vitae']['name']) && $_FILES['curriculum_vitae']['name'] != ''){ ?>--PHP-mixed-<?php echo $random_hash; ?>
- Content-Type: application/zip; name="<?php echo $_FILES['curriculum_vitae']['name']; ?>"
- Content-Transfer-Encoding: base64
- Content-Disposition: attachment
- <?php echo $attachment; ?>
- --PHP-mixed-<?php echo $random_hash; ?>--
- <?php } ?>
- <?php
- $message = ob_get_clean();
- $mail_sent = @mail( $to, $subject, $message, $headers );
- echo $mail_sent ? "Mail sent" : "Mail failed";
- <?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.">rn";
- $header .= "Reply-To: ".$replyto."rn";
- $header .= "MIME-Version: 1.0rn";
- $header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
- $header .= "This is a multi-part message in MIME format.rn";
- $header .= "--".$uid."rn";
- $header .= "Content-type:text/plain; charset=iso-8859-1rn";
- $header .= "Content-Transfer-Encoding: 7bitrnrn";
- $header .= $message."rnrn";
- $header .= "--".$uid."rn";
- $header .= "Content-Type: application/octet-stream; name="".$filename.""rn"; // use different content types here
- $header .= "Content-Transfer-Encoding: base64rn";
- $header .= "Content-Disposition: attachment; filename="".$filename.""rnrn";
- $header .= $content."rnrn";
- $header .= "--".$uid."--";
- if (mail($mailto, $subject, "", $header)) {
- echo "mail send ... OK"; // or use booleans here
- } else {
- echo "mail send ... ERROR!";
- }
- }
- ?>
- <?php
- $my_file = "somefile.zip";
- $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
- $my_name = "Olaf Lederer";
- $my_mail = "my@mail.com";
- $my_replyto = "my_reply_to@mail.net";
- $my_subject = "This is a mail with attachment.";
- $my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
- mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement