PHP加载目录选择HTML文件并通过Mail发送

时间:2014-01-16 11:34:00

标签: php html email

我将自己设定为一个小PHP项目。我的项目的目的是创建一个webapp,它从目录中加载HTML电子邮件并将其发送出去。

webapp仅在硬编码时发送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 个答案:

答案 0 :(得分: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>

如果无法实施浪费我的努力,请求帮助

答案 1 :(得分: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>