Note: Remember to download PHPMailer from https://github.com/Synchro/PHPMailer and upload extracted files (smtp.php, phpmailer.php & exception.php) to your script's folder at the same level of your PHP script.
For security reason, our mail servers require your script to pass SMTP authentication in order to send email.
To send email via PHP script, please refer to following sample code.
Sample Code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer.php';
require 'smtp.php';
require 'exception.php';
$mail = new PHPMailer();
$mail->IsSMTP(); //set mailer to use SMTP
$mail->Host = "mail.yourdomain.com"; //specify SMTP mail server
$mail->Port = "2525"; //specify SMTP Port
$mail->SMTPAuth = true; //turn on SMTP authentication
$mail->Username = "email@yourdomain.com"; //Full SMTP username
$mail->Password = "your_password"; //SMTP password
$mail->From = "email@yourdomain.com";
$mail->FromName = "Your Name";
$mail->AddAddress("email2@yourdomain.com", "Recipient 1");
$mail->AddAddress("email3@yourdomain.com"); //name is optional
$mail->AddReplyTo("email@yourdomain.com", "Information");
$mail->WordWrap = 50; //optional, you can delete this line
$mail->AddAttachment("/var/tmp/file.tar.gz"); //optional, you can delete this line
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); //optional, you can delete this line
$mail->IsHTML(true); //set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body in bold!";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent.
";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
- 149 Utenti hanno trovato utile questa risposta