如何添加附件

时间:2015-10-17 13:07:00

标签: php jquery html css

在我之前的帖子中,我询问有关附件的电子邮件。这是我第一次做一个。为了简单起见,我不知道该怎么做。我已经完成了一些教程,但它似乎没有用。

我有一个联系页面,我会写下面的信息。它工作正常,但我需要添加附件

这是链接

http://jsfiddle.net/misaki03/e7mwn93u/

<?php

$to = "prettychii03@gmail.com";
$subject = isset($_POST['subject']) ? $_POST['subject'] : '';

$fname = isset($_POST['fname']) ? $_POST['fname'] : '';
$lname = isset($_POST['lname']) ? $_POST['lname'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$pnum = isset($_POST['pnum']) ? $_POST['pnum'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$emailType = isset($_POST['emailType']) ? $_POST['emailType'] : '';

$body = <<<Email

First Name: $fname
Last Name: $lname
Email Address: $email
Phone Number: $pnum
Message:
$message

Thanks,
$lname, $fname

Email;
$header = "From: $email";

$subject2 = $emailType." : ".$subject;
if($_POST){
    if($lname == '' || $fname == '' || $email == '' || $pnum == '' || $message == ''){

    }else{
        mail($to, $subject2, $body, $header);
        echo "Thank you for sending us an email! We have recieved your message.";
    }

}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My title</title>
<link href="style2.css" rel="stylesheet" />

</head>
<body>
<div id="container"> <!--wrapper-->
    <header id="header">
        <div id="menu" class="menu"> <!--navMenu-->
            <ul>
                <li><a href="index.php" > HOME </a></li>
                <li>OUR PRODUCT
                <ul> 
                    <li><a href="productOverview.php" > PRODUCT OVERVIEW</a></li>
                    <li><a href="usage.php" > CHEESECLOTH USAGE</a></li>
                    <li><a href="reviews.php" > PRODUCT REVIEWS </a></li>
                </ul>
                </li>
                <li> SUGGESTIONS 
                <ul>
                    <li><a href="tips.php" > TIPS and IDEAS </a></li>
                    <li><a href="recipe.php" > RECIPES </a></li>
                    <li><a href="ideas.php" > SEND US YOUR IDEAS </a></li>
                </ul>
                </li>
                <li><a href="about.php" > ABOUT US </a> </li>
                <li><a href="contact.php" > CONTACT US </a> </li>
            </ul> 
            <br class="clearFloat" />
        </div>
</header>

<section id="content">
        <div class="about" id="about">
        <center>
            <h2>- CONTACT US -</h2>
            <h4>Got any question or comments for us? Or do you have any suggestion that you would want us to share the world? Give us your thoughts and we might feature your work on our site! Don't hesitate to send us an email using the form below.
            <br /><br />
            <div id="contactUs">
            <form action="?" method="post">
                <table style="width:750px; border:0">
                    <tr>
                        <td><label for="fname">First Name : </label> </td>
                        <td><input type="text" name="fname" id="fname" required="required" /> </td>
                    </tr>
                    <tr>
                        <td><label for="lname">Last Name : </label></td>
                        <td><input type="text" name="lname" id="lname" required="required" /></td>
                    </tr>
                    <tr>
                        <td><label for="email">Email : </label></td>
                        <td><input type="text" name="email" id="email" required="required" /></td>
                    </tr>
                    <tr>
                        <td><label for="pnum"> Phone Number : </label></td>
                        <td><input type="text" name="pnum" id="pnum" required="required" /></td>
                    </tr>
                    <tr>
                        <td><label for="emailType">Email Type : </label></td>
                        <td>
                            <select name="emailType" required="required">
                                <option value="Comments">Comments</option>
                                <option value="Question">Question</option>
                                <option value="Recipe Suggestion">Recipe Suggestion</option>
                                <option value="Tips Suggestion">Tips Suggestion</option>
                                <option value="Other">Other</option>

                            </select>
                        </td>

                    </tr>
                    <tr>
                        <td><label for="subject">Subject : </label></td>
                        <td><input type="text" name="subject" id="subject" required="required" /></td>
                    </tr>
                    <tr>
                        <td><label for="message">Message : </label></td>
                        <td><textarea id="message" name="message" cols="42" rows="9" required="required"></textarea></td>
                    </tr>
                    <tr>
                        <td colspan="2"><div class="buttonSubmit" id="buttonSubmit">
                                <input type="submit" value="Submit">
                        </div></td>
                    </tr>
                </table>
            </form>
                <h3>Jungle Sales LLC / <br />
                    7495 W. Azure Dr. Suite 110<br />
                    Las Vegas, NV 89130</h3>
            </div>
        </center>
        </div>

</section>

<footer id="footer">
    <div id="fmenu" class="fmenu">
        <p>©2015 copyright    |  All Rights Reserved</p>
        <ul class="social_icons">
            <li class="social">
                <a href="#" target="_blank">
                    <img src="images/fb.png" /> </a></li>
            <li class="social">
                <a href="#" target="_blank">
                    <img src="images/pin.png" /> </a></li>
            <li class="social">
                <a href="#" target="_blank">
                    <img src="images/insta.png" /> </a></li>
        </ul>
</footer>
    </div>

</div>
</body>
</html>

我被告知要使用PHPMailer,如何在我的代码中添加它。这里提到的一条评论需要代码。但不确定我还应该添加什么,因为它与我学到的代码不同。对不起,谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

你应该考虑使用: https://github.com/PHPMailer/PHPMailer

使用此插件发送带附件的电子邮件要简单得多。

我的例子:

<?php

require 'PHPMailer/PHPMailerAutoload.php';
$email = new PHPMailer();
$email->From      = $email_from;
$email->FromName  = $name;
$email->Subject   = $subject;
$email->Body      = $body;

$email->AddReplyTo($email_from, $name);
$email->AddAddress( $receiver );

if(isset($_FILES['fileAttach'])){
    $name_file = $_FILES['fileAttach']['name'];
    $path_file  = $_FILES['fileAttach']['tmp_name'];

    $email->AddAttachment( $path_file ,$name_file );
}
$flgSend = $email->Send();
if($flgSend){
    //success
}else{
    //error
}