如何使用php将表单数据上传文件发送到gmail?

时间:2018-08-27 11:23:23

标签: php forms file

如何使用php上传文件到gmail?我是php的新手,尝试了以下代码。我无法发送文件,只能发送文件名。请帮助我实现这一目标。还有其他解决方案吗?

<?php 
if(isset($_POST['submit'])){
    $to = "xyz@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $name = $_POST['name'];
	$subject= $_POST['subject'];
	
    
    
    $subject2 = "Copy of your form submission";
    $upload = $name . " wrote the following:" . "\n\n" . $_POST['upload'];
    $upload2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['upload'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$upload,$headers);
    mail($from,$subject2,$upload2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $name . ", I will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    // You cannot use header and echo together. It's one or the other.
    }
?>
<html>

<head>
    <title>Test</title>
    <title>Demo</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt"
        crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <!-- Popper JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

</head>

<body>
    <center>
        <div class="contact-us">
            <div class="row">
                <div class="col-md-4 col-md-offset-4">
                    <div class="contact-form">
                        <form action="mailer.php" method="post">
                            <div class="form-group">
                                <input name="name" type="text" required autocomplete="off" class="form-control" placeholder="Your Name">
                            </div>
                            <div class="form-group">
                                <input name="email" type="email" required autocomplete="off" class="form-control" placeholder="Email Address">
                            </div>
                            <div class="form-group">
                                <input name="subject" type="text" required autocomplete="off" class="form-control" placeholder="Your Subject">
                            </div>
                            <div class="form-group">
                                <input type="file" accept=".doc, .docx, .pdf" class="form-control" name="upload">
                            </div>
                            <div class="custom-btn">
                                <button name="submit" type="submit" class="btn">Send Now</button>
                            </div>
                        </form>
                    </div>
                </div>
              </div>
            </div>
    </center>
</body>

</html>

0 个答案:

没有答案