I have been reading through SO trying to find an answer to my PHPMailer issue.
I am using the latest PHPMailer code and I have my code in a try and catch block. I am using the same SMTP credentials on both the XAMPP hosted site and the 1&1.co.uk hosted site. I have also tried printing debug to a file and know the script gets as far as the $mail->send() section, but then all I get back is a page saying:
The www.reasonstobejolly.com page isn’t working
www.reasonstobejolly.com didn’t send any data.
No exceptions or anything thrown. Can anyone explain why this isn't working?
require '../../../PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'auth.smtp.1and1.co.uk'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '##########'; // SMTP username
$mail->Password = '##########'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('orders#reasonstobejolly.com', 'Orders at Reasons to be Jolly');
$mail->AddAddress($email, $firstName." ".$lastName); // Add a recipient
$mail->addReplyTo('enquiries#reasonstobejolly.com', 'Order Enquiry');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Your Order Has Been Received";
$mail->Body = "Dear ".$firstName." ".$lastName."<br/>"
. "Your order has been received; it will be processed as soon as possible and should be with you in the timeframe specified in our shipping guidelines.<br/>
";
if (isset($randomPassword)) {
$mail->Body .= "<p>Username: $email</p><p>Password: $randomPassword</p>";
}
$mail->Body .= "<p>Order Reference : $transactionId<br/>
Order Date : ".date('d-m-Y H:i:s', strtotime($orderTime))."</p>
<p>BILL TO:<br/>
$shipToName <br/>
$shipToStreet<br/>
$shipToCity<br/>
$shipToState<br/>
$shipToZip<br/>
</p>
<p>
DELIVER TO:<br/>
$shipToName <br/>
$shipToStreet<br/>
$shipToCity<br/>
$shipToState<br/>
$shipToZip<br/>
</p>
<table>
<tr><td>Qty</td><td>Description</td><td>Price</td></tr>";
$orderOverviewID = getOrderOverviewIDByPayPalReference($transactionId);
$orderItemsArray = getItemsByOrderOverviewID($orderOverviewID);
$postage = 0;
foreach ($orderItemsArray as $id => $order) {
$productEmail = getProductByProductID($order->productID);
$postageAmount = getPostageByPostageID($productEmail->postageID);
$mail->Body .= "<tr><td>".$order->itemQuantity."</td>"
. "<td>".$productEmail->productName."</td>"
. "<td>£". (getDimensionByID(getColourByID($order->colourID)->dimensionID)->dimensionPrice) * $order->itemQuantity."</td></tr>";
if ($postageAmount->postagePrice > $postage) {
$postage = $postageAmount->postagePrice;
}
}
// these are from create invoice.
$vat = $amt * 0.2;
$subtotal = $amt - $postage;
// TODO VAT and postage need to be calculated in the Paypal bit.
$mail->Body .= "<tr><td></td><td align='right'>Sub Total:</td> <td>£$subtotal</td></tr>
<tr><td></td><td align='right'>Standard Delivery:</td><td>£$postage</td></tr>
<tr><td></td><td align='right'>(Including VAT # 20%):</td><td>£$vat</td></tr>
<tr><td></td><td align='right'>GRAND TOTAL:</td><td>£$amt</td></tr>
</table>
If you have a query, or require further information, please contact Customer Service at: http://www.reasonstobejolly.com";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Turns out after a phone call to 1and1.co.uk, that the only mail option they support is mail(). They told me that PHPMailer will not work on their servers.
Related
I wrote a PHP mailer code to send emails to multiple recipients. But unfortunately i found a drawback in script, i am fetching emails from database, lets see if it fetches 5 emails and try to send email to all of them and no.3 email is not a valid address(any reason) then my script just "exist" the function entirely by sending mail to 1-2 and showing error invalid email 3 and "exit"(not continuing further), what i want is rather to "exit" the function it should just skip that iteration statement(email) and then go on to next one like the "continue" statement. Also in code the message and subject are being sent from another page which consists of a form.
Here is the code :
<?php
// 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;
if(isset($_SESSION['userIs']))
{
$message = $_POST['message'];
$subject = $_POST['subject'];
//Load composer's autoloader
require './PHPMailer/vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 1; // 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 = 'trying.test.00'; // SMTP username
$mail->Password = 'trying.test.00'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('trying.test.00#gmail.com', 'Tester');
include('./create-connection.php');
$get_list = "select name,email from signup";
$result = $conn->query($get_list) ;
if($result->num_rows>0)
{
while($row_list = $result->fetch_assoc())
{
$to = $row_list['email'];
$name = $row_list['name'];
$mail->addAddress($to,$name); // Add a recipient
}
}
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = strip_tags($message);
if($mail->send())
{
echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-check"></i></div>';
}
} catch (Exception $e) {
echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-times"></i></div>';
}
$conn->close();
}
?>
You could validate the email addresses before sending.
Or better:
You could validate the email addresses before inserting in the database so you know that the database contains only valid data.
Just needed to add a validation to check if the email is valid or not without sending email inside while loop, then if email is valid process the mail else "continue" the current iteration(email) and jump to next email.
Thus after changing code ->
<?php
// 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;
if(isset($_SESSION['userIs']))
{
$message = $_POST['message'];
$subject = $_POST['subject'];
//Load composer's autoloader
require './PHPMailer/vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 1; // 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 = 'trying.test.00'; // SMTP username
$mail->Password = 'trying.test.00'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('trying.test.00#gmail.com', 'Tester');
include('./create-connection.php');
$get_list = "select name,email from signup";
$result = $conn->query($get_list) ;
if($result->num_rows>0)
{
while($row_list = $result->fetch_assoc())
{
//add a validation here
if(!filter_var($row_list['email'], FILTER_VALIDATE_EMAIL))
{
echo "invalid email".$row_list['email'];
continue;
}
else
{
$to = $row_list['email'];
$name = $row_list['name'];
$mail->addAddress($to,$name); // Add a recipient
}
}
}
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = strip_tags($message);
if($mail->send())
{
echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-check"></i></div>';
}
} catch (Exception $e) {
echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-times"></i></div>';
}
$conn->close();
}
?>
I want to check whether the SMTP Server is working or not by sending mail or pinging server and get response Receipt OK.
i want to check whether my smtp is down or up using php . I tried to test it using below PHPmailer code. Which is the best method to test whether smtp running using php script.
/**
* Simple example script using PHPMailer with exceptions enabled
* #package phpmailer
* #version $Id$
*/
require ('mailer/class.phpmailer.php');
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = "Please return read receipt to me.";
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 465; // set the SMTP server port
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Username = "bhakuq#gmail.com"; // SMTP server username
$mail->Password = "bhakuxxx"; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("bhakuq#gmail.com","Riya Patel");
$mail->From = "bhakuq#gmail.com";
$mail->FromName = "Riya Patel";
$to = "bhakuq#gmail.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message[Test Read Receipt]";
$mail->ConfirmReadingTo = "someone#something.com"; //this is the command to request for read receipt. The read receipt email will send to the email address.
$mail->AltBody = "Please return read receipt to me."; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
I tried above code but still no use.
I've been running PHPMailer for a year now on a php server. Everything was fine until 3 days ago when I started getting the following error:
SMTP Error: Could not authenticate.
Allow less secure apps is ON
Here is the code:
function SendEmail($to,$cc,$bcc,$subject,$body) {
require 'PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->SMTPDebug = 1;
try {
$addresses = explode(',', $to);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
if($cc!=''){
$mail->addCustomHeader("CC: " . $cc);
}
if($bcc!=''){
$mail->addCustomHeader("BCC: " . $bcc);
}
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->Username = "myemail#gmail.com"; // SMTP username
$mail->Password = "myemailpass"; // SMTP password
$webmaster_email = "myemail#gmail.com"; //Reply to this email ID
$name=$email;
$mail->From = $webmaster_email;
$mail->FromName = "Service";
//$mail->AddReplyTo($webmaster_email, "DiFractal Customer Service");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
} catch (phpmailerException $e) {
$myfile = fopen("debug_email.txt", "w");
fwrite($myfile,$e->errorMessage() . "\n" . $mail->ErrorInfo);
fclose($myfile);//Pretty error messages from PHPMailer
} catch (Exception $e) {
$myfile = fopen("debug_email_stp.txt", "w");
fwrite($myfile,$e->getMessage());
fclose($myfile);//Pretty error messages from PHPMailer
}
}
Note I just updated PHPMailer to the latest version to try to remedy the problem but nothing has changed! The old version 5.2.2 was still having the same problem!
EDIT: I just had one successful email go through to google and sent properly. Which now makes me question if it's lag issue or something of that sort. Does anyone know how phpmailer functions under high loads or if high loads can cause the above error?
Try going to:
myaccount.google.com -> "connected apps & sites", and turn "Allow less secure apps" to "ON".
Alternative:
Try changing SMTP Port to: 465 (gmail also).
I was having similar issues and needed to set the from address
$mail->setFrom('myemail#gmail.com', 'Webmaster');
Make sure you check google's usage limits! PHPMailer will not tell you particulars it will just give you the Could not authenticate error but the reason why can be because of your limits.
# https://support.google.com/a/answer/166852?hl=en
Upgraded to a new account with google business and switched to that account. Issue resolved.
I have been testing the following code for hours. The email will send to the addresses added through $mail->AddAddress() and in the received email it states the cc but the person cced does not receive the email. I have looked everywhere and can not find a solution to why this is happening. I have run tests and all variables are being submitted to this code properly.
My server is running Linux Red Hat
My Code:
require_once('../smtp/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = $port; // set the SMTP port for the GMAIL server 465 or 587
$mail->Username = $username; // GMAIL username
$mail->Password = $password; // GMAIL password
// Add each email address
foreach($emailTo as $email){ $mail->AddAddress(trim($email)); }
if($cc!=''){ foreach($cc as $email){ $mail->AddCC(trim($email)); } }
if($bcc!=''){ foreach($bcc as $email){ $mail->AddBCC(trim($email)); } }
$mail->SetFrom($emailFrom, $emailName);
$mail->AddReplyTo($emailFrom, $emailName);
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($content);
// $mail->AddAttachment('images/phpmailer.gif'); // attachment
// $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo'1';exit();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Old question, but I ended up here looking for an answer. Just learned elsewhere that those functions AddCC and AddBCC only work with win32 SMTP
Try using:
$mail->addCustomHeader("BCC: mybccaddress#mydomain.com");
See http://phpmailer.worxware.com/?pg=methods
Hope this helps someone, cheers!
$address = "xxxxx#gmail.com";
$mail->AddAddress($address, "technical support");
$address = "yyyyyy#gmail.com";
$mail->AddAddress($address, "other");
$addressCC = "zzzzzz#gmail.com";
$mail->AddCC($addressCC, 'cc account');
$addressCC = "bcc#gmail.com";
$mail->AddBCC($addressCC, 'bcc account');
I'm trying to send a file as an email attachment, but for some reason if the file is > 100k then the email doesn't go through, even though I get the email sent message.
It may also be a limit on the attachments in IIS smtp setting, but when I unchecked the Limit session size and limit message size options, it didn't change anything. I may have to restart the server tonight...
I don't know if it's a php.ini setting, or what.
<?
$path_of_attached_file = "Images/marsbow_pacholka_big.jpg";
require 'include/PHPMailer_5.2.1/class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = $message; //"<p><b>Test</b> another test 3.</p>";
$mail->AddReplyTo("admin#example.com","Admin");
$mail->From = "admin#example.com";
$mail->FromName = "Admin";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($attach){
$mail->AddAttachment($path_of_attached_file);
}
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
I might be wrong because I don't use IIS but the code you provided would actually use a native MTA not SMTP. As far as I know you have to use the IsSMTP() method to let PHPMailer know that you intend to use SMTP.
Something like this:
<?
$path_of_attached_file = "Images/marsbow_pacholka_big.jpg";
require 'include/PHPMailer_5.2.1/class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = $message; //"<p><b>Test</b> another test 3.</p>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->AddReplyTo("admin#example.com","Admin");
$mail->From = "admin#example.com";
$mail->FromName = "Admin";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($attach){
$mail->AddAttachment($path_of_attached_file);
}
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
Your code is not actually checking if the message was sent or not.
You need to change your code to check the return of the send method
if ($mail->Send())
echo 'Message has been sent.';
else
echo 'Sorry there was an error '.$mail->ErrorInfo;
This should give you the error message saying what is up if it does go wrong.