PHP - Ayuda a modificar Script Php AttachMailer que envia pdf vacio

 
Vista:
Imágen de perfil de EDUARDO ARROYO
Val: 165
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

Ayuda a modificar Script Php AttachMailer que envia pdf vacio

Publicado por EDUARDO ARROYO (175 intervenciones) el 27/08/2020 00:57:38
Hola a todos como están, espero que muy bien, espero me puedan ayudar.. tengo este formulario que envía mensajes a correos electrónicos con adjuntos, y llama a otros archivos que son los que hacen el proceso.. (pongo mas abajo) pero sucede algo (explico mas abajo también) el código lo vi en una web y funciona perfecto solo por el problema que espero me puedan ayudar a solucionar....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html lang="es">
<head>
	<meta charset="UTF-8">
	<title>Envio de Mensajes</title>
	<style>
		body {
			background: #f5f5f5;
			font-family: Arial, Verdana, sans-serif;
		}
		#container {
			background: #fff;
			border: 1px solid #cecece;
			max-width: 1170px;
			margin: 20px auto 0 auto;
			padding: 20px;
			text-align: center;
		}
		h1 {
			color: #636363;
		}
		a {
			color: #9fcd32;
			text-decoration: none;
		}
		a:hover {
			text-decoration: underline;
		}
		form {
			max-width: 500px;
			margin: 0 auto;
		}
		form fieldset {
			background: #f5f5f5;
		}
		form legend {
			color: red;
			font-size: 20px;
			font-weight: bold;
			text-transform: uppercase;
		}
		form ul {
			padding: 0px;
		}
		form ul li {
			list-style-type: none;
			text-align: left;
			margin-top: 10px;
		}
		form ul li:last-child {
			text-align: center;
		}
		form ul li input[type="text"],form ul li input[type="email"], form ul li textarea {
			border: 1px solid #cecece;,
			border-radius: 3px;
			-webkit-border-radius: 3px;
			-moz-border-radius: 3px;
			-o-border-radius: 3px;
			padding: 7px;
			width: 96%;
		}
		form ul li input[type="submit"] {
			background: #FC6A00;
			border: none;
			color: #fff;
			cursor: pointer;
			font-size: 16px;
			font-weight: bold;
			padding: 10px;
			text-transform: uppercase;
			width: 50%;
		}
		.msn-ok, .msn-ko {
		    color: #fff;
		    padding: 10px;
		    text-align: center;
		    width: 65%;
		    margin: 20px auto;
		}
		.msn-ok {
			background: #22AF22;
		}
		.msn-ko {
			background: #CC2F2F;
		}
	</style>
   </head>
<body>
	<div id="container">
	  <h1><br>
	    <?php
			//si se ha enviado el correo
			if(isset($_REQUEST['s'])){
				$ok = $_REQUEST['s'];
				if($ok == 'si'){
					echo "<div class='msn-ok'>¡Mensaje enviado correctamente!</div>";
				}
				elseif($ok == 'no'){
					echo "<div class='msn-ko'>Ha habido un error de envio...</div>";
				}
			}
		?>
	  </h1>
	  <form action="send.php" method="post" enctype="multipart/form-data">
	    <fieldset>
	      <legend>nOMBRE:
			<input name="nombre" type="text" id="nombre" value="">
				<br>
				eMAIL:
				<input name="email" type="text" id="email" value="">
				<br>
eMAIL rECIBE:
<input name="emailr" type="text" id="emailr" value="">
			  <br>
			  <label for="asunto"><br>
			      <br>
		        Asunto:</label>
		    <br>
		    <input type="text" id="asunto" name="asunto" placeholder="Asunto" required>
		    <br>
		    <br>
          <label for="adjunto">Archivo adjunto</label>
            <br>
            <input type="file" id="adjunto" name="adjunto">
          </legend>
	      <ul>
			  <li>
				  <label for="msn">Mensaje:</label>
				  <br>
				  <textarea name="msn" id="msn" rows="10" required></textarea>
		    </li>
			  <li>
				  <input type="submit" value="Enviar">
			  </li>
			</ul>
			</fieldset>
		</form>
	</div>
</body>
</html>

el cual recoje los datos y los envia a correo electronico con archivo adjunto ( a continuacion pongo los archivos que hacen el proceso.. )


send.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
 
class Email {
 
	//nombre
	var $nombre;
	//email del emisor
	var $mail;
	//email del receptor
	var $mailr;
	var $asunto;
	//mensaje
	var $msn;
	//archivo adjunto
	var $adjunto;
	//enviar el mensaje
	private $sender;
	//url para redireccionar
	private $url;
 
	//función constructora
	public function __construct(){
		//cada uno de ellos es el parámetro que enviamos desde el formulario
		$this->nombre = $n;
		$this->mail = $m;
		$this->mailr = $mr;
		$this->asunto = $a;
		$this->msn = $ms;
		$this->adjunto = $ad;
	}
 
