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

时间:2019-02-19 01:36:24

标签: php html

我知道这是一个非常笼统的问题,但是我需要制作一个在线时间卡。并需要一个文件上传字段。我知道我需要在html端进行输入,但是在此之后不知道该怎么办。我之前曾使用过php电子邮件,但从未上传过文件。所以我基本上需要知道从那里去(如何编写php电子邮件发送部分的代码)。我真的很感谢一个非常简单的解释,非常感谢任何帮助。

我还需要它发送给员工列出的电子邮件。但是我知道那部分非常简单。

我的客户当前正在使用实时链接,他们希望我添加文件上传。如有任何疑问,请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;
}

?>

1 个答案:

答案 0 :(得分:1)

听起来,您想要创建一个带有客户信息输入的html表单,包括他们将上传的表单。不知道您是否打算将相同(pdf?)文件发送给提交表单的人,我不清楚那部分内容。

对于电子邮件发送,我建议使用Sendgrid或Mailgun,它们是SMTP服务,因此您不必使用自己的服务器来发送电子邮件。许多托管服务提供商(例如Google)都禁止从其服务器发送SMTP,因此有必要使用第三方。

沿着这些行,我建议使用Sendgrid documentation并直接从中获取PHP代码。他们有一个Github library for sending mail,并附有示例。

要通过PHP上传表单,您也可以search github for some helpful projects。这样,您可以有一个起点。

我希望这可以给您一些起点。