此问题的目的是查看如何通过SMTP中继将HTML表单中的已上传文件作为附件发送到电子邮件地址-文件类型无关紧要。我正在使用SendGrid而不是Composer。
所以我有一个HTML
编码的表单,其中包含doctype
,html
和form
标签。
带有其他input
类型(即text
,email
,tel
)的表单本身可以正常工作,但是当我添加input
标签时name="attachment"
属性以及如下的PHP代码不会发送附件:
<?php
if($_POST['submit'] && isset($_FILES['attachment'])){
$department = $_POST['department'];
$title = $_POST['title'];
$first = $_POST['first'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$number = $_POST['number'];
$userMessage = $_POST['userMessage'];
//Get uploaded file data using $_FILES array
$tmp_name = $_FILES['my_file']['tmp_name']; // get the temporary file name of the file on the server
$name = $_FILES['my_file']['name']; // get the name of the file
$size = $_FILES['my_file']['size']; // get size of the file for size validation
$type = $_FILES['my_file']['type']; // get type of the file
$error = $_FILES['my_file']['error']; // get the error (if any)
//validate form field for attaching the file
if($file_error > 0)
{
die('Upload error or No files uploaded');
}
$tmp_name = 'attachment'['tmp_name']; // get the temporary file name of the file on the server
$name = 'attachment'['name']; // get the name of the file
$size = 'attachment'['size']; // get size of the file for size validation
$type = 'attachment'['type']; // get type of the file
$error = 'attachment'['error']; // get the error (if any)
//read from the uploaded file & base64_encode content
$handle = fopen($tmp_name, "r"); // set the file handle only for reading the file
$content = fread($handle, $size); // reading the file
fclose($handle); // close upon completion
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("random"); // define boundary with a md5 hashed value
//header
$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
$headers .= "From:".$email_from."\r\n"; // Sender Email
$headers .= "Reply-To: ".$email_from."\r\n"; // Email addrress to reach back
$headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type
$headers .= "boundary = $boundary\r\n"; //Defining the Boundary
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($userMessage));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";
$body .= $encoded_content; // Attaching the encoded file with email
$email_from = 'xxxx';
$Password = 'xx';
$email_subject = "New Email Form Submission from $first.";
$email_body =
"User title: $title. \n".
"The department this message is for is: $department.\n".
"User First Name: $first.\n".
"User Surname Name: $surname. \n".
"User email: $email.\n".
"User Contact Number: $number.\n".
"User Message is: $userMessage.\n";
$to = 'xxx';
$headers = "From: $email_from \r\n";
$headers ="Reply to: $first $surname \r\n";
mail($to,$email_subject,$email_body, $headers);
header('xxx');
}
?>
<!doctype html>
<meta charset="utf-8">
<html>
<body>
<form action="../php/contact-us-main.php" enctype = "multipart/form-data" method="POST">
<input type="file" name="attachment" id="uploadBtn" multiple = "true" />
<input type="submit" class="" name="submit" value="Send Message"/>
</form>
</body>
</html>
我需要进行哪些更改才能真正确保表单将上传的文件作为附件发送到$to
部分中指定的收件人地址?
谢谢