SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    london
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Advanced email in PHP

    Hi people

    I've been trying to get my head round Kev's article Advanced email in PHP. I downloaded the PHP which I have posted below, but I can't work out how to send a plain/html email. Kevin's code is for sending plain text emails with attachments via an online form.

    Can I adapt this script to send plain/html mails? (without the attachment bit)

    Any help much appreciated. Thanks


    PHP Code:
    <html>
    <head>
    <title> Sending Email </title>
    </head>
    <body>
    <?php
    // Read POST request params into global vars
    $to      $_POST['to'];
    $from    $_POST['from'];
    $subject $_POST['subject'];
    $message $_POST['message'];

    // Obtain file upload vars
    $fileatt      $_FILES['fileatt']['tmp_name'];
    $fileatt_type $_FILES['fileatt']['type'];
    $fileatt_name $_FILES['fileatt']['name'];

    $headers "From: $from";

    if (
    is_uploaded_file($fileatt)) {
      
    // Read the file to be attached ('rb' = read binary)
      
    $file fopen($fileatt,'rb');
      
    $data fread($file,filesize($fileatt));
      
    fclose($file);

      
    // Generate a boundary string
      
    $semi_rand md5(time());
      
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x";
      
      
    // Add the headers for a file attachment
      
    $headers .= "\nMIME-Version: 1.0\n" .
                  
    "Content-Type: multipart/mixed;\n" .
                  
    " boundary=\"{$mime_boundary}\"";

      
    // Add a multipart boundary above the plain message
      
    $message "This is a multi-part message in MIME format.\n\n" .
                 
    "--{$mime_boundary}\n" .
                 
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                 
    "Content-Transfer-Encoding: 7bit\n\n" .
                 
    $message "\n\n";

      
    // Base64 encode the file data
      
    $data chunk_split(base64_encode($data));

      
    // Add file attachment to the message
      
    $message .= "--{$mime_boundary}\n" .
                  
    "Content-Type: {$fileatt_type};\n" .
                  
    " name=\"{$fileatt_name}\"\n" .
                  
    //"Content-Disposition: attachment;\n" .
                  //" filename=\"{$fileatt_name}\"\n" .
                  
    "Content-Transfer-Encoding: base64\n\n" .
                  
    $data "\n\n" .
                  
    "--{$mime_boundary}--\n";
    }

    // Send the message
    $ok = @mail($to$subject$message$headers);
    if (
    $ok) {
      echo 
    "<p>Mail sent! Yay PHP!</p>";
    } else {
      echo 
    "<p>Mail could not be sent. Sorry!</p>";
    }
    ?>
    </body>
    </html>
    give me all your lentils

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ignore this response if the purpose of the above is merely educational.

    If not, and you really just want to be able to easily send emails - text, html, attachments - without reinventing the wheel, I highly recommend the phpmailer class.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    london
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi ghuytro - thanks for your response.

    phpmailer seems overly complicated for my purposes. I really just want to learn how to send simple html/text emails via php.
    give me all your lentils

  4. #4
    SitePoint Enthusiast drisley's Avatar
    Join Date
    Jun 2001
    Location
    Florida
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is phpmailer faster than the mail() function? I was looking at the possibility of implementing it in a newsletter system I coded, but it wouldn't be worth it if it wouldn't releive some overhead.
    David Risley
    Web Developer, Web Publisher, Blogger, Tech Dude
    PC Mechanic | WebbyOnline

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh it's not hard at all!

    This took me 3 minutes to do :

    PHP Code:
    //  Include the class file
    require("class.phpmailer.php");

    // Create new mailer object
    $mail = new PHPMailer();

    // Tell it to use SMTP
    $mail->IsSMTP();
    //  Mail host
    $mail->Host     "your.mailserver.com";

    // 'From' information
    $mail->From     "you@you.com";
    $mail->FromName "Your name";

    // Recipient Information
    $mail->AddAddress("to@recipient.com","Recipient Name");

    // Create the email message

    //  Tell it to use HTML
    $mail->IsHTML(true);

    // Email subject
    $mail->Subject "Email Subject";

    // Body - HTML message
    $mail->Body    "
    <html>
    <head>
    </head>
    <body>
    <h1>Hi there</h1>
    </body>
    </html>"
    ;

    // AltBody - Text message for those who won't/can't read HTML
    $mail->AltBody "Hi there!";

    //  Send the email
    $mail->Send();


  6. #6
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    london
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey

    I got it working - thanks!
    give me all your lentils

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    london
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm - but how can I include images ?

    I'd like to include them from my web server rather than as attachments with the email...
    give me all your lentils

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lentildal
    hmm - but how can I include images ?

    I'd like to include them from my web server rather than as attachments with the email...
    Just make sure you reference the image using your full web site path eg. http://www.yoursite.com/images/images.gif instead of /images/images.gif

    eg.
    PHP Code:
    $html_message "
    <html>
    <head>
    </head>
    <body>
    <h1>Here is an image from your web site!</h1>
    <img src=\"http://www.yoursite.com/images/image.gif\">
    </body>
    </html>"

    Note that you even link in stylesheets and javascript files from your web site as well. Just keep in mind that not all CSS or Javascript will work in HTML email ( eg. image rollovers)

    PHP Code:
    $html_message "
    <html>
    <head>
    <link rel=\"stylesheet\" href=\"http://www.yoursite.com/css/mystylesheet.css\">
    <script type=\"text/javascript\" src=\"http://www.yoursite.com/script/scriptfile.js\"></script>
    </head>
    <body>
    <h1 class=\"foo\">Here is an image from your web site!</h1>
    <img src=\"http://www.yoursite.com/images/image.gif\">
    </body>
    </html>"

    If you want to get really fancy, you can use an email class like PHPMailer with a template class like Fast Template for added flexibility in generating emails.

    However, I think that's for another discussion

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    london
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply.

    this has been bugging me for a while now. I've added regular html code as in your example but my images dont show up.

    I know my email client can view html images as I get other emails in this format.

    very annoying
    give me all your lentils

  10. #10
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    london
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh - stupid mistake - it works, thanks for all your help
    give me all your lentils


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •