Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá pessoal. Estou fazendo um cadastro de curriculos. E estou tentando enviar email com arquivo em anexo + não to conseguindo. Já tentei vários códigos da web + nenhum funcionou... Alguem tem algum codigo q possa me passar?
Abaixo o codigo q to usando:
envia_anexo.php
>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="[http://www.w3.org/1999/xhtml">](http://www.w3.org/1999/xhtml%22)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Envio de e-mail com anexo</title>
</head>
<body>
<?php
if ($_POST) {
$arquivo =$_POST['attach'];
require('class/Mail_attach.php');
$attach = new Mail_attach($_POST['from'], $_POST['to'], $_POST['subject'], $_POST['message']);
if ($attach->sendMail($_FILES['attach'])) {
echo 'E-mail enviado com sucesso!';
} else {
echo 'Erro ao anexar arquivo ou enviar e-mail.<br>';
echo"$_POST[from]<br>";
echo"$_POST[subject]<br>";
echo"$_POST[message]<br>";
echo"$arquivo<br>";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<fieldset>
<p>
<label><input type="text" name="from" /> Remetente</label>
</p>
<p>
<label><input type="text" name="to" /> Destinatário</label>
</p>
<p>
<label><input type="text" name="subject" /> Assunto</label>
</p>
<p>
<input type="file" name="attach" />
</p>
<p>
<textarea cols="30" rows="7" name="message"></textarea>
</p>
<p><input type="submit" value="Enviar" /></p>
</fieldset>
</form>
</body>
</html>
Mail_attach.php
>
<?php
/**
* Send email with attachment
*
* @package Mail_attach
* @author Paulo Andre G Rodrigues <pauloandreget@gmail.com>
* @link http://www.paulorodrigues.eti.br
* @since February 12, 2010
* @version 20100212
*/
// ------------------------------------------------------------------------
class Mail_attach {
// Array with allowed mime type of attachment files
private $mime_files = array(
"html" => "text/html",
"htm" => "text/html",
"txt" => "text/plain",
"rtf" => "text/enriched",
"csv" => "text/tab-separated-values",
"css" => "text/css",
"gif" => "image/gif",
"jpg" => "image/pjpeg",
"png" => "image/png"
);
// Email attributes
private $__from;
private $__to;
private $__subject;
private $__message;
/**
* Constructor
*
* @access public
* @param string
* @param string
* @param string
* @param string
* @return void
*/
public function __construct($from, $to, $subject, $message) {
$this->__from = $from;
$this->__to = $to;
$this->__subject = $subject;
$this->__message = "--XYZ-" . date('dmyhms') . "-ZYX\n";
$this->__message .= "Content-Transfer-Encoding: 8bits\n";
$this->__message .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n\n";
$this->__message .= $message;
$this->__message .= "\n";
}
/**
* Make file attachment
*
* @access private
* @param array
* @return void||boolean
*/
private function attach($file = array()) {
if (!in_array($file['type'], $this->mime_files)) return false;
$fp = fopen($file['tmp_name'], 'rb');
$content = fread($fp, filesize($file['tmp_name']));
$encoded = chunk_split(base64_encode($content));
fclose($fp);
$this->__message .= "--XYZ-" . date('dmyhms') . "-ZYX\n";
$this->__message .= "Content-Type: " . $file['type'] . "\n";
$this->__message .= "Content-Disposition: attachment; filename=\"" . $file['name'] . "\" \n";
$this->__message .= "Content-Transfer-Encoding: base64\n\n";
$this->__message .= "$encoded\n";
$this->__message .= "--XYZ-" . date('dmyhms') . "-ZYX\n";
return true;
}
/**
* Send email
*
* @access public
* @param array||boolean
* @return boolean
*/
public function sendMail($file = false) {
if ($file['size'] > 0) {
if (!$this->attach($file)) {
return false;
}
}
$headers = "MIME-Version: 1.0\n";
$headers .= "From: <" . $this->__from . ">\r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"XYZ-" . date('dmyhms') . "-ZYX\"\r\n";
$mail = mail($this->__to, $this->__subject, $this->__message, $headers);
if ($mail) {
return true;
} else {
return false;
}
}
}
/ End of file Mail_attach.php /
/ Location: ./mail/class/Mail_attach.php /
Carregando comentários...