How to send email using php mailer? - php

i have installed php mailer using composer PHPMailer/PHPMailer and i got PHPMailer-Master directory but how and what files can i include to my mail file i.e sendEmail.php.
Here is my sendEmail.php-
include 'What is include paht?'
$mail = new PHPMailer;
$mail->setFrom($email, $name);
$mail->addAddress('dev5.veomit#gmail.com', 'Admin');
$mail->Subject = 'First PHPMailer Message';
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
if(!$mail->send()) {
header("Location: https://m-expressions.com/test/voy/");
}else {
echo 'Mailer error: ' . $mail->ErrorInfo;
}
Please help me to complete this code. thanks in advance.

If you have installed the PHPMailer via Composer, you should have a vendor folder in your project. Include /vendor/autoload.php

Related

phpMailer Not Working on web host

Can someone tell me why phpMailer not working on my webhost. I test my website locally with XAMMP and email sending with phpMailer is working correctly but when I upload my files to my webhost server it doesn't work. my script do not tell me any errors. neither my server error_log message is appended.
Is there any configuration that I may need to do on my server?
This is how am using phpMailer:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//mailer function
function sendmail($mailFrom,$orgName,$toMail,$replyTo,$mailSugject,$mailBody){
//Load composer's autoloader
require 'mailer/vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Recipients
$mail->setFrom($mailFrom, $orgName);
$mail->addAddress($toMail);// Add a recipient
$mail->addReplyTo($replyTo, $orgName);
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $mailSugject;
$mail->Body = $mailBody;
$mail->send();
return array('Mailer' => 1,'msg' => 'Message has been sent');
} catch (Exception $e) {
return array('Mailer' => $mail->ErrorInfo ,'msg' => 'Message could not be sent');
}
}

New PHPMailer required PHPMailerAutoload.php?

I'm working on a project, And i need send email after enter email and password by user. They can activate them account click that send email. But i got error when i using PHPMailer. Error was that like
Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer' not found in
C:\xampp\htdocs\E-Commerce-New-2\registration.php:20 Stack trace: #0
{main} thrown in C:\xampp\htdocs\E-Commerce-New-2\registration.php on
line 20
In new PHPMailer version use PHPMailerAutoload.php file? I think answer is no.
I tried use that code $mail = new PHPMailer\PHPMailer(); and also this code $mail = new PHPMailer(); No one is worked yet. I put my file structure down below
And here i'm past my codes lines.
<?php
session_start();
include("includes/db.php");
include("functions/functions.php");
include('PHPMailer/PHPMailer.php');
?>
<?php
// If the values are posted, insert them into the database.
if (isset($_POST['email']) && isset($_POST['password'])){
$email = $_POST['email'];
$password = $_POST['password'];
$verification_key = md5($email);
$query = "INSERT INTO `customers` (customer_pass,customer_email,verification_key) VALUES ('$password', '$email','$verification_key')";
$result = mysqli_query($con, $query);
if($result){
$mail = new PHPMailer\PHPMailer();
$mail->setFrom('noreplay#clickshop.com');
$mail->addAddress($email,'test');
$mail->Subject = "Verify Your Account";
$mail->isHTML(true);
$mail->Body = ' Please verify your email address to activate your account by clicking on this link <br>
<a href="http://localhost/E-Commerce-New-2/verify.php?key="'. $verification_key.'>Click Here</a>';
if(!$mail->send()) {
$fmsg= 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
} else {
$smsg = 'User Registration successfull. Please Check your email to Activate Account!';
}
}else{
$fmsg = "User registration failed";
}
}
?>
Can anybody give me a suggestion?
In the new PHPMailer v6.0+, you will now need to import the PHPMailer classes into the global namespace at the very top of your PHP script.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.example.com";
$mail->SetFrom("$from", "$from");
$mail->AddAddress("$to");
$mail->Subject = "$subject";
$mail->Body = "$message";
$mail->Send();

$mail->send() in PHPMailer is returning true but I didn't receive mail

I'm using PHPMailer to send emails from my website but $mail->send() is returning true and mail is not sending. No error is reporting in my error log. I hosted my site in Bigrock. I didn't find any errors in my code.
<?php
if(isset($_POST['submit']))
{
require 'class.smtp.php';
require 'PHPMailerAutoload.php';
ini_set('SMTP','localhost' );
ini_set('sendmail_from', 'exmaple#gmail.com');
$fromrec=$_POST['from'];
$from="example#gmail.com";
$subject=$_POST['sf'];
$message=$_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->setFrom($from, 'Rahul');
$mail->addAddress("example1#gmail.com");
$mail->Subject = $subject;
$mail->Body = "From:".$fromrec."".$message;
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else if($mail->send()) {
echo 'Message has been sent.';
echo $mail->ErrorInfo;
}
else
{
echo 'Mailer error: ' . $mail->ErrorInfo;
}
}
?>
On this issue, I have consulted Bigrock customer care and I made a chat with them. It's simple in the above code the from address email id must be domain specific and to address would be anything. After changing the email id to domain specific and Host to mail.example.com. My problem has been solved. If anyone got the same problem please try this.

PHPmailer Fatal Error

Hi guys i'm trying to use a php mailer however I keep getting issues for example in the sampler my emails simply won't send. So I changed to this new method and when testing i'm getting this error: Fatal error: Class 'SMTP' not found in /home3/hutch/public_html/Murphy/class.phpmailer.php on line 1325 anyone know why this is. I tried chanign the require to ("PHPMailerAutoload.php"); Which still didn't work still got the same error. I got both class.phpmailer.php and PHPMailerAutoload.php from the Github hosted documentation so I'm sure they are up to date. Any ideas?
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.sulmaxmarketing.com"; // SMTP server
$mail->From = "info#sulmaxmarketing.com";
$mail->AddAddress("sulmaxcp#gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else {
echo 'Message has been sent.';
}
?>
You are sending email using SMTP, so you need to include the SMTP class file:
require("class.smtp.php");

Script php : send mail using phpmailer

I'm trying to send a simple email using phpmailer.
I really have no idea if it's relevant since I'm just doing a php script, but I have apache2 & sendmail, apache2 running and sendmail include in the php.ini (sendmail_path=etc).
So here is my code :
<?php
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = "<h1>Coucou</h1>";
$body = eregi_replace("[\]",'',$body);
$mail->AddReplyTo("totest#test.com","First Last");
$mail->SetFrom('fromtest#test.com', 'First Last');
$mail->AddReplyTo("totest2#test.com","First Last");
$address = "XXXX#gmail.com";
$mail->AddAddress($address, "XXXXX");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
//$mail->AddAttachment("./test.doc"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
So the thing is that my output is always "Message sent!", even if the script take a really long time to be executed (up to 10minutes !), but the mail never arrived to my mailbox (and I checked the spam folder).
Well I hope someone will have an explanation to that annoying problem!
Thank you anyway.
Regards,
Bdloul

Categories