using mail() for attachments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PeterR

    using mail() for attachments

    I've spent weeks trying to write a function that is simple to use for
    sending emails with attachments, but I my programming skills are not that
    advanced. I've noticed a few postings in these newsgroups with the same
    problem, so I just want to share my solution.

    I recommend using libmail from


    it has made my university project much simplier now! :-)

    --
    Peter, Sunderland University. UK

    PS: sorry for cross-posting.


  • Dennis Biletsky

    #2
    Re: using mail() for attachments


    "PeterR" <spam@spam.co m> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
    news:40828867.0 @entanet...[color=blue]
    > I've spent weeks trying to write a function that is simple to use for
    > sending emails with attachments, but I my programming skills are not that
    > advanced. I've noticed a few postings in these newsgroups with the same
    > problem, so I just want to share my solution.
    >
    > I recommend using libmail from
    > http://lwest.free.fr/doc/php/lib/ind...e=mail&lang=en
    >
    > it has made my university project much simplier now! :-)
    >
    > --
    > Peter, Sunderland University. UK
    >
    > PS: sorry for cross-posting.
    >
    >[/color]

    If you use Mail.php and Mail_mime.php packages from pear.php.net, there is
    no any problem to send mail.

    with attachment
    <?php
    include('Mail.p hp');
    include('Mail/mime.php');

    $text = 'Text version of email';
    $html = '<html><body>HT ML version of email</body></html>';
    $file = '/home/richard/example.php';
    $crlf = "\r\n";
    $hdrs = array(
    'From' => 'you@yourdomain .com',
    'Subject' => 'Test mime message'
    );

    $mime = new Mail_mime($crlf );

    $mime->setTXTBody($te xt);
    $mime->setHTMLBody($h tml);
    $mime->addAttachment( $file, 'text/plain');

    $body = $mime->get();
    $hdrs = $mime->headers($hdrs) ;

    $mail =& Mail::factory(' mail');
    $mail->send('postmast er@localhost', $hdrs, $body);
    ?>

    with picture
    <?php
    include('Mail.p hp');
    include('Mail/mime.php');

    $text = 'Text version of email';
    $html = '<html><body>HT ML version of email<img
    src="image.jpg" ></body></html>';
    $file = '/tmp/image.jpg';
    $crlf = "\r\n";
    $hdrs = array(
    'From' => 'you@yourdomain .com',
    'Subject' => 'Test mime message'
    );

    $mime = new Mail_mime($crlf );

    $mime->setTXTBody($te xt);
    $mime->addHTMLImage ($file, 'image/jpeg');
    $mime->setHTMLBody($h tml);

    $body = $mime->get();
    $hdrs = $mime->headers($hdrs) ;


    $mail =& Mail::factory(' mail');
    $mail->send('postmast er@localhost', $hdrs, $body);
    ?>

    Also you can send mail using sockets.


    Comment

    • Ludwig Moser

      #3
      Re: using mail() for attachments

      > If you use Mail.php and Mail_mime.php packages from pear.php.net, there is[color=blue]
      > no any problem to send mail.[/color]

      i do not have pear installed so i do not have those files

      i tried to figure it out, no errors come up, but no mail is sent in this
      code:

      <?php
      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_".md 5(rand());
      $text_boundary = "----=_NextPart_".md 5(rand());
      $html_boundary = "----=_NextPart_".md 5(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:
      multipart/mixed;\n\tbound ary=\"$main_bou ndary\"\n";
      $message = "\n--$main_boundary\ n";
      $message .= "Content-Type:
      multipart/alternative;\n\ tboundary=\"$te xt_boundary\"\n ";
      $message .= "\n--$text_boundary\ n";
      $message .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
      $message .= "Content-Transfer-Encoding: 7bit\n\n";
      $message .= ($text_message! ="")?"$text_mes sage":"Text portion of
      HTML Email";
      $message .= "\n--$text_boundary\ n";
      $message .= "Content-Type:
      multipart/related;\n\tbou ndary=\"$html_b oundary\"\n";
      $message .= "\n--$html_boundary\ n";
      $message .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
      $message .= "Content-Transfer-Encoding: quoted-printable\n\n";
      $message .= str_replace ("=", "=3D", $html_message). "\n";
      if (isset ($attachment) && $attachment != "" && count ($attachment)[color=blue]
      >= 1)[/color]
      {
      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;
      filename=\"$fil e_name\"\n";
      $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 = "luke2000@gmx.a t";
      # Recipient Name
      $to_name = "Ludwig Moser";
      # Recipient Email
      $to_email = "luke2000@gmx.a t";
      # Email Subject
      $subject = "von mir an mich";
      # Text Portion
      $text_message = "This is HTML email and your email client softawre ain't
      support HTML email.";
      # HTML Portion
      $html_message = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0
      Transitional//EN\">\n";
      $html_message.= "<html><head><t itle></title>\n";
      $html_message.= "<meta http-equiv=\"Content-Type\" content=\"text/html;
      charset=iso-8859-1\">\n";
      $html_message.= "<style type=\"text/css\">\n";
      $html_message.= "body, td {\nfont-family: Trebuchet MS;\nfont-size:
      12px;\n}\n";
      $html_message.= "</style>\n";
      $html_message.= "</head>\n";
      $html_message.= "<body><tab le 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.jp g\"><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.";
      ?>

      any ideas?

      my system is running on php4.3, apache2 on windowsXP

      TIA
      Luke


      Comment

      • Dennis Biletsky

        #4
        Re: using mail() for attachments


        "Ludwig Moser" <l.moser@chello .at> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
        news:M9Ogc.4937 05$Or1.113586@n ews.chello.at.. .[color=blue][color=green]
        > > If you use Mail.php and Mail_mime.php packages from pear.php.net, there[/color][/color]
        is[color=blue][color=green]
        > > no any problem to send mail.[/color]
        >
        > i do not have pear installed so i do not have those files
        >
        > i tried to figure it out, no errors come up, but no mail is sent in this
        > code:
        >
        > <?php
        > 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_".md 5(rand());
        > $text_boundary = "----=_NextPart_".md 5(rand());
        > $html_boundary = "----=_NextPart_".md 5(rand());
        > $headers = "From: $from\n";
        > $headers .= "Reply-To: $from\n";
        > $headers .= "X-Mailer: The Bat! (v2.04.7) Personal\n"; // say we[/color]
        are[color=blue]
        > the bat mail client ;-)
        > $headers .= "MIME-Version: 1.0\n";
        > $headers .= "Content-Type:
        > multipart/mixed;\n\tbound ary=\"$main_bou ndary\"\n";
        > $message = "\n--$main_boundary\ n";
        > $message .= "Content-Type:
        > multipart/alternative;\n\ tboundary=\"$te xt_boundary\"\n ";
        > $message .= "\n--$text_boundary\ n";
        > $message .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
        > $message .= "Content-Transfer-Encoding: 7bit\n\n";
        > $message .= ($text_message! ="")?"$text_mes sage":"Text portion of
        > HTML Email";
        > $message .= "\n--$text_boundary\ n";
        > $message .= "Content-Type:
        > multipart/related;\n\tbou ndary=\"$html_b oundary\"\n";
        > $message .= "\n--$html_boundary\ n";
        > $message .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
        > $message .= "Content-Transfer-Encoding: quoted-printable\n\n";
        > $message .= str_replace ("=", "=3D", $html_message). "\n";
        > if (isset ($attachment) && $attachment != "" && count[/color]
        ($attachment)[color=blue][color=green]
        > >= 1)[/color]
        > {
        > 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;
        > filename=\"$fil e_name\"\n";
        > $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 = "luke2000@gmx.a t";
        > # Recipient Name
        > $to_name = "Ludwig Moser";
        > # Recipient Email
        > $to_email = "luke2000@gmx.a t";
        > # Email Subject
        > $subject = "von mir an mich";
        > # Text Portion
        > $text_message = "This is HTML email and your email client softawre[/color]
        ain't[color=blue]
        > support HTML email.";
        > # HTML Portion
        > $html_message = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0
        > Transitional//EN\">\n";
        > $html_message.= "<html><head><t itle></title>\n";
        > $html_message.= "<meta http-equiv=\"Content-Type\"[/color]
        content=\"text/html;[color=blue]
        > charset=iso-8859-1\">\n";
        > $html_message.= "<style type=\"text/css\">\n";
        > $html_message.= "body, td {\nfont-family: Trebuchet MS;\nfont-size:
        > 12px;\n}\n";
        > $html_message.= "</style>\n";
        > $html_message.= "</head>\n";
        > $html_message.= "<body><tab le 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.jp g\"><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.";
        > ?>
        >
        > any ideas?
        >
        > my system is running on php4.3, apache2 on windowsXP
        >
        > TIA
        > Luke
        >
        >[/color]

        If you have installed pear module correctlly so it maybe problem with Apache
        2. http://php.net/install.apache2


        Comment

        • Ludwig Moser

          #5
          Re: using mail() for attachments

          > If you have installed pear module correctlly so it maybe problem with
          Apache[color=blue]
          > 2. http://php.net/install.apache2[/color]

          i do not have pear installed...
          php.net says:
          Installation
          There is no installation needed to use these functions; they are
          part of the PHP core.

          sorry my fault, no smtp server installed

          any recommendations for a smtp server (freeware)

          TIA
          Luke


          Comment

          • Dennis Biletsky

            #6
            Re: using mail() for attachments


            "Ludwig Moser" <l.moser@chello .at> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
            news:OoPgc.4942 40$Or1.77421@ne ws.chello.at...[color=blue][color=green]
            > > If you have installed pear module correctlly so it maybe problem with[/color]
            > Apache[color=green]
            > > 2. http://php.net/install.apache2[/color]
            >
            > i do not have pear installed...
            > php.net says:
            > Installation
            > There is no installation needed to use these functions; they are
            > part of the PHP core.
            >
            > sorry my fault, no smtp server installed
            >
            > any recommendations for a smtp server (freeware)
            >
            > TIA
            > Luke
            >
            >[/color]
            [mail function]
            ; For Win32 only.
            SMTP = ip_of_your_mail _server

            your should indicate ip_of_your_mail _server. It can be a mail server of your
            provider or any other mail server that accept mail from you.


            Comment

            • Ludwig Moser

              #7
              Re: using mail() for attachments

              [color=blue]
              > [mail function]
              > ; For Win32 only.
              > SMTP = ip_of_your_mail _server[/color]
              [color=blue]
              > your should indicate ip_of_your_mail _server. It can be a mail server of[/color]
              your[color=blue]
              > provider or any other mail server that accept mail from you.[/color]

              added
              # For Win32 only.
              SMTP = 127.0.0.1;

              then php reports
              Parse error: parse error, unexpected '=' in D:\httproot\tes t\sendmail.php on
              line 55

              i have now installed PostCast a freeware SMTP server, its up and working
              localhost:25

              TIA
              Luke


              Comment

              • Ludwig Moser

                #8
                Re: using mail() for attachments

                > [mail function][color=blue]
                > ; For Win32 only.
                > SMTP = ip_of_your_mail _server[/color]

                sorry, hehe
                i edited the php ini ;)



                Comment

                Working...