Can't determine if PHPMailer is working - php

I have a php page containing PHPMailer. Apperently it was working in both the test server and the production server. It was in a separate php file, but since I need to do send different kinds of email with different alert messages on screen, I placed my code on the same php file as my form.
What I did is:
if(isset($_POST))
{
require_once('PHPMailer/class.phpmailer.php');
$mail = new PHPMailer(true);
if(isset($_POST['feedback_mail']))
{
$mail->Host = "my mail host"; // SMTP server
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Username = "the account that I will be using"; // SMTP account username
$mail->Password = "my password";
$mail->AddReplyTo($_POST['Email'], $_POST['Name']);
$mail->AddAddress('address to where I will send it', 'Name');
$mail->SetFrom($_POST['Email'], $_POST['Name']);
$mail->Subject = 'Subject'.$_POST['Subject'];
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($_POST['Body']);
$mail->Send();
$marker = 1;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
$marker = 0;
} else {
$marker = 1;
}
}
}
When I try to send an email message, it says that the message is sent, but I have been waiting for almost an hour but there is still no message recieved on my email. I know that my test server allows sending of email because I also have another application that can send mails. And I am using the same account for sending these mails. How do I know if PHPMailer is really working or not?
EDIT: I found out it might probably be my local network connection that's stopping the mail. I just tried running the from on a VPN connection and somehow it recieved an email.
Either my network connection is having problems or the server is deliberately blocking IP's from my country.

Related

how to send two different email to two different email addresses using PHPMailer in php

