通过我的php联系表单问题发送图像上传

时间:2019-01-26 17:37:22

标签: php file email upload

表格已经制作成功。我唯一尚未解决的是“上传图片”功能。如果要从网站上传的图像发送到我的电子邮件地址,我应该在PHP脚本中写些什么?如何以html格式上传图像以通过电子邮件发送?谢谢您的帮助!
我的html文件:

<div class="form-group">
<label for="name"> Image upload:</label>
<input name="attachment" type="file">
</div>

我的错误php文件:

<?php
$from = 'xxx@gmail.com>';
$sendTo = 'xxx@gmail.com';
$subject = 'xxx';

$fields = array(
'name' => 'Name', 
'email' => 'E-mail', 
'phonenumber' => 'Tel', 
'attachment' => 'Uploaded File' ,
'message' => 'Message'); 

$okMessage = 'Ok!';
$errorMessage = 'Error!';

try
{
    $emailText = nl2br("new message!\n");
    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= nl2br("$fields[$key]: $value\n");
        }
    }

    $headers = array('Content-Type: text/html; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}

0 个答案:

没有答案