[PHP] MIME + attachment komt niet goed aan

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • mr_wizard
  • Registratie: Februari 2003
  • Laatst online: 29-04 21:17
Ik heb een MIME script geschreven om MIME email met een attachment te kunnen versturen. De attachment moet iedere attachment kunnen zijn.

Als ik nu verstuur komt het allemaal prima aan, filesize in de attachment is OK, maar als ik hem open geeft het desbetreffende programma wat hem opend een foutmelding! Alleen HTM, HTML, TXT en....ZIP komen in goede vorm aan?!

Ik heb encoding op 'base64' en c_type op de file type van de file, die klopt wel. Maar waarom lukt het met ZIP bijvoorbeeld wel? op JPG en EXE crasht hij weer?!?

R&D professional


Acties:
  • 0 Henk 'm!

  • SchizoDuckie
  • Registratie: April 2001
  • Laatst online: 18-02 23:12

SchizoDuckie

Kwaak

mr_wizard schreef op 16 augustus 2004 @ 11:14:
Ik heb een MIME script geschreven om MIME email met een attachment te kunnen versturen. De attachment moet iedere attachment kunnen zijn.

Als ik nu verstuur komt het allemaal prima aan, filesize in de attachment is OK, maar als ik hem open geeft het desbetreffende programma wat hem opend een foutmelding! Alleen HTM, HTML, TXT en....ZIP komen in goede vorm aan?!

Ik heb encoding op 'base64' en c_type op de file type van de file, die klopt wel. Maar waarom lukt het met ZIP bijvoorbeeld wel? op JPG en EXE crasht hij weer?!?
Sorry, mijn glazen bol is momenteel kapot, dus we kunnen je niet helpen als je geen code samples post :)

Maar iig, dit is een stukje code uit een PHP E-mail class die ik zelf gebouwd heb:

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function getAttachments()
    {
        $output = '';
        if (!empty($this->Attachments))
        {
             foreach($this->Attachments as $filename=>$filecontents)
            {
                    $output .= "\n--{$this->outerBoundary}\n";
                    $output .= "Content-Type: application/octetstream;\n\t";
                    $output .= "name=\"{$filename}\"\n";
                    $output .= "Content-Transfer-Encoding: base64\n";
                    $output .= "Content-Disposition: attachment;\n\t";
                    $output .= "filename=\"{$filename}\"\n\n";
                    $output .= chunk_split(base64_encode($filecontents));
                    $output .= "\n\n";
            }
        }
        return ($output);
    }


Hoop dat je daar wat aan hebt.

[ Voor 34% gewijzigd door SchizoDuckie op 16-08-2004 11:20 ]

Stop uploading passwords to Github!


Acties:
  • 0 Henk 'm!

  • Xenon
  • Registratie: Januari 2001
  • Laatst online: 21-08 09:12
Geef je de contenttype correct mee?

ProtocoLAN.be: De beste LAN van de Maaskant


Acties:
  • 0 Henk 'm!

  • mr_wizard
  • Registratie: Februari 2003
  • Laatst online: 29-04 21:17
De reden dat ik dat niet meestuurde was dat het samen 1500 regels is :|
hier is een deel:
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
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
        $target = "./".$HTTP_POST_FILES['attachment']['name'];
        $attach['name'] = $HTTP_POST_FILES['attachment']['name'];
        $attach['type'] = $HTTP_POST_FILES['attachment']['type'];
        copy($HTTP_POST_FILES['attachment']['tmp_name'],$target);
        unlink($HTTP_POST_FILES['attachment']['tmp_name']);
        
        $attachment = $mail->getFile($target);

        function getFile($filename)
        {
                $return = '';
                if ($fp = fopen($filename, 'rb')) {
                        while (!feof($fp)) {
                                $return .= fread($fp, 1024);
                        }
                        fclose($fp);
                        return $return;

                } else {
                        return false;
                }
        }
        function addAttachment($file, $name = '', $c_type, $encoding = 'base64')
        {
                $this->attachments[] = array(
                                                                        'body'                => $file,
                                                                        'name'                => $name,
                                                                        'c_type'        => $c_type,
                                                                        'encoding'        => $encoding
                                                                  );
        }

        $mail->addAttachment($attachment, $attach['name'], $attach['type']);

