使用PHP发送带附件的电子邮件 [英] Send email with attachement using PHP

查看:108
本文介绍了使用PHP发送带附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用PHP从联系表单发送附件中的电子邮件。

我有2个问题:

1-它在localhost上不起作用。

2-在托管网站的服务器上,只有在发送到GMAIL帐户但没有发送附件时才有效。

网站非常简单,只是HTML页面,这是我需要使用服务器代码发送电子邮件的唯一页面。





请告知我做错了什么

谢谢



我尝试过:



HTML code:

Hi All,

I'm using PHP to send an email from contact form with attachment.
I have 2 problems:
1- it doesn't work on localhost.
2- on the server where the website is hosted it worked only when sending to GMAIL account but without sending the attachment.
The website is very simple just HTML pages, this is the only page that I need to use server code to send an email.


Please advise what I'm doing wrong
Thank you

What I have tried:

HTML code:

<pre><!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' type='text/css' href='sendEmail.css'/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 
<script type='text/javascript' src='validateEmail.js'></script>
</head>
<body>
    <form name="contactform" class="contactform" method="post" onsubmit="return validateForm()"  action="sendEmail.php">
        <label>Sender Email:</label>
        <input type="text" id="semail" name="semail" />
        <label>Recipient Email:</label>
        <input type="text" id="remail" name="remail" value="" />
        <label for="Subject">Subject:</label>
        <input type="text" name="Subject" id="Subject" />
        <label for="Attachment">Attachment:</label>
        <input type="file" name="attach1" id="attach1" />
        <label for="Message">Message:</label><br />
        <textarea name="Message" rows="20" cols="20" id="Message"></textarea>
        <div class="submit-btn">
        <input type="submit"/>
        </div>
    </form>
</body>
</html>





< b> PHP代码:



PHP code:

<pre><?php
ini_set('display_errors', 1);
$senderEmail = filterInput($_POST['semail']);
$recipientEmail = filterInput($_POST['remail']); 
$message = filterInput($_POST['Message']);
$subject = filterInput($_POST['Subject']);
 
/* echo $senderEmail.$recipientEmail.$message.$subject; */
if(sendEmailWithAttachments($recipientEmail,$senderEmail,$subject,$message))
{
    echo "Email sent successfully!";
}
else
{
    echo "Failed to send the email...";
}
 
function filterInput($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
function sendEmailWithAttachments($recipientEmail,$senderEmail,$subject,$message){
 
     
    if(isset($_FILES)) {

        $allowedExtensions = array("pdf","doc","docx"); /*,"gif","jpeg","jpg","JPG","png","PNG","rtf","txt","xml"*/
 
        $files = array();
        foreach($_FILES as $name=>$file) {
            //die("file size: ".$file['size']);
            if($file['size']>=5242880)//5mb
            {
                $fileSize=$file['size'];
                return false;
            }
            $file_name = $file['name']; 
            $temp_name = $file['tmp_name'];
            $file_type = $file['type'];
            $path_parts = pathinfo($file_name);
            $ext = $path_parts['extension'];
            if(!in_array($ext,$allowedExtensions)) {
                return false;
                die("File $file_name has the extensions $ext which is not allowed");
            }
 
            //move the file to the server, cannot be skipped
            $server_file="/tmp/$path_parts[basename]";
            move_uploaded_file($temp_name,$server_file);
            array_push($files,$server_file);
            //array_push($files,$temp_name);
        }
 
        // email fields
        $headers = "From: $senderEmail";
 
 
        // boundary 
        $semi_rand = md5(time()); 
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
        // headers for attachment 
        $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
        // multipart boundary 
        $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
        $message .= "--{$mime_boundary}--\n";
 
        // preparing attachments
        for($x=0;$x<count($files);$x++){
            $file = fopen($files[$x],"rb");
            $data = fread($file,filesize($files[$x]));
            fclose($file);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
            "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            $message .= "--{$mime_boundary}\n";
        }
 
        // send
        return mail($recipientEmail, $subject, $message, $headers); /*@mail*/
 
    }
}   

推荐答案

senderEmail = filterInput(
senderEmail = filterInput(


_POST [' semail']);
_POST['semail']);


recipientEmail = filterInput(
recipientEmail = filterInput(


这篇关于使用PHP发送带附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