I am trying to send two different emails to two different recipients using PHPmailer but only the second email is arriving.
My code:
/**
* This code shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "olaozias#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('olaozias#gmail.com', 'Department of Information Science');
//Set an alternative reply-to address
$mail->addReplyTo('olaozias#gmail.com', 'Department of Information Science');
//Set who the message is to be sent to
$mail->addAddress($email , 'Parent');
//Set the subject line
$mail->Subject = 'Student Attendance System';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->Body = 'Dear Parent \r\n This email is sent from the university of gondar , Department of information science to inform you that your child '. $firstname.' has been registered for semester '.$semister. ' in order to see your child attendance status and to communicate easily with our department use our attendance system. First download and install the mobile application which is attached in this email to your phone and use these login credentials to login to the system \r\n Your child Id: '.$student_no. '\r\n Password: '.$parent_pass.'\r\n Thank you for using our attendance system \r\n University of Gondar \r\n Department of Information Science ';
//Attach an image file
//$mail->addAttachment('AllCallRecorder.apk');
$mail->send();
$mail->ClearAddresses();
$mail->AddAddress($stud_email,'Student');
$mail->Subject = 'Student Attendance System';
$mail->Body = "email 2";
//send the message, check for errors
if (!$mail->Send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
echo '
<script type = "text/javascript">
alert("Mailer Error: " . $mail->ErrorInfo);
window.location = "student.php";
</script>
';
} else {
echo '
<script type = "text/javascript">
alert("student Added successfully and an Email the sent to email address provided");
window.location = "student.php";
</script>
';
//echo "Message sent!";
}
the second email is delivered successfully but the first one is not.
There are a couple of different possibilities. The fact that the second one is sending properly is a good indication that your code is working in general. Focusing on the first one, I'd suggest three things:
Add error checking to the first send() call. You have if (!$mail->Send()) {... on the second one, but you aren't checking the first one. You can use $mail->ErrorInfo as you have in a comment in the second part. (By the way, the $mail->ErrorInfo you have in the script tag will not work. Variables in single quoted strings like that will not be parsed, so you'll just get the literal string "$mail->ErrorInfo" there if there is an error.)
Add error checking to the first addAddress() call. PHPMailer will give you an error that you can check if the email address is invalid for some reason. As far as the code you've shown here, $email appears to be undefined, but so does $stud_email and you've said that one is working properly, so I assume those are both defined somewhere before the code that you've shown here, but a possible cause for this is that $email is undefined or doesn't have the value you expect it to.
The email is being sent, but not received. It's pretty easy for a message to be mis-identified as spam at multiple points between the sender and the receiver. This is more difficult to diagnose, but if you add the error checking to the first send() call and don't get any errors, you'll at least be able to rule that out as a point of failure.
you can do an array with de emails and subject.
$recipients = array(
'person1#domain.com' => 'Person One',
'person2#domain.com' => 'Person Two',
// ..
);

PHPmailer - 500 internal server error

I keep on having the 500 internal sever error with my web hosted by GoDaddy, using the PHPmailer; I've tried several solutions available on StackOverflow relating to my challenge, but none of them worked.
Here is my code:
require 'phpmailer/PHPMailerAutoload.php';
$feedback='';
$flag = array();
if(isset($_POST["submitlogin"]) and $_SERVER['REQUEST_METHOD'] == "POST"){
$name = seo_friendly_url($_POST['name']);
$email = seo_friendly_url($_POST['email']);
$subject = seo_friendly_url($_POST['subject']);
$message = seo_friendly_url($_POST['message']);
//Email
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email)) {
$feedback="Invalid email format";
array_push($flag,"false");
}else {
array_push($flag,"true");
}
//Email
//Name
if (preg_match('/^[-a-zA-Z0-9._]+$/', $name)){
array_push($flag,"true");
}else {
$feedback="Invalid name format";
array_push($flag,"false");
}
//Name
if (!in_array("false", $flag)) {
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "smtpout.asia.secureserver.net";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
//$mail->Username = 'email#email.com';
//$mail->Password = 'password';
$mail->SMTPAuth = false;
//Set who the message is to be sent from
$mail->setFrom('email#email.com');
//Set an alternative reply-to address
$mail->addReplyTo($email);
//Set who the message is to be sent to
$mail->addAddress('email#email.com'); // Add a recipient
$mail->addAddress('email#email.com');
$mail->addAddress('email#email.com);
//Set the subject line
$mail->Subject = 'HLS Inquiry';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($message);
$email_ar = $email;
$subject_ar = $name.", Thank you for your Inquiry";
$acknowledgement_receipt = '
<div style="padding:10px 20px;font-size:16px;">
<p>Dear '.$name.',</p>
<p>We've received your message and would like to thank you for contacting us. We will reply by email shortly.</p>
<p>Talk to you soon,</p>
<p>
<div>Customer Service</div>
</p>
</div>
';
if ($mail->send()) {
$feedback = "<span style='color:green;'>Thank you for your inquiry. We will respond to you within 24 hours.</span>";
}
}
Please note that I replaced some of the email values as: email#email.com for my privacy.
I believed that the email setups are correct because I already used those setups from my friend's website hosted by JustHost.
This line is missing a single quote:
$mail->addAddress('email#email.com);
This is quite obvious in SO because you can see how the syntax highlighting breaks at that point.
When you get an error 500 and nothing is displayed, it's a sign that you should be looking in your web server's error log.
There is a "split" function used to write the PHPMailer class. This function has been long deprecated and as such gives error in later PHP versions.
Find them and replace with "explode".
This worked for me.

Could not send mail to other accounts apart from my own?

I've downloaded PHPMailer library from github and used the following script to send the mail. I've tried to send email to my own account and it worked. It printed "Message has been sent" and received the email in gmail. But when I tried sending mail to my friend's account, he didn't receive the email. But the script says message has been sent.
<?php
include('PHPMailer-master/PHPMailerAutoload.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "example#gmail.com";
$mail->Password = "examplepassword";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("example2#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
I'm using xampp. What's the reason behind the problem, do I need to change any settings in my gmail settings?
EDIT: Now I got mail to second address but after 40 mins. But when I send it first address, it is received immediately. Don't know why? I want to use it for email address verification, 40 mins is very long.
below link will help you:
Sending emails through PHP mail is slow
http://stackoverflow.com/questions/6380477/sending-emails-through-php-mail-is-slow

PHPMailer Change the senders name

I'm having trouble setting the name that's sent with mailings sent using the PHPMailer class.
I've written the following function so that it can be used in a similar way the php's bulit in mail() function.
function pmail($to, $subject, $message, $headers = "", $attachments = "")
{
date_default_timezone_set('Europe/London');
require_once($_SERVER['DOCUMENT_ROOT']."/lib/inc/class.phpmailer.php");
//include($_SERVER['DOCUMENT_ROOT']."/lib/inc/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$defaultEmail = "reply#example.com";
$defaultEmailName = "Web Mailer";
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.example.com"; // SMTP server
$mail->SMTPDebug = false; // enables SMTP debug information (for testing, 1 = errors and messages, 2 = messages only, false = off)
$mail->SMTPAuth = true; // enable SMTP authentication
//$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "mail.example.com"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "###"; // SMTP account username
$mail->Password = "###"; // SMTP account password
$mail->SetFrom( ($headers['fromEmail'] != "" ? $headers['fromEmail'] : $defaultEmail), ($headers['fromName'] != "" ? $headers['fromName'] : $defaultEmailName) );
$mail->AddReplyTo( ($headers['replyToEmail'] != "" ? $headers['replyToEmail'] : $defaultEmail), ($headers['replyToName'] != "" ? $headers['replyToName'] : $defaultEmailName) );
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($message);
foreach($attachments as $attachment) {
//$mail->AddAttachment("images/phpmailer.gif"); // attachment example
$mail->AddAttachment($attachment);
}
if(!$mail->Send()) {
//echo "Mailer Error: ".$mail->ErrorInfo;
return false;
} else {
//echo "Message sent!";
return true;
}
}
When testing with something like so;
pmail("test#test.com", "test email", "test message here");
Everything works fine, the from address shows up as reply#example.com in the headers as expected, however the name that I see in the inbox of the recipient is not Web Mailer its the default account associated with the user whos credentials are used to send the email.
In the headers the from name does show up as Web Mailer, but its the inbox where I want to see it
We are unable to set up more user accounts on our system to allow us to just make a new one with the desired name and email, therefore we have to send it via an existing user account.
In this case mine, and emails get sent with my name attached, but we want the name to show up as Web Mailer.
Is this even possible?
If you use PHP Mailer API of GitHub, you can use this to set the Sender Name:
$mail->SetFrom("$youremail ", "Your Name");
Turns out my function does work as intended.
Because I was receiving emails from this system to internal addresses, they all automatically had me in their address book, therefore as the address used to send out the emails was associated with my personal address, they saw my name instead of "Web Mailer".
When testing with an external email account the senders name is listed correctly.
I once used this php code and it worked well: (it displayed what's below, not my gmail credentials)
$headers = "From: Cartrader UK <noreply#cartrader.co.uk";
.
.
.
if (mail($to, $subject, $msg, $headers)) {
echo "Mail sent successfully";
} else {
echo "There was some problem sending the E-Mail";
}
Maybe it helps you.
EDIT:
I would try change your line with SetFrom to:
$mail->SetFrom($defaultEmail,$defaultEmailName);
You can set in your mail.php
$headers = "From: Your Name <yourname#example.com>";

PHPMailer With Attachment Never Received

I am tying to send an email with an attachment using phpmailer.
include_once('/home/site/PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$body = $mail->getFile('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.free.fr"; // SMTP server
$mail->IsSendmail(); // telling the class to use SendMail transport
$mail->From = "name#sub.fr";
$mail->FromName = "name";
$mail->Subject = "subject";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress("sub#sub.net", "name");
$mail->AddAttachment("mylist.csv"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I receive a "Message sent !" on execution, but no email is ever received.
It is very possible the user you sent the email to did not recieve it due to the fact a lot of mail providers such as AOL and Yahoo block massive amounts of Ip addresses related to email spam. So if the server you are running this php script from is on their blacklist the user will not even receive it to the spam folder.
Check your php email logs.
http://help.yahoo.com/l/us/yahoo/smallbusiness/webhosting/php/php-03.html
Also, if this is not the case, try making sure the file your trying to include exists, and you are passing the correct path to the file. Like #Waygood said, try seeing if it sends without the attachment.

Categories