The below function to send email but got any mail or error :
function send_Email($email, $subject, $body ,$cc = '')
{
$this->Log("Sending email with To: ". $email. " <br>Subject: ". $subject ." <br>Body: ".$body);
$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = email_host;
$mail->Username = email_username;
$mail->Password = email_password;
$mail->SetFrom('support#xyv.com', 'zzz Support');
$mail->AddReplyTo('support#xyv.com', 'zzz Support');
$mail->AddAddress($email);
$mail->AddBCC('support#xyv.com');
$mail->AddBCC('zzz#zzz.com');
if(!empty($cc)) {
$mail->addCC($cc);
}
$mail->Subject = $subject;
$mail->Body = $body;
echo "<pre>"; print_r($mail);echo "</pre>";
$response = $mail->Send();
var_dump($response);
return $response;
}
Debug : IN DEBUG MODE NOT GET ERROR
[error_count:protected] => 0
It would really help to read the docs and base your code on the examples provided, which show how to get info about errors, and show debug output. You're not getting any output because you're not asking for any. From the example in the readme:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
That will show you the reason for any sending failure. To enable SMTP debug output, do this:
$mail->SMTPDebug = 2;
Related
I am trying to send emails using phpmailer and getting the error as mentioned in the title. I am using XAMPP for apache server.
I have searched almost every link on google and everywhere but nothing is working for me.
<?php
ini_set('display_errors', 1);
require 'autoload.php';
require 'class.phpmailer.php';
//require 'class.smtp.php';
$mail = new PHPMailer;
$mail->IsSMTP();
//$mail->SetLanguage("en", 'language');
$mail->Mailer = "smtp";
//$mail->SMTPSecure = "tls";
$mail->Port = 465;
$mail->SMTPDebug = 2;
$mail->SMTPAuth=False;
$mail->Host="smtp.gmail.com";
$mail->Username = "email#gmail.com";
$mail->Password = "password";
//$mail->setFrom("email#gmail.com");
$mail->From = "email#gmail.com";
$mail->addAddress('email#gmail.com','Rishabh Goel');
$mail->isHTML(false);
$mail->Subject = 'First Subject';
$mail->Body = 'Mail body in HTML';
$mail->AltBody = 'This is the plain text version of the email content';
/*try{
$mail->Send();
echo "Thanks. Bug report successfully sent.
We will get in touch if we have any more questions!";
} catch(Exception $e){
//Something went bad
echo "Mailer Error: - " . $mail->ErrorInfo;
}*/
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent successfully";
}
?>
How can I redirect when the email is being sent with PHPMailer?
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
// $mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->Host = "email-smtp.us-west-2.amazonaws.com";
$mail->Username = "AKIAIVSF45PCGR7NZWNQ";
$mail->Password = "Am2SBg4vluOvIc1+kycsWpCnxtf3jhGjYCAdBv7YYp/y";
//
$mail->SetFrom('test#gmail.com', 'Z-Reports'); //from (verified email address)
$mail->Subject = "Z-Reports (sent via smtp)"; //subject
//message
$body = emailZReports($total_sales, $inventory);
// $body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
//
//recipient
$mail->AddAddress("test#gmail.com", "Z-Reports");
//Success
if ($mail->Send()) {
echo "Message Sent!";
}
//Error
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
How can I redirect to a specific page instead of displaying Message Sent!?
if you are using SMTP, you can't redirect to another page if $mail->SMTPDebug is not set to 0. This is because it sends out headers which you can't modify with header("Location: path/to/redirect"). Set $mail->SMTPDebug = 0; so that you can redirect to another page after $mail->send(). but you have to do that only when you have finished debugging and your mail is sending successfully.
You can use php header function
//Success
if ($mail->Send()) {
header("Location:Yourfile.php");//echo "Message Sent!";
}
Make sure there is not echo or any output before this function
You can use like this,
See Header for more info.
It can be like this,
//Success
if ($mail->Send()) {
header('Location: nextpage.php');
}
Otherwise, you can use Javascript to redirect the user.
Just use
window.location = "http://www.nextpage.com/"
This works
if(!$mail->Send()) {
echo "Message could not be sent. ";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
} //echo getcwd();
header("Location:index.php") //redirects to a page named index.php;
// make another script for redirecting
<?php
require_once("mail_function.php");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
header("Location: form.php");exit; //redirect to form submit page
} else {
echo "Message sent!";
header("Location: index.php");exit; //redirect to your home page
}
?>
//mail_function.php
<?php
require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Mailer = "smtp";
$mail->SetFrom("xxxx#gmail.com", "web");
$mail->Port = '587'; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication.
$mail->Username = 'xxxx#gmail.com'; //Sets SMTP username
$mail->Password = 'xxxx'; //Sets SMTP password
$mail->SMTPSecure = 'tls'; //Sets connection prefix.
$mail->FromName = $_POST["name"];
$mail->AddAddress($email); //Adds a "To" address
$mail->AddCC($_POST["email"], $_POST["name"]); //Adds a "Cc" address
$mail->WordWrap = 50; //Sets word wrapping on the body
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = "somthing";
$mail->MsgHTML($message_body);
$mail->Body = "somthing";
$result = $mail->Send();
return $result;
?>
<?php
require_once 'PHPMailer-master/class.phpmailer.php';
require_once 'PHPMailer-master/class.phpmaileroauthgoogle.php';
require_once 'PHPMailer-master/PHPMailerAutoload.php';
require_once 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = TRUE;
//$mail->SMTPDebug =2;
$mail->Host = 'smtp.gmail.com';
$mail->Username = 'zhaider113#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 587;
$from = 'shahghafoor439#gmail.com';
$mail->setFrom($from, 'Ghafoor Shah');
$mail->addReplyTo($from, 'Ghafoor Shah');
$mail->addAddress('zhaider113#gmail.com', 'zeeshan');
$mail->Subject = 'This is subject';
$mail->Body = 'This is the body of email';
$mail->AltBody = 'This is the body of email';
$mail->send();
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
?>
I try to send email but it not send and give error message which is:SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting */
try with $mail->SMTPSecure = 'tls';
See full example at PHPMailer's gmail example below: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
Make sure you include correct PHPMailer Library.
And make this changes
Some Servers not response for SSL(Secure). So that change this $mail->SMTPSecure = 'tls';
And In your code you have two Mail sending option $mail->send();
//$mail->send();//Comment this
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
Now either mail sent Or either It will print Error log
I don't know if my code is the issue or if I had configured Zoho's SMTP's settings incorrectly.
Basically I want the ability to dynamically send emails with a simple php function like for example
phpMail("to#example.com", $subject, $body, "from#example.com", "replyto#example.com");
This is my PHPMailer.php script (it sees the function and its settings)
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.zoho.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**REMOVED**'; // SMTP username
$mail->Password = '**REMOVED**'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true); // Set email format to HTML
// SYTAX: phpMail($from, $reply, $to, $subject, $body);
function phpMail($to, $subject, $body, $from = "from#example.com", $reply = "replyto#example.com") {
if (isset($from))
$mail->From = $from;
$mail->FromName = "testing";
if (isset($to))
$mail->addAddress($to);
if (isset($reply))
$mail->addReplyTo($reply);
if (isset($subject))
$mail->Subject = $subject;
if (isset($body))
$mail->Body = $body;
$mail->AltBody = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
Now the issue with this is that, I'm not receiving emails nor am I receiving any errors / messages but it's also not sending me an email.
When you make a PHP function, make sure your objects aren't outside of your function.
My error wasn't that though, it was basically my own stupidity which had nothing to do with the code but more of my "form post data".
The following configuration works for me using zoho mail.
Give it a try:
$mail=new JPhpMailer;
$mail->IsSMTP();
$mail->Host='smtp.zoho.com';
// Enable this option to see deep debug info
// $mail->SMTPDebug = 4;
$mail->SMTPSecure = 'ssl';
$mail->Port='465';
$mail->SMTPAuth=true;
$mail->Username='your_email_address';
$mail->Password='your_eamil_address_password';
$mail->isHTML(true);
$mail->SetFrom('your_email_address','Your Name');
$mail->Subject='PHPMailer Test Subject via smtp, basic with authentication';
$mail->AltBody='To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('<h1>JUST A TEST!</h1>');
$mail->AddAddress('destination_email_address','John Doe');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I'm trying to send some mail using php mailer, the boring thing is that one email eas sent the first time and I have no idea what I've done that makes this code to now fail.
ERROR: SMTP server error: authentication required
require("../mailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtpout.secureserver.net"; // SMTP server
$mail->From = "site email";
$mail->AddAddress("useremail");
$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.';
}
I'm hosting with GoDaddy.
Thanks to Jake.
Incase someone goes through this again here is what i put in my code.
CODE
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtpout.secureserver.net"; // SMTP server
$mail->Username = "email#domain";
$mail->Password = "pwd";
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->SMTPDebug = 2; /// i guess this is for reporting...
$mail->From = "email#domain";
$mail->AddAddress("recipient email");
$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.';
}