Get Email Message from File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrsjrs
    New Member
    • Sep 2006
    • 24

    Get Email Message from File

    <?php $to = "someone@exampl e.com";
    $subject = "Test mail";
    $message = "Hello! This is a simple email message.";
    $from = "someonelse@exa mple.com";
    $headers = "From: $from";
    mail($to,$subje ct,$message,$he aders);
    echo "Mail Sent.";?>

    The above is the standard example for a simple email sender.
    I have long letters in text and doc formats. Call them letter.txt. or letter.doc
    How can I have the computer read the documents into the "$message" line?
    The following line does not work.
    $message=letter .txt;
    How can I get it to work properly?
    Or is this just impossible to do?
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Use the file_get_conten ts command, reading the entire file into a string.

    [PHP]<?php
    $to = "someone@exampl e.com";
    $subject = "Test mail";
    $message = file_get_conten ts("file.txt") ;
    $from = "someonelse@exa mple.com";
    $headers = "From: $from";
    if (mail($to,$subj ect,$message,$h eaders))
    echo "Mail Sent";
    else
    echo "Mail message failed";
    ?>[/PHP]

    Info on the command (and error handling) can be found at http://nl2.php.net/manual/en/functio...t-contents.php

    Ronald :cool:

    Comment

    • jrsjrs
      New Member
      • Sep 2006
      • 24

      #3
      Thank you very much ronverdonk!
      That was a completely new php command for me.
      I spent at least 6 hours trying to solve this problem unsuccessfully.
      Incidentally, it works fine with text files, but not with .doc files.
      I learn something new every day.
      Thanks also to this great website - "thescripts ".

      Comment

      Working...