2014-01-16 33 views
0

发送我给自己设定了一个小PHP项目。我的项目的目的是创建从目录中加载HTML电子邮件一个webapp并发送出去。PHP装载目录选择HTML文件,并通过邮件

web应用程序被发送HTML电子邮件文件,只有当他们硬编码的,但我想创造加载从目录和用户的所有HTML文件可以选择要发送的一个功能。该函数加载的所有文件在下拉菜单中显示,但发送的电子邮件为空白/空白。

PHP代码:

<?php 
$to = $_POST['recipient']; 
$subject = $_POST['subject']; 

function LoadTemplate() 
    { 
    foreach(glob(dirname(__FILE__) . '/templates/*') as $filename) 
     { 
     $filename = basename($filename); 
     echo "<option value='" . $filename . "'>" . $filename . "</option>"; 
     } 

    $message = file_get_contents($filename, "r") or exit("Unable to open file"); 
    return $message; 
    } 

$headers = "Content-type: text/html\r\n"; 
mail($to, $subject, $message, $headers); 
?> 

HTML代码:

<html> 
<head></head> 
<body> 
    <h2>Mail</h2> 
    <form name="form1" method="post" action="mail.php">Send To: 
     <input type="text" id="recipient" name="recipient"> 
     <br>Subject: 
     <input type="text" id="subject" name="subject"> 
     <br> 
     <select name="s1"> 
      <option value="" selected="selected">-----</option> 
      <?php require('mail.php'); echo LoadTemplate(); ?> 
      <br> 
      <input type="submit" value="Send"> 
     </select> 
    </form> 
</body> 
</html> 

所以我想从一个目录中加载HTML文件到一个下拉菜单,然后在发送文件通过电子邮件解决个人问题,以显示HTML内容。

回答

2

试试这个代码

<?php 


function LoadTemplate() 
    { 
    foreach(glob(dirname(__FILE__) . '/templates/*') as $filename) 
     { 
     echo "<option value='" . $filename . "'>" . basename($filename) . "</option>"; 
     } 
    } 
if(isset($_REQUEST['submit'])){ 
    $to = $_POST['recipient']; 
    $subject = $_POST['subject']; 
    $message = file_get_contents($_REQUEST['s1'], "r") or exit("Unable to open file"); 
    $headers = "Content-type: text/html\r\n"; 
    $mail_send = mail($to, $subject, $message, $headers); 
    if($mail_send){ 
    echo 'Mail Send '; 
    }else{ 
    echo 'Try Later'; 
    } 
} 
?> 


<html> 
<head></head> 
<body> 
    <h2>Mail</h2> 
    <form name="form1" method="post" action="mail.php">Send To: 
     <input type="text" id="recipient" name="recipient"> 
     <br>Subject: 
     <input type="text" id="subject" name="subject"> 
     <br> 
     <select name="s1"> 
      <option value="" selected="selected">-----</option> 
      <?php require('mail.php'); echo LoadTemplate(); ?> 
      <br> 
      <input type="submit" value="Send" name='submit'> 
     </select> 
    </form> 
</body> 
</html> 

不要寻求帮助,如果无法实现浪费我的精力

+0

嘿感谢你提出的代码,但是当我通过我的收件箱 – Tomazi

+0

发送电子邮件nothing快到我已编辑的代码尝试现在 – sanjeev

+0

完美谢谢 – Tomazi

0

所以你基本上要选择的静态HTML文件发送作为电子邮件的内容?这应该是一件容易的事。你的PHP代码不是,你想要它做什么。而你的源代码有一些问题。让我们尝试把它清理干净一点:

<?php 

    // if form was submitted, send the mail 
    if(isset($_REQUEST['submit'])){ 
     $to = $_POST['recipient']; 
     $subject = $_POST['subject']; 
     $message = file_get_contents($_REQUEST['s1'], 'r') or exit('Unable to open file'); 
     $headers = "Content-type: text/html\r\n"; 
     if(mail($to, $subject, $message, $headers)){ 
      $mail_sent = true; 
     } else { 
      $mail_sent = false; 
     } 
    } else { 
     // read all templates from the folder 
     foreach(glob(dirname(__FILE__) . '/templates/*') as $filename){ 
      $templates = basename($filename); 
     } 
    } 
?> 
<html> 
    <head></head> 
    <body> 
     <h2>Mail</h2> 
     <?php if(isset($_REQUEST['submit'])) : ?> 
      <?php if($mail_sent) : ?> 
       <p>The mail has been sent successfully</p> 
      <?php else : ?> 
       <p>There has been an error sending the mail.</p> 
      <?php endif ?> 
     <?php endif ?> 
     <form name="form1" method="post" action="mail.php">Send To: 
      <input type="text" id="recipient" name="recipient"> 
      <br>Subject: 
      <input type="text" id="subject" name="subject"> 
      <br> 
      <select name="s1"> 
       <option value="" selected="selected">-----</option> 
       <?php foreach($templates as $template) : ?> 
       <option value="<?php echo $template ?>"><?php echo $template ?></option> 
       <? endforeach ?> 
      </select> 
      <br> 
      <input type="submit" name="submit" value="Send"> 
     </form> 
    </body> 
</html> 
+0

是的我同意的代码需要一些适当的清洗和重组,但对于具有基础工作的目的,我在最烂的方式编码也无妨.......使用您的更改不会发送电子邮件和误差修改的消息抛出无法打开文件..... – Tomazi

+0

看起来像选项不拿起空数据插入 – Tomazi

+0

你想打印出$ _POST阵列?是模板集。 – 2ndkauboy