Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[php] Mail met attachment

Pagina: 1
Acties:

  • geenidee
  • Registratie: September 2001
  • Laatst online: 18-11 14:13
Ik heb een probleem met het versturen van een mail met attachment in php. De mail wordt wel verstuurd maar nooit ontvangen, kheb de search al gebruikt maar kben niks wijzer geworden.

<?

ini_set("max_execution_time","300");


if ($action == "send")

{
$from = "$Voorletters $Naam";
$to = "mail@mail.com";
$header = "From: " . $from . "\n";
$gegevens = "Advertentie: $Advertentie\n" . "Prijs: $Prijs\n" . "Zoekwoord: $Zoekwoord\n" . "Naam: $Naam, $Voorletters\n" . "Adres: $Adresnummer\n" . "Postcode: $Postcodecijfers $Postcodeletters\n" . "Woonplaats: $Woonplaats\n" . "Email: $email\n" . "Telefoonnummer: $Telefoonnummer\n" . "Fax: $FAX";

if ($attachment != "none")
{
// Get attachment type
@$type = $HTTP_POST_FILES[attachment][type];
if (($type == "image/jpg") || ($type == "image/jpeg") || ($type == "image/gif"))
$encoding = "base64";
else
$encoding = "8bit";

// Get attachment content
$fp = @fopen($HTTP_POST_FILES[attachment][tmp_name],"r");
if (!$fp) {
print "Could not open attachment for reading...<br>\n";
print "Exiting script!<br><br>";
print "No mail has been sent!";
exit;
}
@$file_content = fread($fp,filesize($HTTP_POST_FILES[attachment][tmp_name]));

// if encoding is base64 ... encode it
if ($encoding == "base64")
$file_content = chunk_split(base64_encode($file_content));

// create a unqiue boundary
$boundary = strtoupper(md5(uniqid(time())));

// create the message header...
$header .= "MIME-version: 1.0\n";
$header .= "Content-Type: multipart/mixed;\n";
$header .= "\tboundary= " . $boundary . "\n\n";
$header .= "This is a multi-part message in MIME format.\n\n";

// create the message body in the header
$header .= "--" . $boundary . "\n";
@$header .= "Content-Type: " . $ct . ";\n";
$header .= "\tcharset=\"iso-8859-1\"\n";
$header .= "Content-Transfer-Encoding: quoted-printable\n\n";

// actual message
$header .= $gegevens . "\n\n";

// now for the attachment
$header .= "--" . $boundary . "\n";
$header .= "Content-Type: " . $type . "\n";
$header .= "Content-Transfer-Encoding: " . $encoding . "\n";
@$header .= "Content-Disposition: attachment; filename=\"" . $HTTP_POST_FILES[attachment][name] . "\"\n\n";

// actual attachment
$header .= $file_content . "\n\n";
$header .= "--" . $boundary . "--";
}
else // if there is no attachment...
{
@$header .= "Content-Type: " . $ct . ";\n";
$header .= "\tcharset=\"iso-8859-1\"\n";
$header .= "Content-Transfer-Encoding: quoted-printable\n\n";

// actual message
$header .= $gegevens . "\n\n";
}
if (@mail($to,$subject,"",$header))
print "";
else
print "Fout met het versturen van e-mail";
}
?>

alvast bedankt

  • RdeTuinman
  • Registratie: Mei 2001
  • Laatst online: 07-11 16:42
misschien ligt het aan de \n achter de header enzo. In de php manual staat iets met \r\n. mail

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06 13:31

drm

f0pc0dert

code:
1
include ( PHP_quotingTags ); // [ php] en [ /php]


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
<?
ini_set("max_execution_time","300"); 

