Inviare emails tramite SMTP con PHPmailer e Gmail

Posted on 19. Sep, 2009 by daniele in email, guide per il web, php, script php


 
 
define('GUSER', 'you@gmail.com'); // Gmail username
define('GPWD', 'password'); // Gmail password
 
function smtpmailer($to, $from, $from_name, $subject, $body) {
	global $error;
	$mail = new PHPMailer();  // create a new object
	$mail->IsSMTP(); // enable SMTP
	$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
	$mail->SMTPAuth = true;  // authentication enabled
	$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
	$mail->Host = 'smtp.gmail.com';
	$mail->Port = 465;
	$mail->Username = GUSER;
	$mail->Password = GPWD;
	$mail->SetFrom($from, $from_name);
	$mail->Subject = $subject;
	$mail->Body = $body;
	$mail->AddAddress($to);
	if(!$mail->Send()) {
		$error = 'Mail error: '.$mail->ErrorInfo;
		return false;
	} else {
		$error = 'Message sent!';
		return true;
	}
}
 
//esempio applicato
 
if (smtpmailer('to@mail.com', 'from@mail.com', 'yourName', 'test mail message', 'Hello World!')) {
	// do something
}
if (!empty($error)) echo $error;
 


Correlati

2 Comments

Mario

08. Jan, 2010

Sono su un server IIS con php 5
Ho fatto dei test e non capisco perchč se io inserisco dei dati errati la mail arriva comunque… Questo mi fa pensare che la mail venga inviata senza l’ausilio di smtp.
Qualcuno puň aiutarmi?

gax80

20. Apr, 2010

hi
i want to say where this code must be included…in html file o i must created another file .php?

thanks
Gax

Leave a reply