Mail attachment questions...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi there,

I've found a script at these great Google fora. a script
to send emails with attachments.
The script is below this message, name etc. aren't mine, but from
the original post.

My questions are:
   - Is this script extra heavy for a server, or should it be
     no problem?
   - How big do you advise for max size of attachments?
   - Why is the attachment set in an array, when i remove the
     array and only leave it's content-text i get a big time error.
   - Why is this in there? --> $message .= str_replace ("=", "=3D",
   - In the source of the mail, there are a few THOUSAND lines of
     the following text. Right after "Content-ID: <image.jpg>"
     what is it for? Does it delay my server?

I'm sorry if this seems kind of lazy from me, but i'm a newb so i
know how to figure it out else.

Thanks in advance & Greetings

code from script below found at:
    function sendmail ($from_name, $from_email, $to_name, $to_email,
$subject, $text_message="", $html_message="", $attachment="")
        $from = "$from_name <$from_email>";
        $to   = "$to_name <$to_email>";
        $main_boundary = "----=_NextPart_".md5(rand());
        $text_boundary = "----=_NextPart_".md5(rand());
        $html_boundary = "----=_NextPart_".md5(rand());
        $headers  = "From: $from\n";
        $headers .= "Reply-To: $from\n";
        $headers .= "X-Mailer: The Bat! (v2.04.7) Personal\n"; // say
we are
the bat mail client ;-)
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type:
        $message = "\n--$main_boundary\n";
        $message .= "Content-Type:
        $message .= "\n--$text_boundary\n";
        $message .= "Content-Type: text/plain;
        $message .= "Content-Transfer-Encoding: 7bit\n\n";
        $message .= ($text_message!="")?"$text_message":"Text portion
HTML Email";
        $message .= "\n--$text_boundary\n";
        $message .= "Content-Type:
        $message .= "\n--$html_boundary\n";
        $message .= "Content-Type: text/html;
        $message .= "Content-Transfer-Encoding\n\n";
        $message .= str_replace ("=", "=3D", $html_message)."\n";
        if (isset ($attachment) && $attachment != "" && count
Quoted text here. Click to load it
            for ($i=0; $i<count ($attachment); $i++)
                $attfile = $attachment[$i];
                $file_name = basename ($attfile);
                $fp = fopen ($attfile, "r");
                $fcontent = "";
                while (!feof ($fp))
                    $fcontent .= fgets ($fp, 1024);
                $fcontent = chunk_split (base64_encode($fcontent));
                @fclose ($fp);
                $message .= "\n--$html_boundary\n";
                $message .= "Content-Type: application/octetstream\n";
                $message .= "Content-Transfer-Encoding: base64\n";
                $message .= "Content-Disposition: inline;
                $message .= "Content-ID: <$file_name>\n\n";
                $message .= $fcontent;
        $message .= "\n--$html_boundary--\n";
        $message .= "\n--$text_boundary--\n";
        $message .= "\n--$main_boundary--\n";
        @mail ($to, $subject, $message, $headers);

    # Example
    # Sender Name
    $from_name  = "ich bin ich";
    # Sender Email
    $from_email = "";
    # Recipient Name
    $to_name    = "Ludwig Moser";
    # Recipient Email
    $to_email   = "";
    # Email Subject
    $subject    = "von mir an mich";
    # Text Portion
    $text_message = "This is HTML email and your email client softawre
support HTML email.";
    # HTML Portion
    $html_message = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0
    $html_message.= "<html><head><title></title>\n";
    $html_message.= "<meta http-equiv=\"Content-Type\"
    $html_message.= "<style type=\"text/css\">\n";
    $html_message.= "body, td {\nfont-family: Trebuchet
    $html_message.= "</style>\n";
    $html_message.= "</head>\n";
    $html_message.= "<body><table width=\"100%\" cellpadding=0
cellspacing=0><tr valign=top align=center>";
    $html_message.= "<td width=300>This is my HTML Body\n<br>";
    $html_message.= "<img src=\"cid:hh.jpg\"><br>\n";
    $html_message.= "<font color=red>nice button heh?.";
    $html_message.= "</td>";
    $html_message.= "</tr></table>";
    $html_message.= "</body></html>";

    # Attachment Location
    $attachment = array ("./go.gif"); // image in the same folder
    # Execute SendMail Function
    sendmail ($from_name, $from_email, $to_name, $to_email, $subject,
$text_message, $html_message, $attachment);
    echo "mail sent.";

Re: Mail attachment questions...

knoak wrote:
Quoted text here. Click to load it

I didn't study the script ... but it should be no problem unless (maybe)
you have a very large number of users using it.

Quoted text here. Click to load it

0 bytes :-)

Automated mail from the php server should be small and plain.
In that mail let your users know the URL (by ftp of http) where they can
download a file.

Quoted text here. Click to load it

Maybe (I didn't look at the code) so that multiple attachs can be sent.
If you restructure some part of the code you have to know what that
implies to other parts and change them too.

Quoted text here. Click to load it

That's for the encoding "quoted printable" stuff

Quoted text here. Click to load it

That appears to be the separation between the different things sent
within the mail message.

No, it does not delay your server (apart from the building of the

Quoted text here. Click to load it

Search the web and read, read, read :-)
Even if not all you find is accurate or up-to-date you will learn a lot
by reading and trying the things you find in your code.

Mail to my "From:" address is readable by all at /
== ** ## !! ------------------------------------------------ !! ## ** ==
may bypass my spam filter. If it does, I may reply from another address!

Re: Mail attachment questions...

knoak wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

You may want to know <

<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: /

Site Timeline