if ($action == "send")
{
  $from     = "$Voorletters $Naam";
  $to       = "mail@mail.com"; 
  $header   = "From: " . $from . "\n";
  $gegevens = 
      "Advertentie:    $Advertentie\n
       Prijs:          $Prijs\n
       Zoekwoord:      $Zoekwoord\n
       Naam:           $Naam, $Voorletters\n
       Adres:          $Adresnummer\n
       Postcode:       $Postcodecijfers Postcodeletters\n
       Woonplaats:     $Woonplaats\n
       Email:          $email\n
       Telefoonnummer: $Telefoonnummer\n
       Fax:            $FAX";

  if ($attachment != "none")
  {
    // Get attachment type
    @$type = $HTTP_POST_FILES[attachment][type];

    if (($type == "image/jpg") || ($type == "image/jpeg") || ($type == "image/gif"))
       $encoding = "base64";
    else
       $encoding = "8bit";

    // Get attachment content
    $fp = @fopen($HTTP_POST_FILES[attachment][tmp_name], "r" );
    if (!$fp) {
      print "Could not open attachment for reading...<br>\n";
      print "Exiting script!<br><br>";
      print "No mail has been sent!";
      exit;
    }
    @$file_content = fread($fp,filesize($HTTP_POST_FILES[attachment][tmp_name]));

    // if encoding is base64 ... encode it
    if ($encoding == "base64")
      $file_content = chunk_split(base64_encode($file_content));

    // create a unqiue boundary
    $boundary = strtoupper(md5(uniqid(time())));

    // create the message header...
    $header .= "MIME-version: 1.0\n
                Content-Type: multipart/mixed;\n
                \tboundary=$boundary\n\n
                This is a multi-part message in MIME format.\n\n";

    // create the message body in the header
    $header .= "--$boundary\n
               Content-Type: $ct;\n
               \tcharset=\"iso-8859-1\"\n
               Content-Transfer-Encoding: quoted-printable\n\n";

    // actual message
    $header .= "$gegevens\n\n";

    // now for the attachment
    $header .= "--$boundary\n
                Content-Type: $type\n
                Content-Transfer-Encoding: $encoding \n
                Content-Disposition: attachment; filename=\"{$HTTP_POST_FILES[attachment][name]}\"\n\n";

    // actual attachment
    $header .= "$file_content\n\n
                --$boundary--";
  }
  else // if there is no attachment...
  {
    @$header .= "Content-Type: $ct;\n
                 \tcharset=\"iso-8859-1\"\n
                 Content-Transfer-Encoding: quoted-printable\n\n";

    // actual message
    $header .= "$gegevens\n\n";
  }
  if ( @mail ($to,$subject,"",$header) )
     print "";
  else
     print "Fout met het versturen van e-mail";
}
?>

Daarnaast een aantal opmerkingen:

• Waarom zet je soms '@'-tekens voor je variabelen? (zie Error control operators
• Gebruik je '@'-control niet totdat het hele script volledig doorgetest is. (sowieso is het niet verstandig om te gebruiken imo, maar dat ff daar gelaten)
• Doe de naamgeving van je variabelen iets onderscheidender, bv: wat is $type en wat is $ct? Allebei Content-types, maar welke is welke ...?
• Verwijzing naar de functie move_uploaded_file
• Waarom neem je als message boundary niet gewoon alleen de md5 van de message?
• Probeer aan het eind van je script een dump te maken van de message die gegenereert wordt inclusief headers en attachment. Ga dan met de hand na of het een geldig bericht is. Maak er desnoods een .eml bestand van en open heb met Outlook of Mozilla mail of whatever, om te zien of die hem wel goed openen.
• check de errorlogs van de server, kijk of er ergens een bestand staat met de naam "dead.letter"

meer kan ik op het moment ff niet bedenken.

HTH :)

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • geenidee
  • Registratie: September 2001
  • Laatst online: 18-11 14:13
Uhm ja goeie vragen allemaal, maar zoals de naam al zegt ik heb geen idee hoe alles moet, kheb dit script van internet gehaald en ben een totale leek met php, dus kan je het misschien in lekentaal uitleggen, of heb je een hiervoor een beter script?

Ik krijg trouwens bij mn locale webserver wel mail binnen(maar verkeerde filesize 10kb versturen 6bytes binnen), maar niet op de webserver van de provider.

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06 13:31

drm

f0pc0dert

Op maandag 19 november 2001 09:43 schreef geenidee het volgende:
Uhm ja goeie vragen allemaal, maar zoals de naam al zegt ik heb geen idee hoe alles moet, kheb dit script van internet gehaald en ben een totale leek met php, dus kan je het misschien in lekentaal uitleggen, of heb je een hiervoor een beter script?
Je bent een van de weinigen waarbij de nickname ook direct betrekking heeft op het topic dattie opent :P
Ik krijg trouwens bij mn locale webserver wel mail binnen(maar verkeerde filesize 10kb versturen 6bytes binnen), maar niet op de webserver van de provider.
Zet eens aan het eind van je script:
PHP:
1
2
3
<?
echo "<b>Bericht:</b><pre>$header</pre>";
?>

