Langsung ke konten utama

Send Attachments using PHP mail() function

E-Mail Attachments With PHP's mail() Function


Below is a full working example of how to include one or more attachments to an outbound e-mail utilizing PHP's mail() function. Usage Example
<?php
$to = 'destination-address@somewhere.com';
$from = 'source@somewhere.com';
$subject = 'See Attachments';
$message = 'Please review the following attachments.';

// Define a list of FILES to send along with the e-mail. Key = File to be sent. Value = Name of file as seen in the e-mail.
$attachments = array(
 '/tmp/WEDFRTS' => 'first-attachment.png',
 '/tmp/some-other-file' => 'second-attachment.png'
);

// Define any additional headers you may want to include
$headers = array(
 'Reply-to' => 'source@somewhere.com',
 'Some-Other-Header-Name' => 'Header Value'
);

$status = mailAttachments($to, $from, $subject, $message, $attachments, $headers);
if($status === True) {
 print 'Successfully mailed!';
} else {
 print 'Unable to send e-mail.';
}
Where the Magic Happens
<?php
function mailAttachments($to, $from, $subject, $message, $attachments = array(), $headers = array(), $additional_parameters = '') {
 $headers['From'] = $from;

 // Define the boundray we're going to use to separate our data with.
 $mime_boundary = '==MIME_BOUNDARY_' . md5(time());

 // Define attachment-specific headers
 $headers['MIME-Version'] = '1.0';
 $headers['Content-Type'] = 'multipart/mixed; boundary="' . $mime_boundary . '"';

 // Convert the array of header data into a single string.
 $headers_string = '';
 foreach($headers as $header_name => $header_value) {
  if(!empty($headers_string)) {
   $headers_string .= "\r\n";
  }
  $headers_string .= $header_name . ': ' . $header_value;
 }

 // Message Body
 $message_string  = '--' . $mime_boundary;
 $message_string .= "\r\n";
 $message_string .= 'Content-Type: text/plain; charset="iso-8859-1"';
 $message_string .= "\r\n";
 $message_string .= 'Content-Transfer-Encoding: 7bit';
 $message_string .= "\r\n";
 $message_string .= "\r\n";
 $message_string .= $message;
 $message_string .= "\r\n";
 $message_string .= "\r\n";

 // Add attachments to message body
 foreach($attachments as $local_filename => $attachment_filename) {
  if(is_file($local_filename)) {
   $message_string .= '--' . $mime_boundary;
   $message_string .= "\r\n";
   $message_string .= 'Content-Type: application/octet-stream; name="' . $attachment_filename . '"';
   $message_string .= "\r\n";
   $message_string .= 'Content-Description: ' . $attachment_filename;
   $message_string .= "\r\n";

   $fp = @fopen($local_filename, 'rb'); // Create pointer to file
   $file_size = filesize($local_filename); // Read size of file
   $data = @fread($fp, $file_size); // Read file contents
   $data = chunk_split(base64_encode($data)); // Encode file contents for plain text sending

   $message_string .= 'Content-Disposition: attachment; filename="' . $attachment_filename . '"; size=' . $file_size.  ';';
   $message_string .= "\r\n";
   $message_string .= 'Content-Transfer-Encoding: base64';
   $message_string .= "\r\n\r\n";
   $message_string .= $data;
   $message_string .= "\r\n\r\n";
  }
 }

 // Signal end of message
 $message_string .= '--' . $mime_boundary . '--';

 // Send the e-mail.
 return mail($to, $subject, $message_string, $headers_string, $additional_parameters);
}

Komentar

Postingan populer dari blog ini

Use X++ wildcard (LIKE and NOT LIKE) in X++ select statement

For x++ select statements:  select firstOnly batchHistory      where batchHistory.Caption  LIKE  "*Test*"  For x++ queries:  queryBuildRange.value(*Test*); Note the LIKE instead of a '==' and the wildcards inside of the quotations. All other combinations of wildcard usage will work here. This is the same functionality as what the users would put in the grid filtering(eg. '*TEST*' in the caption field filter on the batch tasks form).  However, if you want to find all Captions that do not have the word Test in them (NOT LIKE, !LIKE), you will have to modify the above example slightly.  For x++ select statements:  select firstOnly batchHistory      where  !( batchHistory.Caption LIKE "*TEST*" ) ;  For x++ queries:  queryBuildRange.value(!*Test*);

Format Label Tom Jerry (MS Word)

Setelah beberapa lama aku tidak membuat label undangan, tiba-tiba kemarin aku diminta untuk membuat label undangan Wisuda dengan menggunakan label merek Tom & Jerry (TJ). Harusnya sih menjadi tugas sekretaris panitia tapi karena yang bersangkutan ada tugas di luar kantor maka aku yang membereskannya. Mula-mula aku membuat format labelnya dengan cara manual, tapi untung ada pak Mulyadi (staf tata usaha) yang menyarankan aku untuk mengunduh format label yang telah ada di Internet. Mulailah aku browsing di google dan menemukan format label berbagai ukuran dari website Tom & Jerry di alamat berikut  http://tjlabels.com/en/download.html  (yang link ini agak rempong krn pake masukin nomor captcha-captcha-an). Sedangkan kalau yang link ini langsung pilih file ga pake nomor captcha-captcha-an ( http://tjlabels.com/assets/download/ ). Ternyata juga ada banyak berbagai blog yang menyediakan  link download seperti ini, tapi aku mem- posting  artikel ini ...

Solved : Update on a valid time state table is not allowed without specifying a ValidTimeStateUpdateMode

How To Solve Error :  Cannot edit a record in Position hierarchies (xyz). Update on a valid time state table is not allowed without specifying a ValidTimeStateUpdateMode. select forUpdate * from xyz where xyz.recid== _RecId ; try { ttsBegin; _Value= “Abcd”; xyz.Name = _Value; xyz.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction); xyz.ValidFrom =today(); xyz.ValidTo=dateMax(); xyz.update(); ttsCommit; } catch { ttsAbort; }