<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
// Set default variables for all new objects
public $From = 'noreply#domain.com';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'isSMTP';
public $SMTPAuth = true;
public $Username = 'email#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'ssl';
public $WordWrap = 75;
public function subject($subject)
{
$this->Subject = $subject;
}
public function body($body)
{
$this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
I am Using this Code for Email.php file and it works but it sends Mail in Attachment Not in Normal Form...
Email Attachment Showing in Email
Email Attachment Output
Here is link where i am using it for verification purposes.
http://monthlyreport.ultimatefreehost.in
At index.php I am using Like this
//send email
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
$mail = new Mail();
$mail->setFrom(SITEEMAIL);
$mail->addAddress($to);
$mail->subject($subject);
$mail->body($body);
$mail->send();
//redirect to index page
header('Location: index.php?action=joined');
exit;
Your functions should return the values.
Use return $this->Body = $body; at the end of body(), and similarly do it for the other functions as well.
Related
I'm trying to send emails using the PHPMailer library and gmail in my MVC project. Here's what I did in my project. My project and files:
myproject
-app
-controllers
-AccountController.php
-core
-PHPMailer
-PHPMailer.php
-SMTP.php
-Exception.php
-MyCustomMailer.php
My AccountController.php has following code:
namespace App\Controllers;
use Core\PHPMailer\MyCustomMailer;
...
class AccountController extends Controller {
...
public function sendSecurityCode(){
$user_email = "example#gmail.com";
$name = "John Doe";
$code = "ranD0m"
$mailer = new MyCustomMailer(true);
$send = $mailer->sendSecurityCodeEmail($user_email, $name, $code);
if($send) die("Sent.");
else die("Not sent.");
}
}
And MyCustomMailer.php is somthing like this:
namespace Core\PHPMailer;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once('PHPMailer.php');
require_once('SMTP.php');
class MyCustomMailer extends PHPMailer
{
private $_host = 'smtp.gmail.com';
private $_user = ''userJohnDoe#gmail.com';';
private $_password = 'paSsW0rd';
public function __construct($exceptions=true)
{
$this->isSMTP();
$this->SMTPDebug = 1;
$this->Host = $this->_host;
$this->Port = 587;
$this->Username = $this->_user;
$this->Password = $this->_password;
$this->SMTPAuth = true;
$this->SMTPSecure = 'tls';
parent::__construct($exceptions);
}
// send security code email
public function sendSecurityCodeEmail( $email, $name, $code){
$mail_subject = "Security code account";
$html_body = "";
$mail_body = " Hey here is a test email and security code is: ". $code;
$email_sent = self::sendEmail($email, $name, $mail_subject, $html_body, $mail_body);
return $email_sent;
}
public static function sendEmail($to_email, $to_name, $subject, $html_body, $email_body){
$mail_sent = false;
try{
$mail = new PHPMailer;
$mail->setFrom("noreply#example.com", "Example");
$mail->addAddress($to_email,$to_name);
$mail->Subject = $subject;
if(!empty($html_body)) {
$mail->isHTML(true);
$mail->AltBody = $email_body;
$mail->Body = $html_body;
} else{
$mail->Body = $email_body;
}
if($mail->send()) $mail_sent = true;
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
return $mail_sent;
}
}
Getting errors Uncaught Error: Class 'PHPMailer\PHPMailer\Exception' not found. What is right way to implement it in my MVC project??
Incase someone having similar issues or looking for a MVC implementation: I made it work by adding the missing Exception.php in MyCustomMailer.php. Now it looks like this :
...
require_once(__DIR__.'/PHPMailer.php');
require_once(__DIR__.'/SMTP.php');
require_once(__DIR__.'/Exception.php');
...
It solved the PHPMailer\PHPMailer\Exception' not found problem. Then got another error Could not connect to SMTP host and solved it by updating the sendEmail function in MyCustomMailer.php. Here is the new sendEmail function:
public static function sendEmail($to_email, $to_name, $subject, $html_body, $email_body){
$mail_sent = false;
try{
$mail = new PHPMailer;
$mail->setFrom("noreply#example.com", "Example");
$mail->addAddress($to_email,$to_name);
$mail->Subject = $subject;
if(!empty($html_body)) {
$mail->isHTML(true);
$mail->AltBody = $email_body;
$mail->Body = $html_body;
} else{
$mail->Body = $email_body;
}
// added Gmail hack
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
if($mail->send()) $mail_sent = true;
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
return $mail_sent;
}
set the smtp username and password correctly and It's working just fine :) .
/*send email Using at Index.php page*/
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
//include 'classes/phpmailer/mail.php';
//function Send_Mail($to,$subject,$body)
$mail = new Mail();
//$mail->setFrom(SITEEMAIL);
//$mail->Debugoutput = 'html';
$mail->addAddress($to);
//$mail->setFrom('noreply#domain.com', 'noreply');
//$mail->addReplyTo('replyto#example.com', 'First Last');
$mail->subject($subject);
//$mail->IsHTML(true);
$mail->body($body);
$mail->send();
/*PHP Mailer Code used in mail.php*/
<?php
include('class.phpmailer.php');
class Mail extends PhpMailer
//function Send_Mail($subject,$body)
{
// Set default variables for all new objects
public $From = 'rssbmonthlyreport#gmail.com';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'isSMTP';
public $SMTPAuth = true;
public $Username = 'abc#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $Port = 587;
public $Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
public $CharSet = 'UTF-8';
public $Encoding = '8bit';
public $ContentType = 'text/html; charset=utf-8\r\n';
//public $From = 'rssbmonthlyreport#gmail.com';
//public $FromName = 'GMail Test';
public $WordWrap = 75;
public function subject($subject)
{
return $this->Subject = $subject;
}
public function body($body)
{
return $this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
I am Using these code above at Index page and this in email.php and email.php was included in config.But it is not working in place of this default mail() function running.....I tried every thing even the latest code of phpmailer its crashing..Anyone has idea so please help
This is wrong:
$Mailer = 'isSMTP';
It should be:
$Mailer = 'SMTP';
A better approach would be to call $this->isSMTP() from an overridden constructor.
I'd also recommend using the built-in html2text method if you're going to forcibly set AltBody like that.
If you're going to override things, read the code.
Here's an improved version:
class Mail extends PhpMailer
//function Send_Mail($subject,$body)
{
// Set default variables for all new objects
public $From = 'rssbmonthlyreport#gmail.com';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $SMTPAuth = true;
public $Username = 'abc#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $Port = 587;
public $Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
public $CharSet = 'UTF-8';
public $Encoding = '8bit';
//public $From = 'rssbmonthlyreport#gmail.com';
//public $FromName = 'GMail Test';
public $WordWrap = 75;
public function __construct($exceptions = null)
{
parent::__construct($exceptions);
$this->isSMTP();
$this->isHTML();
}
public function subject($subject)
{
return $this->Subject = $subject;
}
public function body($body)
{
return $this->Body = $body;
}
public function send()
{
$this->AltBody = $this->html2text($this->Body);
return parent::send();
}
}
}
which email you send
ex: gmail or hotmail
if ssl not present
hotmail automatically deletes email without even notifying. You have add exception for particular sender.
code:
class email extends main{
function email($to, $to_name, $subject, $message, $type, $schoolTitle, $schoolEmail, $attachments){
$this->to = $to;
$this->to_name = $to_name;
$this->subject = $subject;
$this->message = $message;
$this->type = $type;
$this->schoolTitle = $schoolTitle;
$this->schoolEmail = $schoolEmail;
$this->attachments = $attachments;
}
function connect(){
require_once PLUGINS.'PHPMailer-20160322/PHPMailerAutoload.php';
$this->mail = new PHPMailer;
$this->mail->isSMTP();
$this->mail->SMTPDebug = 0;
$this->mail->Debugoutput = 'html';
$this->mail->Host = "**********";
$this->mail->Port = **;
$this->mail->SMTPAuth = true;
$this->mail->SMTPSecure = 'tls';
//$mail->Sender="************";
$this->mail->Username = "**************";
$this->mail->Password = "**************";
}
function sendSingle(){
//this is used in the bulk emailer when the connect it called so we can send multiple emails from 1 connection
$this->mail->addAddress($this->to, $this->to_name);
$this->mail->Subject = $this->subject;
$this->mail->msgHTML($this->getMessage());
$this->mail->AltBody = strip_tags($this->message);
$this->mail->setFrom($this->schoolEmail, $this->schoolTitle);
$this->mail->AddReplyTo($this->schoolEmail, $this->schoolTitle);
foreach($this->attachments as $item){
$this->mail->addAttachment($item);
}
if (!$this->mail->send()) {
return false;
} else {
return true;
}
}
}
Result I want:
I want to be able to use the testEmail and testTitle
$this->schoolEmail= "chris.beckett#testing.co.uk";
$this->schoolTitle= "Chris Beckett";
$this->mail->setFrom($this->schoolEmail, $this->schoolTitle);
$this->mail->AddReplyTo($this->schoolEmail, $this->schoolTitle);
What I've tried and worked:
I don't understand why the testTitle works but when I try and pass in the testEmail the email doesn't send or it will send but it won't have the parameters of from: or reply to:
$this->schoolEmail= "chris.beckett#testing.co.uk";
$this->schoolTitle= "Chris Beckett";
$this->mail->setFrom('name#yourdomain.com', $this->schoolTitle);
$this->mail->AddReplyTo('name#yourdomain.com', $this->schoolTitle);
Debug Info showed that it worked but the email not showing up in my inbox:
CLIENT -> SERVER: From: Chris Beckett <chris.beckett#testing.co.uk>
CLIENT -> SERVER: Reply-To: Chris Beckett <chris.beckett#testing.co.uk>
Could anyone explain what I could be doing wrong?
I tried sending email using smtp gmail using phpmailer. My gmail account got suspended saying that there is an unusual activity. Here is the code I have used for sending emails. What's the correct way of sending emails using smtp gmail in phpmailer?
My question is not duplicate. I have already tried this : Send email using the GMail SMTP server from a PHP page
I'm using phpmailer, and my accounts getting suspended.
<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
// Set default variables for all new objects
public $From = 'noreply#exmaple.org';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'smtp';
public $SMTPAuth = true;
public $Username = 'username#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $WordWrap = 75;
public function subject($subject)
{
$this->Subject = $subject;
}
public function body($body)
{
$this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
Here is my php code :
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
$mail = new Mail();
$mail->setFrom('noreply#example.org');
$mail->addAddress($to);
$mail->subject($subject);
$mail->body($body);
$mail->send();
//redirect to index page
header('Location: index.php?action=joined');
exit;
I'm using PHPmailer to send emails and I've created a function that prepares the email and sends it. If I try to use this function more than once in a script it stops the script execution when it trys to send a second email using the same function.
my function:
public static function sendEmail($from, $fromName, $to, $subject, $body){
require("includes/class.phpmailer.php");
$mailer = new PHPMailer();
$mailer->IsSMTP(true);
$mailer->Host = 'ssl://smtp.gmail.com:465';
$mailer->SMTPAuth = true;
$mailer->Username = 'removed';
$mailer->Password = 'removed';
$mailer->From = $from;
$mailer->FromName = $fromName;
$mailer->AddAddress($to);
$mailer->Subject = $subject;
$mailer->Body = $body;
$mailer->WordWrap = 100;
if ($mailer->Send()) {
return true;
} else {
return false;
}
}
Why is this happening? Is it anything to do with creating a new phpmailer object each time?
I think you should use require_once() instead of require