How to send an email with PHPmailer - phpmailer

In order to use PHPmailer I installed vendor on my index file. Vendor contains the autoloader. I have seen that we can use his own SMTP or in localhost. in my case I want to do it in localhost.
I tried to change the lines after [mail function] in "php.ini" with my informations but it didn't help. here is the code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->setFrom('myEmail#gmail.com', 'Darth Vader');
$mail->addAddress('myEmail#gmail.com', 'Emperor');
$mail->Subject = 'Force';
$mail->Body = 'There is a great disturbance in the Force.';
$mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
Here is what I get on firefox on localhost: "Could not instantiate mail function.".

Related

PHPMailer not working when function is within and included file

Something is mighty odd. I created a basic test file with the code from PHPMailer.
# Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'includes/PHPMailer/PHPMailer.php';
require 'includes/PHPMailer/SMTP.php';
require 'includes/PHPMailer/Exception.php';
function sendCampaignEmail ($email, $firstname, $lastname)
{
$mail = new PHPMailer(true); // Create a new PHPMailer instance. Passing `true` enables exceptions.
try {
//Server settings
# $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output. SMTP::DEBUG_SERVER = client and server messages
# $mail->SMTPDebug = SMTP::DEBUG_CLIENT; // SMTP::DEBUG_CLIENT = client messages
$mail->SMTPDebug = SMTP::DEBUG_OFF; // SMTP::DEBUG_OFF = off (for production use)
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'myhostxxxx.co.uk'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'oobaclub#xxxxxxxxx.co.uk'; // SMTP username
$mail->Password = 'xxxxxmyEmailPasswordxxxxx'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 465; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('myfromemail.co.uk', 'My Name');
$mail->addAddress($email, $firstname . ' ' . $lastname); // Add a recipient
$mail->addReplyTo('myfromemail.co.uk', 'My Name');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'Dear ' . $firstname . ' ' . $lastname . '<br/><br/>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 '<br/>Message has been sent to ' . $email . ' (' . $firstname . ' ' . $lastname . ')';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
$names= array();
$id=0;
$names[$id]['email'] = "atestemail#gmail.com";
$names[$id]['first_name'] = "Bob";
$names[$id]['last_name'] = "gMail"; $id++;
$names[$id]['email'] = "anothertest#testingmyemail.co.uk";
$names[$id]['first_name'] = "Sid";
$names[$id]['last_name'] = "Smith"; $id++;
$count=0;
while ($count < count($names))
{
sendCampaignEmail ($names[$count]['email'], $names[$count]['first_name'], $names[$count]['last_name']);
$count++;
}
THIS CODE ABOVE WORKS FINE.
So... Then, I took the function and put it into an Included file (where all my functions are: "globalfunctions.php").... And now it says "Fatal error: Uncaught Error: Class 'PHPMailer' not found"
So, now, at the top of my index.php, I have:
## PHP MAILER - # Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'includes/PHPMailer/PHPMailer.php';
require_once 'includes/PHPMailer/SMTP.php';
require_once 'includes/PHPMailer/Exception.php';
require_once 'includes/connect.php';
require_once 'includes/globalfunctions.php';
I am confused as everything else works. All my other functions work. I thought namespaces were global... But, I tried adding the "use" code into the function... but, as expected, that didn't work either....
I am stumped.
PHP use declarations are local aliases that apply only to the file they appear in. You have to add a use statement in every file that uses that class; you can't put all your declares in one file and then include it from somewhere else.
Now might be a good time to learn how to use composer as it takes care of a lot of this.

problems with server SMTP PHPMailer

I have a problem with phpmailer, I have the correct SMPT server data and also the username and password, but it still marks me an error in the credentials, this is my code
<?php
include ("class.phpmailer.php");
include ("../vendor/phpmailer/phpmailer/PHPMailer.php");
include ("../vendor/phpmailer/phpmailer/src/SMTP");
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/phpmailer/phpmailer/src/Exception.php';
require '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '../vendor/phpmailer/phpmailer/src/SMTP.php';
require '../vendor/autoload.php';
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Load Composer's autoloader
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // 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 = 'admin#ucarolina.mx'; // SMTP username
$mail->Password = '******'; // SMTP password
$mail->SMTPSecure = 'TSL'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('luribe#qvoz.com');
$mail->addAddress($email, $name); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $contact_message;
$mail->AltBody = $contact_message;
$mail->send();
echo "Mensaje enviado";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
and this is the error that marks me
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
There is a line where SMTPSecure has to be specified. You have that line, but tls has been mistyped as TSL. Let's change that:
Change
$mail->SMTPSecure = 'TSL';
to
$mail->SMTPSecure = 'tls';
Additionally, you should add an if/then surrounding $mail->send(); like so:
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
If the mail is not sent, your if statement will catch that and report the error. See an example of such error handling in PHPMailer example: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

Sending Email by PHPmailer without SMTP

I know that we could use PHPMailer to send an email without the need of SMTP. But I still don't understand how it is configurated.
1/ Can we set anything to "Email From"? If so then anybody could fake an email address and send it to anybody? If not what is the condition?
2/ What is the basic configuration that a Web server must have in order for it to work.
Sorry for my silly questions.
<?php
$mail = new PHPMailer(true);
//Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom("britney#britneyspears.com", "Britney Spears");
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";
try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
//Something went bad
echo "Fail - " . $mail->ErrorInfo;
}
?>
I am not familiar.
see. https://github.com/PHPMailer/PHPMailer
1/ Can we set anything to "Email From"?
You can use setFrom . (https://github.com/PHPMailer/PHPMailer#a-simple-example)
$mail->setFrom('from#example.com', 'Mailer');
If so then anybody could fake an email address and send it to anybody?
uhhh... I think SMTP server has error.
PHPMailer use $from. maybe $from was setted by setFrom . (https://github.com/PHPMailer/PHPMailer/blob/master/src/SMTP.php#L817)
If not what is the condition?
If address doesn't set, I think error will occur. (https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php#L1185)

PHP mailer throws error in php 7 when tried send mail

I'm trying to send mail using PHP Mailer but I'm getting an error.
Kindly check my code let me know the fix.
Here is my code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer;
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxxxxxxxx ';
$mail->Password = 'xxxxxxxxx';
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('xxxxxxx', 'shiva');
$mail->addAddress('siva.sing.sivan#gmail.com', 'SP'); // Add a recipient
$mail->addAddress('senthil.mca2008#gmail.com', 'SK'); // Add a recipient
//\\$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('opensourcesivaprakash#gmail.com', 'Information');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Test mail form SP';
$mail->Body = 'This is the HTML message <b>From SP!</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;
exit;
}else{
echo 'Message has been sent';
}
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
My Error:
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Please correct my code to fix the issue.
Hey #shiva use this $mail->SMTPSecure = 'tls' and use port as $mail->Port = 587 that's it!
Tell me it worked for you, if not then make sure to enable "Less Secure Apps" in your account settings, Thanks!
Let me know it worked for you or not?
upgrade the php version to 7.2.x and try it has inbuilt openssl 1.1.1

Wamp Server and PHP mailer

I have a WAMP server database which contains users with their emails and activation codes.
How do I send an email for every new record in the WAMP server database automatically with its activation code? Using PHP.
P.s: I can already send an email from WAMP using PHP mailer but I have to specify the receiver each time.
This is the mail.php script that I use for sending emails using PHP mailer
<?php
require "init.php";
require"PHPMailerAutoload.php";
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com";
$mail->Port = "587";
$mail->IsHTML(true);
$mail->Username = "******#gmail.com";
$mail->Password = "******";
$mail->From = "*******#gmail.com";
$mail->FromName = "Vodafone-El7a2ny";
$mail->AddAddress("*****", "Marwa Hashem");
$mail->Subject = "Activation Code";
$mail->Body = " Dear User,
Thank you for registering in El72any Mobile Application,
Your Activation Code is
Best Regards,
****";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Email was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
exit;
} else {
echo 'Email has been sent.';
}
?>
This is the register.php script
<?php
require "init.php";
$EmailAddress=$_POST["EmailAddress"];
$MobileNumber=$_POST["MobileNumber"];
$Password=$_POST["Password"];
$sql_query = "insert into user_information VALUES('$EmailAddress', '$MobileNumber', '$Password','');";
if(mysqli_query($con,$sql_query))
{
echo "<h3>Database Insertion Success...</h3>";
}
else
{
echo "Data Inerstion Error...".mysqli_error($con);
}
?>
Also this is the init.php script I used to connect to my database
<?php
$db_name="CustomerDB";
$mysql_user="root";
$mysql_pass=null;
$server_name="localhost";
$con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
if(!$con)
{
echo "Connection Error...".mysqli_connect_error();
}
else
{
echo "<h3>Database connection Success...</h3>";
}
?>
This is really an opinion question since you've not posted any code. It helps to start by reading the docs and examples that are provided with PHPMailer. In your case, start with the mailing list example.

Resources