附件保存在变量中,wp_mail()未被发送

时间:2016-09-28 18:37:26

标签: php wordpress phpmailer

在多次阅读wp_mail的文档并寻找解决方案之后,我仍然无法弄清楚如何使用wp_mail()附加文件;

首先,我的理解是需要文件路径,但我怎么知道它在wordpress网站上的保存位置?当它被保存时,是暂时的还是永久性的?

目前我只使用$attachments = array(WP_PLUGIN_DIR . '/uploads/file_to_attach.zip'); 因为我在stackoverflow上的另一篇文章中看到了它。但这意味着我没有使用我的$_POST['file']  其中应存储路径。

HTML

<form method="post" role="form" action=''>
        <div class="col-xs-12 col-sm-6 form-group">
            <label>Navn</label>
            <input class="form-control" name="name" type="text" value="<?php echo $_POST['name']; ?>"  required/>
        </div>

        <div class="col-xs-12 col-sm-6 form-group">
            <label>E-mail</label>
            <input class="form-control" name="email" type="email" value="<?php echo $_POST['email']; ?>" required/>
        </div>

        <div class="col-xs-12 col-sm-6 form-group">
            <label>Tlf.</label>
            <input class="form-control" name="phone" type="tel" value="<?php echo $_POST['phone']; ?>" required/>
        </div>

        <div class="col-xs-12 col-sm-6 form-group">
            <label>Firma</label>
            <input class="form-control" name="company" type="text" value="<?php echo $_POST['company']; ?>" />
        </div>

        <div class="col-xs-12 form-group">
            <label>Besked</label>
            <textarea class="form-control" name="message" type="text" rows="6" required><?php echo $_POST['message']; ?></textarea>
        </div>

        <div class="col-xs-12 form-group">
        <div class="input-group">
                <label class="input-group-btn">
                    <span class="btn btn-primary">
                        Vedhæft billeder&hellip; <input type="file" name="file" style="display: none;" multiple>
                    </span>
                </label>
                <input type="text" class="form-control" readonly>
            </div>
        </div>

        <div class="col-xs-12" id="checkrow">
            <label>
                <input type="checkbox" name="check" required> Jeg er et menneske
            </label>
        </div>

        <div class="col-xs-4 form-group" style="padding-top:1rem;">
            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary" style="min-width:5rem">
        </div>

        <?php echo $result; ?>
</form>

PHP     

$to = "ch@ap-pe.dk";
$msg = "Navn: ".$_POST['name']."
        Email: ".$_POST['email']."
        Tlf: ".$_POST['phone']."
        Firma: ".$_POST['company']."
        Besked: ".$_POST['message'];
$msg = wordwrap($msg, 70);
$headers = 'From: ';
$attachments = array(WP_PLUGIN_DIR . '/uploads/file_to_attach.zip');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    if (!$_POST['name']){
        $errorMsg="Indtast navn";
    }
        if (!$_POST['email']){
        $errorMsg.="Indtast email";
    }
        if (!$_POST['message']){
        $errorMsg.="Indtast besked";
    }
        if (!$_POST['check']){
        $errorMsg.="Bekræft at du er menneske";
    }

    $result = "Ret følgende fejl: $errorMsg";

    wp_mail($to, "Kontakt Besked",$msg, $headers, $attachments);



}
?>

0 个答案:

没有答案