I haven't sent email using MVC before and am getting a bit stuck.
In my app folder, I have a libraries folder which has Controller.php, Core.php, Database.php and I created Email.php
In Email.php I have a class:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/autoload.php';
class Email {
public function sendMail()
{
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mail#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('mail#example.com');
$mail->addAddress('someone#example.com'); // Add a recipient // Name is optional
$mail->addReplyTo('mail#example.com');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
}
I am now trying to trigger the sending of the email when I access the email view. However, I don't know what to put in the controller. The code below gives me an error.
public function email()
{
$this->sendMail();
$this->view('pages/email');
}
Fatal error: Uncaught Error: Call to undefined method Pages::sendMail()
You have to create an instance of the class Email:
$email = new Email();
$email->sendMail();
Related
I'm trying to include PHPmailer in functions.php
my code:
add_action('wp_ajax_nopriv_test_mailer', 'test_mailer');
function test_mailer () {
try {
require_once(get_template_directory('/includes/mail/PHPMailer.php'));
require_once(get_template_directory('/includes/mail/Exception.php'));
$mail = new PHPMailer(true); // Passing `true` enables exceptions
//Server settings
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'test#gmail.com'; // SMTP username
$mail->Password = 'dummypassword!'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('test#gmail.com', 'Mailer Test');
$mail->addAddress('john.doe#gmail.com', 'John User'); // Add a recipient
$mail->addReplyTo('test#gmail.com');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject testing';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
wp_die();
}
I also tried to put require_once out of the try catch still the same error here is the snippet about the error
"PHP Fatal error: Uncaught Error: Class 'PHPMailer' not found"
I use betheme template and I stored the files PHPmailer in betheme/includes/mail.
As BA_Webimax pointed out, you should be using Wordpress' built-in email functions, though due to WP's reliance on outdated PHP versions, you will end up using a very old version of PHPMailer with it.
Back to your current problem: It's not your require_once statements that are failing, it's that you have not imported the namespaced PHPMailer classes into your namespace. Add these at the top of your script:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
Alternatively, use the FQCN when creating your instance:
$mail = new PHPMailer\PHPMailer\PHPMailer;
Note that this applies to the Exception class too, so you'd need to say:
catch (PHPMailer\PHPMailer\Exception $e) {
get_template_directory() returns the absolute path of the theme and doesn't take any params.
Try this for your includes:
require_once(get_template_directory().'/includes/mail/PHPMailer.php');
require_once(get_template_directory().'/includes/mail/Exception.php');
I want to send otp mail to user by fetching email value from database . I am using php auto mailer . It works when i use mail address directly but when i retrieve it from database i cant send it . Can anyone please help me with sending it .
function mailit()
{
$rand=rand(100000,999999);
$user=$_SESSION['user'];
$sql=mysqli_query($db,"UPDATE `doctor_login` SET `otp`='$rand' WHERE `doctor_id`='1'");
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername '; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587 ; // TCP port to connect to
$mail->setFrom('dont_reply_back#example.xyz');
$mail->addAddress('$row'); // Add a recipient
$mail->addReplyTo('dont_reply_back#example.xyz');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'THis is your OTP';
$mail->Body = 'Your otp is '.$rand;
$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;
}
}
Code for retrieving and displaying of email
$mailadd=mysqli_query($db,"SELECT `doctor_email` FROM `doctor_login` WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);
echo $row['doctor_email'];
$row2=$rowa['doctor_email'];
echo $row2;
i have used both $row and $row2 in $mail->addAddress but doesnt seem to work . What changes can i do . it works whwn i give a specific address
I think this is a scope issue. If you are calling this code in the main body of the code
$mailadd=mysqli_query($db,"SELECT `doctor_email`
FROM `doctor_login`
WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);
Then I assume you call mailit() after this, so pass the data you want to use as a parameter to the mailit() function so it is available inside the function scope.
function mailit($row)
{
$rand=rand(100000,999999);
$user=$_SESSION['user'];
$sql=mysqli_query($db,"UPDATE `doctor_login` SET `otp`='$rand' WHERE `doctor_id`='1'");
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername '; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587 ; // TCP port to connect to
$mail->setFrom('dont_reply_back#example.xyz');
$mail->addAddress($row['doctor_email']); // Add a recipient
$mail->addReplyTo('dont_reply_back#example.xyz');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'THis is your OTP';
$mail->Body = 'Your otp is '.$rand;
$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;
}
}
$mailadd=mysqli_query($db,"SELECT `doctor_email`
FROM `doctor_login`
WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);
mailit($row);
You should have been getting errors from your original code so in future add the following:
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
// if you are using the `mysqli` API
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser as well as normal PHP errors.
I just downloaded phpmailer from github and uploaded to shared hosting.
I made file called mailer.php and added this code (example from git-page):
When I start this (open file) Nothing is echoed and no mails are received. If i try to echo anything like any text after this code it won't do it but if I put it before it will echo it out.
I looked up Here but nothing from answers works here...
<?php
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.mydomain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; //shared hosting ssl port
//Recipients
$mail->setFrom('email#mydomain.com', 'Site Title');
$mail->addAddress('myGmail#gmail.com', 'Soma name'); // Name is optional
$mail->addReplyTo('email#mydomain.com', 'Info');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
I got the above error when executing my code of PHP Mailer. I cannot not figure out the reason. PHP version is 5.2. in my free web hosting server 000webhost.com.
error screenshot is shown there;
error
require 'PHPMailer-master/PHPMailerAutoload.php';
date_default_timezone_set('Asia/Colombo'); //this function sets the default timezone used by all data/time functions in the script
//Solution for the error - It is not safe to rely on the system's timezone settings.
ini_set ( 'max_execution_time', 1200); //Solution for the error - set_time_limit() has been disabled for security reasons
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'thisistestemail16#gmail.com'; // SMTP username
$mail->Password = 'testemail123'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('thisistestemail16#gmail.com', 'Raveen Chandra');
$mail->addAddress('raveen749#gmail.com', 'Chairman'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
$mail->addAttachment('uploads/3.JPG'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$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;
} else {
echo 'Message has been sent';
}
/*
public function smtpClose()
{
//if (is_a($this->smtp, 'SMTP')) {
$o = 'SMTP';
if($this->smtp instanceof $o) {
if ($this->smtp->connected()) {
$this->smtp->quit();
$this->smtp->close();
}
}
}
*/
I am working on phpmailer but I am receiving this error, I dont know what is wrong. I search and did many changes. but noting helped me.
I am testing both on live server and on localhost.
on both I am receiving the error
Message could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Please have a look on my code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
require("PHPMailer/class.phpmailer.php"); // path to the PHPMailer class
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'stockholm85#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'agohar1985#gmail.com';
$mail->FromName = 'Mailer';
//$mail->addAddress('zia_gt#yahoo.com', 'Zia Ullah'); // Add a recipient
$mail->addAddress('zia_gt#yahoo.com'); // Name is optional
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$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;
} else {
echo 'Message has been sent';
}
?>
I have folder on name PHPMailer and mail.php is the file.
I will appreciate your support because I am new to php
Thank you in advance
open your cmd and type the following to get php-mailer,
wget https://github.com/PHPMailer/PHPMailer/archive/master.zip
unzip the master. mention the file path clearly in your code.
Here
<?php
require 'PHPMailer-master/PHPMailerAutoload.php'; //file path
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#gmail.com';
$mail->Password = '_password_';
$mail->SMTPSecure = 'tls';
$mail->From = 'sender#example.com';
$mail->FromName = 'Your Name';
$mail->addAddress('recipient#example.com');
$mail->isHTML(true);
$mail->Subject = 'Test Mail Subject!';
$mail->Body = 'This is SMTP Email Test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
test it