PHP: usare la funzione mail() in locale
This entry was posted on October 30th, 2008 and is filed under php.

Questo è il secondo articolo della settimana che pubblico prendendo spunto dal sito di S.V.Design. L’altra volta vi parlai di una funzione php per eseguire delle operazioni ad una determinata ora, stavolta invece vi parlo di un piccolo programmino da compilare in C++ che ci permette di testare la funzione mail() direttamente in locale.
Innanzitutto dobbiamo compilare il seguente listato di codice con un compilatore C++:
#include
#include
bool isFileExist(char* nomefile) {
FILE *stream;
if ((stream=fopen(nomefile,”r”))!=NULL) {
return true;
} else {
return false;
}
}
void num2str(int num, char* str) {
if (num==0) strcpy(str,”0?); else strcpy(str,”");
while (num) {
switch (num % 10) {
case 0:
strcat(str,”0?);
break;
case 1:
strcat(str,”1?);
break;
case 2:
strcat(str,”2?);
break;
case 3:
strcat(str,”3?);
break;
case 4:
strcat(str,”4?);
break;
case 5:
strcat(str,”5?);
break;
case 6:
strcat(str,”6?);
break;
case 7:
strcat(str,”7?);
break;
case 8:
strcat(str,”8?);
break;
case 9:
strcat(str,”9?);
break;
}
num=(int) (num/10);
}
_strrev(str);
}
int main(int argc, char* argv[]) {
/* sendmail path file
Crea dei file al posto delle email.
Path è il percorso assoluto della cartella dove mettere i file. Deve esistere. Se non specificato path=”C:\”.
File è il nome del file in cui inviare l’output. I file vengono automaticamente numerati.
Se non specificato file=”email”.
p.e.
sendmail c:\mails\ mail
sendmail c:\mails\
*/
// Inizializzo le variabili
FILE *stream;
char buffer[81];
char nomefile[41];
char stringa[10];
int i=0,ch=’a',j=0;
bool leggi=true;
// Inizializzo gli argomenti
switch (argc) {
case 1:
argv[1]=”c:\\”;
case 2:
argv[2]=”email”;
}
//Preparo il nomefile di output
do {
strcpy(nomefile,”");
strcat(nomefile,argv[1]);
strcat(nomefile,argv[2]);
num2str(j,stringa);
strcat(nomefile,stringa);
strcat(nomefile,”.eml”);
j++;
} while (isFileExist(nomefile));
//Apro il file di output
if ((stream=fopen(nomefile,”w”))!=NULL) {
while (!(((buffer[0]==’.')&&(buffer[1]==’\n’))||(ch == <acronym title="End of file">EOF</acronym>))) {
// Prende massimo 80 byte alla volta
for(i=0;(i<79)&&((ch = getchar()) != <acronym title="End of file">EOF</acronym>)&&(ch != ‘\n’);i++) {
buffer[i] = (char) ch;
}
buffer[i++] = ‘\n’; // < = termine di riga
// Mette la riga nel file.
fwrite(buffer,sizeof(char),i,stream);
}
fclose(stream);
} else {
return 1;
}
return 0;
}
Poi apriamo il file php.ini modificando questa parte:
[mail function]
; For Win32 only.
SMTP = localhost
; For Win32 only.
sendmail_from = me@localhost.com
; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).
;sendmail_path =
con:
[mail function]
sendmail_path = path_sorgente_compilato\sendmail.exe path_sorgente_compilato
dove path_sorgente_compilato è il path in cui abbiamo inserito il file sendmail.exe precedetemente compilato.
Via | S.V. Design









