Envio de Mails
Versión 2 (Guillermo Zdanowicz, 04/10/2011 19:24)
| 1 | 1 | Guillermo Zdanowicz | h1. Envio de Mails |
|---|---|---|---|
| 2 | 1 | Guillermo Zdanowicz | |
| 3 | 2 | Guillermo Zdanowicz | $my_path="/ruta completa desde el raiz hasta el archivo/"; |
| 4 | 2 | Guillermo Zdanowicz | $my_file="archivo para adjuntar"; |
| 5 | 2 | Guillermo Zdanowicz | $my_name = "sistema"; |
| 6 | 2 | Guillermo Zdanowicz | $my_mail = "xxx@gmail.com"; |
| 7 | 2 | Guillermo Zdanowicz | $my_replyto = "yyy@gmail.com"; |
| 8 | 2 | Guillermo Zdanowicz | $my_subject = "asunto"; |
| 9 | 2 | Guillermo Zdanowicz | $my_message = "mi mensaje."; |
| 10 | 2 | Guillermo Zdanowicz | $this->mail_attachment($my_file, $my_path, "zzz@gmail.com", $my_mail, $my_name, $my_replyto, $my_subject, $my_message); |
| 11 | 2 | Guillermo Zdanowicz | |
| 12 | 1 | Guillermo Zdanowicz | |
| 13 | 1 | Guillermo Zdanowicz | |
| 14 | 1 | Guillermo Zdanowicz | |
| 15 | 1 | Guillermo Zdanowicz | function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { |
| 16 | 1 | Guillermo Zdanowicz | $file = $path.$filename; |
| 17 | 1 | Guillermo Zdanowicz | $file_size = filesize($file); |
| 18 | 1 | Guillermo Zdanowicz | $handle = fopen($file, "r"); |
| 19 | 1 | Guillermo Zdanowicz | $content = fread($handle, $file_size); |
| 20 | 1 | Guillermo Zdanowicz | fclose($handle); |
| 21 | 1 | Guillermo Zdanowicz | $content = chunk_split(base64_encode($content)); |
| 22 | 1 | Guillermo Zdanowicz | $uid = md5(uniqid(time())); |
| 23 | 1 | Guillermo Zdanowicz | $name = basename($file); |
| 24 | 1 | Guillermo Zdanowicz | $header = "From: ".$from_name." <".$from_mail.">\r\n"; |
| 25 | 1 | Guillermo Zdanowicz | $header .= "Reply-To: ".$replyto."\r\n"; |
| 26 | 1 | Guillermo Zdanowicz | $header .= "MIME-Version: 1.0\r\n"; |
| 27 | 1 | Guillermo Zdanowicz | $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; |
| 28 | 1 | Guillermo Zdanowicz | $header .= "This is a multi-part message in MIME format.\r\n"; |
| 29 | 1 | Guillermo Zdanowicz | $header .= "--".$uid."\r\n"; |
| 30 | 1 | Guillermo Zdanowicz | $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; |
| 31 | 1 | Guillermo Zdanowicz | $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; |
| 32 | 1 | Guillermo Zdanowicz | $header .= $message."\r\n\r\n"; |
| 33 | 1 | Guillermo Zdanowicz | $header .= "--".$uid."\r\n"; |
| 34 | 1 | Guillermo Zdanowicz | $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here |
| 35 | 1 | Guillermo Zdanowicz | $header .= "Content-Transfer-Encoding: base64\r\n"; |
| 36 | 1 | Guillermo Zdanowicz | $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; |
| 37 | 1 | Guillermo Zdanowicz | $header .= $content."\r\n\r\n"; |
| 38 | 1 | Guillermo Zdanowicz | $header .= "--".$uid."--"; |
| 39 | 1 | Guillermo Zdanowicz | if (mail($mailto, $subject, "", $header)) { |
| 40 | 1 | Guillermo Zdanowicz | echo "mail send ... OK"; // or use booleans here |
| 41 | 1 | Guillermo Zdanowicz | } else { |
| 42 | 1 | Guillermo Zdanowicz | echo "mail send ... ERROR!"; |
| 43 | 1 | Guillermo Zdanowicz | } |
| 44 | 1 | Guillermo Zdanowicz | } |