function send($recipients, $type = 'mail')
        {
                if (!defined('CRLF')) {
                        $this->setCrlf($type == 'mail' ? "\n" : "\r\n");
                }

                if (!$this->is_built) {
                        $this->buildMessage();
                }

                switch ($type) {
                        case 'mail':
                                $subject = '';
                                if (!empty($this->headers['Subject'])) {
                                        $subject = $this->_encodeHeader($this->headers['Subject'], $this->build_params['head_charset']);
                                        unset($this->headers['Subject']);
                                }

                                // Get flat representation of headers
                                foreach ($this->headers as $name => $value) {
                                        $headers[] = $name . ': ' . $this->_encodeHeader($value, $this->build_params['head_charset']);
                                }

                                $to = $this->_encodeHeader(implode(', ', $recipients), $this->build_params['head_charset']);

                                if (!empty($this->return_path)) {
                                        $result = mail($to, $subject, $this->output, implode(CRLF, $headers), '-f' . $this->return_path);
                                } else {
                                        $result = mail($to, $subject, $this->output, implode(CRLF, $headers));
                                }
                                
                                // Reset the subject in case mail is resent
                                if ($subject !== '') {
                                        $this->headers['Subject'] = $subject;
                                }
                                
                                // Return
                                return $result;
                                break;

                        case 'smtp':
                                require_once(dirname(__FILE__) . '/smtp.php');
                                require_once(dirname(__FILE__) . '/RFC822.php');
                                $smtp = &smtp::connect($this->smtp_params);
                                
                                // Parse recipients argument for internet addresses
                                foreach ($recipients as $recipient) {
                                        $addresses = Mail_RFC822::parseAddressList($recipient, $this->smtp_params['helo'], null, false);
                                        foreach ($addresses as $address) {
                                                $smtp_recipients[] = sprintf('%s@%s', $address->mailbox, $address->host);
                                        }
                                }
                                unset($addresses); // These are reused
                                unset($address);   // These are reused

                                // Get flat representation of headers, parsing
                                // Cc and Bcc as we go
                                foreach ($this->headers as $name => $value) {
                                        if ($name == 'Cc' OR $name == 'Bcc') {
                                                $addresses = Mail_RFC822::parseAddressList($value, $this->smtp_params['helo'], null, false);
                                                foreach ($addresses as $address) {
                                                        $smtp_recipients[] = sprintf('%s@%s', $address->mailbox, $address->host);
                                                }
                                        }
                                        if ($name == 'Bcc') {
                                                continue;
                                        }
                                        $headers[] = $name . ': ' . $this->_encodeHeader($value, $this->build_params['head_charset']);
                                }
                                // Add To header based on $recipients argument
                                $headers[] = 'To: ' . $this->_encodeHeader(implode(', ', $recipients), $this->build_params['head_charset']);
                                
                                // Add headers to send_params
                                $send_params['headers']    = $headers;
                                $send_params['recipients'] = array_values(array_unique($smtp_recipients));
                                $send_params['body']       = $this->output;

                                // Setup return path
                                if (isset($this->return_path)) {
                                        $send_params['from'] = $this->return_path;
                                } elseif (!empty($this->headers['From'])) {
                                        $from = Mail_RFC822::parseAddressList($this->headers['From']);
                                        $send_params['from'] = sprintf('%s@%s', $from[0]->mailbox, $from[0]->host);
                                } else {
                                        $send_params['from'] = 'postmaster@' . $this->smtp_params['helo'];
                                }

                                // Send it
                                if (!$smtp->send($send_params)) {
                                        $this->errors = $smtp->errors;
                                        return false;
                                }
                                return true;
                                break;
                }
        }

$result = $mail->send(array('lol@lol.nl'));

R&D professional


Acties:
  • 0 Henk 'm!

  • Xenon
  • Registratie: Januari 2001
  • Laatst online: 21-08 09:12
Hoe bepaal je de waarde van je $c_type ?

misschien tipje

PHP:
1
$c_type = exec("file -bi ".$file);

[ Voor 7% gewijzigd door Xenon op 16-08-2004 16:14 ]

ProtocoLAN.be: De beste LAN van de Maaskant