简体   繁体   English

从 html 表单文件上传发送带有附件的 php 电子邮件

[英]Sending a php email with an attachment from a html form file upload

I know this is a very generic question but I need to make a online Time Card.我知道这是一个非常笼统的问题,但我需要制作一个在线考勤卡。 And need a file Upload field.并且需要一个文件上传字段。 I know I need to make the input on the html side, but don't know what to do after that.我知道我需要在 html 端进行输入,但不知道在那之后该怎么做。 I have worked with and sent php emails before but never with a file upload.我以前曾使用过并发送过 php 电子邮件,但从未使用过文件上传。 So I basically Need to know where to go from there(how to code the php email sending part).I would really appreciate a very simple explanation and any help is very much appreciated.所以我基本上需要知道从那里去哪里(如何编码 php 电子邮件发送部分)。我真的很感激一个非常简单的解释,非常感谢任何帮助。

Also I need it to send to the email that the employee lists.我还需要将其发送到员工列出的电子邮件中。 But I know that's part is very simple.但我知道那部分非常简单。

I have a live link that my client is currently using and they wanted me to add a file upload.我有一个我的客户当前正在使用的实时链接,他们希望我添加文件上传。 If you have any questions please lmk.如果您有任何问题,请 lmk。

<?php

if (isset($_POST['submit'])) {


//email varible above to for order of operations reasons

$email = $_POST['email'];
$to = "example@123.com, $email";
$subject = "New TimeSheet";
$name = $_POST['employee'];
$date = $_POST['date'];
$jobnum = $_POST['job#'];
$radio = $_POST['radiobtn'];
$customer = $_POST['customer'];
$address = $_POST['location'];
$jobdes = $_POST['jobdescription'];
$starttime = strtotime($_POST['starttime']);
$lunchtime = $_POST['lunch'];
$stoptime = strtotime($_POST['stoptime']);
$totalhours = $_POST['totalhours'];
$drivetime = $_POST['drivetime'];
$notes = $_POST['notes'];


$m1 = $_POST['m-1'];
$m2 = $_POST['m-2'];
$m3 = $_POST['m-3'];
$m4 = $_POST['m-4'];
$m5 = $_POST['m-5'];
$m6 = $_POST['m-6'];
$m7 = $_POST['m-7'];

$q1 = $_POST['q-1'];
$q2 = $_POST['q-2'];
$q3 = $_POST['q-3'];
$q4 = $_POST['q-4'];
$q5 = $_POST['q-5'];
$q6 = $_POST['q-6'];
$q7 = $_POST['q-7'];

$e1 = $_POST['e-1'];
$e2 = $_POST['e-2'];
$e3 = $_POST['e-3'];
$e4 = $_POST['e-4'];
$e5 = $_POST['e-5'];
$e6 = $_POST['e-6'];
$e7 = $_POST['e-7'];

$t1 = $_POST['t-1'];
$t2 = $_POST['t-2'];
$t3 = $_POST['t-3'];
$t4 = $_POST['t-4'];
$t5 = $_POST['t-5'];
$t6 = $_POST['t-6'];
$t7 = $_POST['t-7'];

$message = "<html><body>";
$message .= '<table rules="all" style="border-color: #666;" 

cellpadding="10">';
    $message .= "<tr><td><strong>Tech Name:</strong> </td><td>" .$name. "</td></tr>";
    $message .= "<tr><td><strong>Job #:</strong> </td><td>" .$jobnum. "</td></tr>";
    $message .= "<tr><td><strong>Date:</strong> </td><td>" . $date . "</td></tr>";
    $message .= "<tr><td><strong>Start Time:</strong> </td><td>" . date('h:i a', $starttime) . "</td></tr>";
    $message .= "<tr><td><strong>Lunch Duration:</strong> </td><td>" . $lunchtime . "</td></tr>";
    $message .= "<tr><td><strong>Stop Time:</strong> </td><td>" . date('h:i a', $stoptime) . "</td></tr>";
    $message .= "<tr><td><strong>Total Hours:</strong> </td><td>" . $totalhours . "</td></tr>";
    $message .= "<tr><td><strong>Drive Time:</strong> </td><td>" . $drivetime . "</td></tr>";
    $message .= "<tr><td><strong>Job Complete:</strong> </td><td>" . $radio . "</td></tr>";
    $message .= "<tr><td><strong>Customer:</strong> </td><td>" . $customer . "</td></tr>";
    $message .= "<tr><td><strong>Location/Address:</strong> </td><td>" . $address . "</td></tr>";
    $message .= "<tr><td><strong>Job Description:</strong> </td><td>" . $jobdes . "</td></tr>";
    $message .= "<tr><td><strong>Notes:</strong> </td><td>" . $notes . "</td></tr>";
    $message .= "<tr><td><strong>FILE:</strong> </td><td>" . $file . "</td></tr>";
    $message .= "<tr><td><strong>Materials:</strong></td> <td><strong>Qty:</strong></td> <td><strong>Each:</strong></td> <td><strong>Total:</strong></td></tr>";
$message .= "<tr><td>$m1</td><td>$q1</td><td>$e1</td><td>$t1</td>";
$message .= "<tr><td>$m2</td><td>$q2</td><td>$e2</td><td>$t2</td>";
$message .= "<tr><td>$m3</td><td>$q3</td><td>$e3</td><td>$t3</td>";
$message .= "<tr><td>$m4</td><td>$q4</td><td>$e4</td><td>$t4</td>";
$message .= "<tr><td>$m5</td><td>$q5</td><td>$e5</td><td>$t5</td>";
$message .= "<tr><td>$m6</td><td>$q6</td><td>$e6</td><td>$t6</td>";
$message .= "<tr><td>$m7</td><td>$q7</td><td>$e7</td><td>$t7</td>";
$message .= "</table>";
$message .= "</body></html>";



// Always set content-type when sending HTML email
$headers = "From: forms@gradyelectricforms.com\r\n";
$headers .= "Content-Type: text/html";
$file = $_POST['fileupload'];

mail( $to, $subject, $message, $headers );
header("location: http://www.gradyelectricinc.com",  true,  301 );  exit;
}

?>

it sounds like what you want is to create a html form with inputs for client info, including a form they will upload.听起来您想要的是创建一个带有客户端信息输入的 html 表单,包括他们将上传的表单。 Not sure if you are planning to send that same (pdf?) file to the person submitting the form, that part isn't clear to me.不确定您是否打算将相同的(pdf?)文件发送给提交表单的人,我不清楚那部分。

For email sending, I'd recommend using Sendgrid or Mailgun, these are SMTP services so that you don't have to use your own server for sending the emails.对于电子邮件发送,我建议使用 Sendgrid 或 Mailgun,它们是 SMTP 服务,因此您不必使用自己的服务器来发送电子邮件。 Many hosting providers, such as Google, prohibit SMTP sending from their servers so it's necessary to use a third party.许多托管服务提供商(例如 Google)禁止从其服务器发送 SMTP,因此有必要使用第三方。

Along those lines, I'd recommend using Sendgrid documentation and grabbing PHP code directly from them.按照这些思路,我建议使用Sendgrid 文档并直接从中获取 PHP 代码。 They have a Github library for sending mail , with examples to follow.他们有一个用于发送邮件Github 库,并附有示例。

For uploading forms via PHP, you can also search github for some helpful projects .通过PHP上传表单,你也可以在github上搜索一些有用的项目 That way you can have a starting point.这样你就可以有一个起点。

I hope this gives you a few places to start.我希望这能给你一些开始的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM