please delete me
please delete me

Reputation: 321

send mail including file attachment

Mail is being sent with file attachment but it's not possible to open it.

what is the problem please solve this.

<?php

    $file_name=$_FILES['Resume']['name'];
    $file_size=$_FILES['Resume']['size'];
    $file_temp=$_FILES['Resume']['tmp_name'];
    
    $to = '[email protected]';
    $subject = 'Test email with attachment'; 
    $from=$_POST['First'];
    $random_hash = md5(date('r', time()));
    $headers = "From: $from\r\nReply-To: $from";
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
    $data = chunk_split(base64_encode(file_get_contents("$file_temp")));
    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-mixed-<?php echo $random_hash; ?>
    Content-Type: $file_type; name=$file_name
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment
    
    <?php echo $data; ?>
    --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";
            ?>
    

Upvotes: 0

Views: 396

Answers (2)

Techie
Techie

Reputation: 45124

You 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.

Also The Geekmail PHP library makes it easy to add attachments to emails

Upvotes: 2

silly
silly

Reputation: 7887

what is "mysql_error"? i think the correct syntax for you die statement is

die(mysql_error());

or

die($mysql_error);

Upvotes: 0

Related Questions