send mail

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hareldo
Forum Newbie
Posts: 8
Joined: Sun Jun 22, 2008 7:50 am

send mail

Post by hareldo »

i wrote this code to send a message:

Code: Select all

 
<?php 
$to = 'hareldo@aatt.com'; 
$subject = 'Test email with attachment'; 
 
$random_hash = md5(date('r', time())); 
 
$headers = "From: a@example.com\r\nReply-To: webmaster@example.com"; 
 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
 
$attachment = chunk_split(base64_encode(file_get_contents('attachments/test.doc'))); 
 
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/msword; name="test.doc"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  
 
<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 
 
<?php 
$message = ob_get_clean(); 
 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>
 
the mail is posted but when i opened it from my mail box,instead of view the file i saw a lot of chars.what the problem??
Last edited by Benjamin on Mon Jun 08, 2009 10:31 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Raph
Forum Commoner
Posts: 43
Joined: Wed May 27, 2009 6:33 pm

Re: send mail

Post by Raph »

instead of view the file i saw a lot of chars
What's with all the hashes and encodes?
hareldo
Forum Newbie
Posts: 8
Joined: Sun Jun 22, 2008 7:50 am

Re: send mail

Post by hareldo »

yes
Post Reply