	//método enviar con los parámetros del formulario
	public function enviar($n,$m,$mr,$a,$ms,$ad){
		//si existe post
		if(isset($_POST)){
 
			//si existe adjunto
			if($ad) {
				//añadimos texto al nombre original del archivo
				$dir_subida = 'fichero_';
				//nombre del fichero creado -> fichero_nombreArchivo.pdf
				$fichero_ok = $dir_subida . basename($ad);
				//y lo subimos a la misma carpeta
				move_uploaded_file($_FILES['adjunto']['tmp_name'], $fichero_ok);
			}
			//creamos el mensaje
			$contenido = '
				<h2>Nuevo mensaje de: '.$n.'</h2>
				<hr>
				<b>Email: </b>'.$m.'<br>
				<b>Asunto: </b>'.$a.'<br>
				<b>Mensaje: </b>'.$ms.'<br>
			';
			//archivo necesario para enviar los archivos adjuntos
			require_once 'AttachMailer.php';
 
			//enviamos el mensaje           (emisor,receptor,asunto,mensaje)
			$this->sender = new AttachMailer($m, $mr, $a, $contenido);
			$this->sender->attachFile($fichero_ok);
			//eliminamos el fichero de la carpeta con unlink()
			//si queremos que se guarde en nuestra carpeta, lo comentamos o borramos
			unlink($fichero_ok);
			//enviamos el email con el archivo adjunto
			$this->sender->send();
			//url para redireccionar
			$this->url = 'http://web.com/email/';
			//redireccionamos a la misma url conforme se ha enviado correctamente con la variable si
			header('Location:'.$this->url.'?s=si');
		}
		else{
			//redireccionamos a la misma url conforme NO se ha enviado correctamente con la variable no
			header('Location:'.$this->url.'?s=no');
		}
	}
}
 
//llamamos a la clase
$obj = new Email();
//ejecutamos el método enviar con los parámetros que recibimos del formulario
$obj->enviar($_POST['nombre'], $_POST['email'], $_POST['emailr'], $_POST['asunto'], $_POST['msn'], $_FILES['adjunto']['name']);
 
?>



AttachMailer.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
 
class AttachMailer{
 
    private $from, $to, $subject, $mess, $hash, $output;
    private $documents = Array();
 
    /*
     * @params from: adresse de l'envoyeur(+reponse)
     * to : adresse a qui on envoie
     * subject : le sujet du message
     * mess : le message lui meme(format html)
     */
    function __construct($_from, $_to, $_subject, $_mess){
        $this->from = $_from;
        $this->to = $_to;
        $this->subject = $_subject;
        $this->mess = $_mess;
        $this->hash = md5(date('r', time()));
    }
    /*
     * @params url du document ajouté
     */
    public function attachFile($url, $name = ""){
        $attachment = chunk_split(base64_encode(file_get_contents($url)));
        $docName = $name == "" ? basename($url) : $name;
        $randomHash = $this->hash;
        $docOutput = "--PHP-alt-$randomHash--\r\n\r\n"
                     ."--PHP-mixed-$randomHash\r\n"
                     ."Content-Type: application/pdf; name=\"$docName\" \r\n"
                     ."Content-Transfer-Encoding: base64 \r\n"
                     ."Content-Disposition: attachment \r\n\r\n"
                     .$attachment . "\r\n";
        $this->documents[] = $docOutput;
    }
    private function makeMessage(){
        $randomHash = $this->hash;
        $messageOutput = "--PHP-mixed-$randomHash\r\n"
                         ."Content-Type: multipart/alternative; boundary=PHP-alt-$randomHash\r\n\r\n"
                         ."--PHP-alt-$randomHash\r\n"
                         ."Content-Type: text/plain; charset='iso-8859-1'\r\n"
                         ."Content-Transfer-Encoding: 7bit\r\n\r\n"
                         .$this->mess . "\r\n\r\n"
                         ."--PHP-alt-$randomHash\r\n"
                         ."Content-Type: text/html; charset='iso-8859-1'\r\n"
                         ."Content-Transfer-Encoding: 7bit\r\n\r\n"
                         . $this->mess . "\r\n";
        foreach($this->documents as $document){
            $messageOutput .= $document;
        }
        $messageOutput .="--PHP-mixed-$randomHash;--";
        $this->output = $messageOutput;
    }
    public function send(){
        $this->makeMessage();
        $from = $this->from;
        $randomHash = $this->hash;
        $headers = "From: $from\r\nReply-To: $from";
        $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-$randomHash\"";
        $mail_sent = @mail( $this->to, $this->subject, $this->output, $headers );
        return $mail_sent ? true : false;
    }
}


EL PROBLEMA:
Lo que necesito es no se en que parte modificar la linea para que al enviar un correo sin adjunto este se envíe sin enviar un pdf vacio que genera automáticamente (ATT00001.pdf) (mientras se envía con adjunto si envía el archivo sin problema)

espero me puedan ayudar!!!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder