In this tutorial we have a simple script which send email through SMTP (Send Mail Transfer protocol) with PHP Mailer. We need <a href="http://phpmailer.worxware.com/">phpmailer</a> library and bellow given code.
<?php
require_once('PHPMailer_v5.1/class.phpmailer.php'); //library added in download source.
$msg = 'Hello World';
$subj = 'test mail message';
$to = 'info@site.com';
$from = 'you@youremail.com';
$name = 'My Name';
echo smtpmailer($to,$from, $name ,$subj, $msg);
function smtpmailer($to, $from, $from_name = 'Example.com', $subject, $body, $is_gmail = true) {
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
if($is_gmail) {
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'admin@example.com';
$mail->Password = '*******';
}
else {
$mail->Host = 'smtp.mail.google.com';
$mail->Username = 'admin@example.com';
$mail->Password = '******';
}
$mail->IsHTML(true);
$mail->From="admin@example.com";
$mail->FromName="Example.com";
$mail->Sender=$from; // indicates ReturnPath header
$mail->AddReplyTo($from, $from_name); // indicates ReplyTo headers
$mail->AddCC('cc@site.com.com', 'CC: to site.com');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return true;
}
else {
$error = 'Message sent!';
return false;
}
}
?>
<?php
require_once('PHPMailer_v5.1/class.phpmailer.php'); //library added in download source.
$msg = 'Hello World';
$subj = 'test mail message';
$to = 'info@site.com';
$from = 'you@youremail.com';
$name = 'My Name';
echo smtpmailer($to,$from, $name ,$subj, $msg);
function smtpmailer($to, $from, $from_name = 'Example.com', $subject, $body, $is_gmail = true) {
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
if($is_gmail) {
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'admin@example.com';
$mail->Password = '*******';
}
else {
$mail->Host = 'smtp.mail.google.com';
$mail->Username = 'admin@example.com';
$mail->Password = '******';
}
$mail->IsHTML(true);
$mail->From="admin@example.com";
$mail->FromName="Example.com";
$mail->Sender=$from; // indicates ReturnPath header
$mail->AddReplyTo($from, $from_name); // indicates ReplyTo headers
$mail->AddCC('cc@site.com.com', 'CC: to site.com');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return true;
}
else {
$error = 'Message sent!';
return false;
}
}
?>
No comments:
Post a Comment