将用户上传的照片从表单发送到电子邮件,作为附件与梨邮件

时间:2015-09-04 20:38:00

标签: php image email email-attachments pear-mail

我遇到了一个问题,我有一个用户上传图片的表单,并希望将其作为附件发送到我们的电子邮箱。由于我是Pear Mail的新手(我的托管服务提供商要求),我需要一些关于如何从下面的代码开始的建议,因为我已经编写了两个我认为可行的部分,但只是发送服务器错误。请记住,我的主机已经在服务器上安装了所有Pear邮件。代码如下:

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>
<?php
if(isset($_POST['email'])) {
    require_once "Mail.php"; //PEAR mail is already installed in our current environment
    require_once "mime.php";

    $email_to = "";  //Enter the email you want to send the form to
    $email_subject = "Pawn Request";  // You can put whatever subject here

    $host = "smtp.hostmanagement.net";  // The hostname of your mail server
    // If your email is with HostMySite, use the actual hostname of your mail server, for example:
    //   SmarterMail: mailXX.safesecureweb.com OR win-mailXX.hostmanagement.net
    //   OpenX-change: smtp.hostmanagement.net
    // Contact Support if you aren't sure what to enter here.

    $username = "";  // A valid email address you have setup
    $from_address = "";  // If your mail is hosted with HostMySite this has to match the email address above
    $password = "";  // Password for the above email address
    $reply_to = "";  //Enter the email you want customers to reply to
    $port = "587"; // This is the default port. Try port 50 if this port gives you issues and your mail is hosted with HostMySite

    function died($error) {
        // your error code can go here
        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();
    }

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

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $price = $_POST['price']; // not required
    $email = $_POST['email']; // required
    $mobile = $_POST['phone']; // not required
    $message = $_POST['message']; // required
    $image = $_POST['image'];

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    $string_exp = "/^[A-Za-z .'-]+$/";
    if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }
    if(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
    }
    if(strlen($message) < 2) {
        $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }
    if(strlen($error_message) > 0) {
        died($error_message);
    }
    $email_message = "Form details below.\n\n";
    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Price: ".clean_string($price)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Phone Number: ".clean_string($mobile)."\n";
    $email_message .= "Comments: ".clean_string($message)."\n";

    // This section creates the email headers
    $auth = array('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password, 'port' => $port);
    $headers = array('From' => $from_address, 'To' => $email_to, 'Subject' => $email_subject, 'Reply-To' => $reply_to);

    // This section send the email
    $smtp = Mail::factory('smtp', $auth);
    $mail = $smtp->send($email_to, $headers, $email_message);
    $mail->addHTMLImage($image);

    if (PEAR::isError($mail)) {?>
<!-- include your own failure message html here -->
<?php
header('location:fail.htm');
?>
<!-- Uncomment the line below to see errors with sending the message -->
<!--<?php echo("<p>". $mail->getMessage()."</p>"); ?> -->

<?php } else { ?>
<?php
header('location:success.htm');
?>
<?php } } ?>

我已删除托管所需信息,原因很明显。感谢您提供的任何帮助!

编辑: 这是我的图像上传的HTML端代码,如果这有助于任何。

`<div class="form-group"> 
<label class="control-label">Upload Photos (required)  </label>
<input name="image" id="input-22" type="file"  class="file" accept="image/*" multiple=true data-preview-file-type="text" data-preview-class="bg-warning">
</div>`

0 个答案:

没有答案