0

我创建了一个简单的联系表单,需要添加用户通过电子邮件提交向我发送一些图像的功能。这是表格:

<form name="submission_form" method="post" action="./contact.php">
    <h3>Submission</h3>
    <span><input class="data" type="email" name="email" placeholder="Email"></span>
    <span><input class="data" type="text" name="first_name" placeholder="First Name"></span>
    <span><input class="data" type="text" name="last_name" placeholder="Last Name"></span>
    <span><input class="data" type="text" name="address" placeholder="Address"></span>
    <span><input class="data" type="text" name="city" placeholder="City"></span>
    <span><input class="data" type="text" name="state" placeholder="State"></span>
    <span><input class="data" type="text" name="zip_code" placeholder="Zip Code"></span>
    <span><input class="submit" type="submit" value="Submit"></span>
</form>

这是到目前为止的contact.php文件:

<?php
    if(isset($_POST['email'])) {

    $email_to = "person@email.com";

    $email_subject = "Mom's Submission";

    function died($error) {
    // error code
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['email']) ||
    !isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['address']) ||
    !isset($_POST['city']) ||
    !isset($_POST['state']) ||
    !isset($_POST['zip_code'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
}

$email = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip_code = $_POST['zip_code'];


$error_message = "";

if(strlen($error_message) > 0) {
    died($error_message);
}
$email_message = "You've received a submission yo!!\n\n";

function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
}

$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Zip Code: ".clean_string($zip_code)."\n";


// email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

我试过简单地添加:

<input name="image" type="file">

到 HTML 并添加:

!isset($_POST['image'])

$image = $_POST['image'];

到 config.php 文件,但我不确定在那之后该怎么做以确保附件与电子邮件一起传递。任何人都可以帮忙吗?

4

1 回答 1

0

首先,您需要使用从 $_FILES 加载到服务器上的文件。

然后您必须知道如何将文件附加到电子邮件,如下所示:

//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email 
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n  
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 


-PHP-mixed- 
Content-Type: multipart/alternative; boundary="PHP-alt-"

--PHP-alt- 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message.

--PHP-alt- 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt---

--PHP-mixed- 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment


--PHP-mixed---

//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>

还有很多关于在互联网上附加文件的相关信息。

于 2013-01-22T09:56:02.607 回答