Email not send using PHPMailer - php

I am a beginner in PHP programming and i have a problem sending email using localhost (Wamp Server) on windows, I looked dozen of answers on this problem but still unable to solve my problem,Is there any problem in my code?
Here is my code
<?php
/**
* Created by PhpStorm.
* User: Farrukh
* Date: 2/19/2016
* Time: 5:09 PM
*/
require "PHPMailer-master/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Username = 'myemail';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->From = 'farrukh#gmail.com';
$mail->FromName = 'FMailer';
$mail->addAddress('myfrien#gmail.com', 'Farrukh');
$mail->WordWrap = 50;
$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->isHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>

Related

PHPMailer : SMTP host connection error on Godaddy

Mailer Error: SMTP Error: Could not connect to SMTP host.;
this error is generating when i run this code in godaddy.
<?php
require("PHPMailer_5.2.0/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtpout.secureserver.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#domain.com"; // SMTP username
$mail->Password = "******"; // SMTP password
$mail->From = "username#domain.com";
$mail->FromName = "User";
$mail->AddAddress("Sendto#gmail.com"); // name is optional
$mail->WordWrap = 50; // set word wrap to 50 characters
$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. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Try below code it's working for me!
<?php
require("src/PHPMailer.php");
require("src/SMTP.php");
require("src/Exception.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->Port = 25;
//Recipients
$mail->setFrom('from#gmail.com', 'Mailer');
$mail->addAddress('to#gmail.com', 'XYZTABC');
//Content
$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 "<pre>";
print_r($e);
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
If i understand correctly, using the SMTP address you are using you need to specify the port too (465).
However, according to the godaddy documentation, you should be using the following SMTP server:
relay-hosting.secureserver.net
This code may help you.
$mail->Host = "smtpout.secureserver.net";
your host name should be like mydomain.com
$mail->Port = 465;
i solved this using localhost as smtp server, port 25, no ssl and no authentication.
I'm using wordpress, with divi theme on a godaddy server.

Send dynamic pdf using phpmailer

I'm building a tremendous web application and now in the last steps the problem i have is that the dynamically generated using fpdf pdf which successfully is created is not sent through phpmailer. Below is the code:
<?php
session_start();
$title=$_SESSION['position'];
$fname=$_SESSION['user_name'];
$sec_name=$_SESSION['sec_name'];
$stel=$_SESSION['stel'];
$wtel=$_SESSION['wtel'];
$user_email=$_SESSION['user_email'];
$position2=$_SESSION['position2'];
$company=$_SESSION['company'];
$companyInd=$_SESSION['companyInd'];
$address=$_SESSION['address'];
$city=$_SESSION['city'];
$postcode=$_SESSION['postcode'];
$email="test#example.com";
require('fpdf.php');
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Dos LTD');
$pdf->Cell(0,10,"INVOICE",0,0,R);
$pdf->Ln(20);
$pdf->Cell(0,10,"Address: {$address}");
$pdf->Ln(20);
$pdf->Cell(0,10,"City: {$city} Postal code: {$postcode}");
$pdf->Ln(20);
$pdf->Cell(0,10,"Phone: {$wtel}");
$pdf->Ln(20);
$pdf->Cell(33,7,'QUANTITY',1,0,'L',0);
$pdf->Cell(85,7,'DESCRIPTION',1,0,'C',0);
$pdf->Cell(45,7,'UNIT PRICE',1,0,'L',0);
$pdf->Cell(25,7,'TOTAL',1,0,'L',0);
$pdf->Ln(7);
$pdf->Cell(33,37,'4',1,0,'L',0);
$pdf->Cell(85,37,'Example',1,0,'C',0);
$pdf->Cell(45,37,'$3.50',1,0,'L',0);
$pdf->Cell(25,37,'$14',1,0,'L',0);
$pdf->SetTitle("Invoice", true);
$pdf->Output("filename345.pdf","I");
require_once ('phpmailera.php'); //class.phpmailer.php
require ('PHPMailerAutoload.php');
require_once ('smtp.php');
$mail-> new PHPMailer;
$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 = 'myemail#gmail.com'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From("test#anemail.com");
$mail->setFrom("Email Sent!");
$mail->addAddress("anemail#gmail.com"); // Add a recipient
$mail->addReplyTo('test#anything.com');
$mail->addCC('test#test.com');
$mail->addBCC('test#likost7.com');
$mail->addAttachment("filename345.pdf"); // Add attachments
$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';
var_dump($mail->send());
$mail->unlink("filename345.pdf");
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
*/
?>

Why can I not send email using PHPMailer?

Here is my code. I really need to know what is wrong with my code.I am getting this error:-
"SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting"
<?php
require_once 'PHPMailer/class.phpmailer.php';
//sending mail for verification
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Host = 'mail.mydomain.com';
$mail->Username = 'myusername'; // Enter your SMTP username
$mail->Password = "mypassword"; // SMTP password
$mail->From = "support#mydomain.com";
$mail->FromName = " Verification";
$mail->AddAddress("emailid#yahoo.com", "Name");
$mail->AddReplyTo("myemail#gmail.com", "Information");
$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";
if($mail->Send()){
echo "Mail sent";
}else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
My credentials are correct.I did follow the link in the error message and tried almost everything mentioned there but nothing seems to work for me. This is the shortest and trimmed version of my original code, the idea is to tell that even this is not working for me.
I found the solution my this problem. Here are the changes I made and it works perfectly fine.
<?php
require_once 'PHPMailer/class.phpmailer.php';
require_once 'assets/import/PHPMailer/class.smtp.php'; //I added this here which wasn't added in original script
//sending mail for verification
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Host = 'mail.mydomain.com';
$mail->Username = 'myusername'; // Enter your SMTP username
$mail->Password = "mypassword"; // SMTP password
$mail->From = "support#mydomain.com";
$mail->FromName = " Verification";
$mail->AddAddress("emailid#yahoo.com", "Name");
$mail->AddReplyTo("myemail#gmail.com", "Information");
// Adding SMTPOption is the main thing that solved my problem. I got this from troubleshooting page of PHPMailer. And they
// recommended to do this only if every other option fails.
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$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";
if($mail->Send()){
echo "Mail sent";
}else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

I am receiving this error from phpmailer Message could not be sent.Mailer Error: SMTP connect() failed?

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

PHPmailer will not send emails to my gmail account

I'm using this code to try and get PHPmailer to send emails to people who use my website as gmail has blocked me from using php mail() apparently. However, It returns the error Mailer Error: SMTP Error: The following recipients failed: myemail#gmail.com
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'myhost'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'site email'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'site email';
$mail->addAddress('myemail#gmail.com'); // Name is optional
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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;
exit;
}
echo 'Message has been sent';
Can someone help me fix this problem?
mail($to, $subject,$message, $headers, "-fSENDEREMAILID");
try to add -f before the sender email id in the mail function and it works i guess

Categories