【问题标题】:How to add email with attachment [duplicate]如何添加带有附件的电子邮件[重复]
【发布时间】:2016-01-16 04:19:05
【问题描述】:

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

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

这是链接

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,如何在我的代码中添加它。这里提到的一条评论需要代码。但不确定我还应该添加什么,因为它与我学到的代码不同。抱歉,感谢您的帮助。

【问题讨论】:

  • 那个条件好吗?? $_FILES[['attachment']['name']] => $_FILES['attachment']['name']
  • 请尽量将您的问题简化为描述问题所需的内容。我怀疑 CSS 与您的问题有什么关系,jsfiddle 也没有,因为它用于 JavaScript 而不是 PHP ;-)...阅读此处了解更多信息:stackoverflow.com/help/how-to-ask
  • 这真的很难解释。保持简单。我不知道如何制作带有附件的联系我们页面。我尝试了不同的教程,但似乎不起作用
  • 我已经澄清了我的问题@mario。抱歉,如果它令人困惑或看起来像重复。

标签: php jquery html css


【解决方案1】:

您应该考虑使用: 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
}

【讨论】:

  • 我试过这个但很困惑。你能教我如何使用这个@fico7489 吗?
  • 看看我编辑的答案。
  • 哦,所以我只需要添加require。这是我拥有的当前工作代码(没有附件)我只需在上面添加这些代码吗? (链接在这里-jsfiddle.net/misaki03/e7mwn93u
  • 你说的我已经试过了。我根据我的需要调整了它。它在我的 wamp 服务器上工作正常但是,一旦我将它上传到域上,它就会给我这个错误“服务器错误 500。它可能因维护而停机或配置不正确。”这是我使用的代码。 jsfiddle.net/misaki03/e7mwn93u/2
猜你喜欢
  • 2012-11-28
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 2014-06-03
  • 2020-06-04
  • 2011-04-14
相关资源
最近更新 更多