En post de output even tussen [ code] en [/ code] tags.

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


Verwijderd

ik heb hetzelfde probleem! het lijkt alsof de mail met attachment goed verstuurd is maar komt niet aan als het adres buiten het domein is waar de site op staat.

heeft iemand een oplossing?
code:
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
To: bart@resolume.com
From: leo@isp.com
X-Priority: 3 (Normal)
Mime-Version: 1.0
Content-Type: multipart/mixed;
 boundary="--08bf7407522a4d1c268d20cf47470561"
Content-Transfer-Encoding: 7bit
X-Mailer: Php/libMailv1.3

This is a multi-part message in MIME format.
----08bf7407522a4d1c268d20cf47470561
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello world!
this is a test of the Mail class
please ignore
Thanks.
----08bf7407522a4d1c268d20cf47470561
Content-type: text/inc;
 name="mime.inc"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
  filename="mime.inc"

PD9waHANCiAvKiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0NCiAgICBNSU1FIENsYXNzOg0KICAgIEFsbG93cyBjcmVhdGlvbiBvZiBlLW1h
aWwgbWVzc2FnZXMgdmlhIHRoZSBNSU1FIFN0YW5kYXJkLg0KICAgIFRoZSBjbGFzcyBzdXBwb3J0
cyBtdWx0aXBsZSBhdHRhY2htZW50cyBhbmQgcHJlc2VudGluZyANCiAgICBhbiBlLW1haWwgaW4g
SFRNTC4NCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLS0gKi8NCiBpbmNsdWRlICgiTUlNRS5kZWYiKTsNCg0KIGNsYXNzIE1JTUVfbWFpbCB7
DQogLy9wdWJsaWM6DQogIHZhciAkdG87DQogIHZhciAkZnJvbTsNCiAgdmFyICRzdWJqZWN0Ow0K
ICB2YXIgJGJvZHk7DQogIHZhciAkaGVhZGVycyA9ICIiOw0KICB2YXIgJGVycnN0cj0iIjsNCg0K

  • geenidee
  • Registratie: September 2001
  • Laatst online: 18-11 14:13
Ja!! ik heb precies hetzelfde als barth.
Weet iemand hoe we dit moeten oplossen??

|:(

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06 13:31

drm

f0pc0dert


/me bekeek de source van een Outlook Express mailtje even:[quote]

[code]From: "drm" <gerardvh@studenten.net>
To: "sjaak"
Subject:
Date: Mon, 19 Nov 2001 16:12:17 +0100
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0013_01C17114.F879C8E0"
X-Priority: 3
X-MSMail-Priority: Normal
X-Unsent: 1
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

This is a multi-part message in MIME format.

------=_NextPart_001_0014_01C17114.F879C8E0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

asdf

------=_NextPart_000_0013_01C17114.F879C8E0
Content-Type: image/gif;
name="dot.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="dot.gif"

R0lGODlhAQABAIAAAAAOygAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==

------=_NextPart_000_0013_01C17114.F879C8E0--[/code]

[/quote]


• Na de regel "This is a multi-part message in MIME format." een extra witregel
• De boundary wordt in de header aangegeven (bovenaan mailtje)
• Voor elke boundary regel een witregel
• boundary-regel komt ook aan het einde van het mailtje voor.
• Na Content-type: multipart/mixed een ;

imo
• kun je alle X-**** headers achterwege laten


* drm stopt nu met in zichzelf praten.

Even wat uit mijn mouw schudden:
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
<?
function createMessage ( $text, $attachment, $attachmentName, $attachmentContentType, $to, $from, $subject )
{
   $boundary = "\n---" . md5 ( $text ) . "---";

   $ret =<<<MAILMESSAGE
From: $from
To: $to
Subject: $subject
Content-Type: multipart/mixed;
    boundary="$boundary"

This is a multi-part message in MIME format.
$boundary
Content-Type: text/plain;
    charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

$text
$boundary
Content-Type: $attachmentContentType;
    name="$attachmentName"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="$attachmentName"

$attachment
$boundary
MAILMESSAGE;

   return $ret;
}
?>

HTH

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz

Pagina: 1