Envoi Email SMTP

Écrit par Agence Web 360 le 11th octobre 2009

/**
* evoi_email_smtp() – Envoie un mail via le protocole SMTP
*
*
* @param string $adr_exp
* @param string $nom_exp
* @param string $adr_dest
* @param string $domaine
* @param string $type
* @param string $sujet
* @param string $contenu
* @param string $smtpServeur
* @param string $username
* @param string $password
* @param string $localhost
*
* @return string
* @return bool
*
*/

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
function envoi_mail_smtp($adr_exp, $nom_exp, $adr_dest, $domaine, $type, $sujet, $contenu, $smtpServer, $username, $password, $localhost)
{
	/* * * * CONFIGURATION  * * * */
	$port = "25";
	$timeout = "30";
	$newLine = "\r\n";
	/* * * * CONFIGURATION END * * * * */
 
	//Connect to the host on the specified port
	$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
	$smtpResponse = fgets($smtpConnect, 515);
	if(empty($smtpConnect))
	{
		$output = "Failed to connect: $smtpResponse";
		return $output;
	}
	else 
	{
		$logArray['connection'] = "Connected: $smtpResponse";
	}
 
	//Request Auth Login
	fputs($smtpConnect,"AUTH LOGIN" . $newLine);
	$smtpResponse = fgets($smtpConnect, 515);
	$logArray['authrequest'] = "$smtpResponse";
 
	//Send username
	fputs($smtpConnect, base64_encode($username) . $newLine);
	$smtpResponse = fgets($smtpConnect, 515);
	$logArray['authusername'] = "$smtpResponse";
 
	//Send password
	fputs($smtpConnect, base64_encode($password) . $newLine);
	$smtpResponse = fgets($smtpConnect, 515);
	$logArray['authpassword'] = "$smtpResponse";
 
	//Say Hello to SMTP
	fputs($smtpConnect, "HELO $localhost" . $newLine);
	$smtpResponse = fgets($smtpConnect, 515);
	$logArray['heloresponse'] = "$smtpResponse";
 
	//Email From
	fputs($smtpConnect, "MAIL FROM: $adr_exp" . $newLine);
	$smtpResponse = fgets($smtpConnect, 515);
	$logArray['mailfromresponse'] = "$smtpResponse";
 
	//Email To
	fputs($smtpConnect, "RCPT TO: $adr_dest" . $newLine);
	$smtpResponse = fgets($smtpConnect, 515);
	$logArray['mailtoresponse'] = "$smtpResponse";
 
	//The Email
	fputs($smtpConnect, "DATA" . $newLine);
	$smtpResponse = fgets($smtpConnect, 515);
	$logArray['data1response'] = "$smtpResponse";
 
	//Construct Headers
	$headers = 'MIME-Version: 1.0' ."\r\n";
	$headers .= 'To: '.$adr_dest.' <'.$adr_dest.'>' ."\r\n";
	$headers .= 'From: "'.$nom_exp.'"<'.$adr_exp.'>'."\r\n";
	$headers .= 'Reply-To: '.$adr_rep."\r\n";
	$headers .= 'Content-Type: text/'.$type.'; charset="iso-8859-15"'."\r\n";
	$headers .= 'Content-Transfer-Encoding: 8bit'."\r\n";
	$headers .= 'X-Sender: <'.$domaine.">"."\r\n";
	$headers .= 'X-auth-smtp-user: '.$adr_rep."\r\n";
	$headers .= 'X-abuse-contact: '.$adr_rep."\r\n";
 
	fputs($smtpConnect, "Subject: $sujet\n$headers\n\n$contenu\n.\n");
	$smtpResponse = fgets($smtpConnect, 515);
	$logArray['data2response'] = "$smtpResponse";
 
	// Say Bye to SMTP
	fputs($smtpConnect,"QUIT" . $newLine);
	$smtpResponse = fgets($smtpConnect, 515);
	$logArray['quitresponse'] = "$smtpResponse";
 
	return true;
}
Cet article a été publié le 11th octobre 2009 à 20 h 54 min et classé sous Emails, Fonctions, PhP. Vous pouvez suivre toutes les reponses a cet article via le flux RSS 2.0 . Vous pouvez répondre, ou faire un lien depuis votre site.

Répondre

Vous devez être connecté pour rédiger un commentaire.