PHP mailer Error receiving an email - php

I am using the code below for the mailing list of the website I am working on, and it's working fine on my localhost using my credentials and on some other server using some other credentials were given to me.
<?php
if(isset($_POST['submit']))
{
require("class.phpmailer.php");
$mail = new PHPMailer();
$query = "SELECT * FROM mailing_list";
$result = mysqli_query($connect, $query)
or die('Error querying the database!');
$mail->IsSMTP();
$mail->Host= "smtp";
$mail->Username= "some email address was given to me";
$mail->Password= "password";
$mail->SMTPAuth= true;
$mail->SMTPSecure = 'tls';
$mail->From ="some email address was given to me";
$mail->FromName ="CSCA";
while($row = mysqli_fetch_array($result))
{
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$to = $row['email'];
$mail->ClearAddresses();
$mail->AddAddress($to);
$mail->Subject= $_POST['subject'];
$mail->Body = "Dear $first_name $last_name,\n" . $_POST['body'];
$mail->WordWrap = 50;
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
Now when I use the following code for the contact us page of the same website it works fine one my localhost using my own credentials, but it doesn't work on the other server I used above. I got a message in the spam 'mail delivery failed'. I don't know how with the same credentials you can send mail but you can't receive! I mean with the same server and same credentials you the mailing list works fine but the contact us form gives an error. Is there a way to fix that??
Thanks in advance!
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$name = $_REQUEST['name'];
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$mail->IsSMTP();
$mail->Host="smtp";
$mail->Username="some email address was given to me";
$mail->Password="password";
$mail->SMTPAuth=true;
$mail->SMTPSecure = 'tls';
$mail->From =$email;
$mail->FromName =$name;
$mail->AddAddress("some email address was given to me", "Some name");
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

Related

Which PHPMailer prop controls the sender email in the email header?

I'm getting an error from jQuery with a 404 to my mail.php (I checked the path and it seems correct)
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require("../includes/libraries/phpmailer/PHPMailer-master/src/Exception.php");
require("../includes/libraries/phpmailer/PHPMailer-master/src/PHPMailer.php");
require("../includes/libraries/phpmailer/PHPMailer-master/src/SMTP.php");
$server_email = 'myemail#outlook.com';
$pass = '123456';
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp-mail.outlook.com';
$mail->Port = 587;
$mail->Username = $server_email;
$mail->Password = $pass;
$mail->FromName = $full_name;
$mail->From = $email;
$mail->setFrom($server_email, $full_name );
$mail->addAddress($server_email);
$mail->addReplyTo($email); //email that it will be replied to
$mail->Subject = 'Subscription submitted by ' . $full_name . '.';
$mail->Body = "
Name: $full_name<br><br>
Email: $email<br>
";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
Right now, my email (the outlook one) comes up in the header of the email next to the Sender's Name. The addReplyTo actually does give me the gmail address to reply to, so that's great. But having my email show on the sender header is confusing. See the image below
Thanks in advance

PHP SMTP Custom contact form

I have this form, that send fine locally, but when i upload it to hostgator i get the following error message
Mailer Error: Could not instantiate mail function.
my php code is
<?php
if(isset($_POST['submit'])){
require 'PHPMailer/PHPMailerAutoload.php';
// Send mail
$mail = new PHPMailer();
// Data received from POST request
$name = stripcslashes($_POST['tbName']);
$emailAddr = stripcslashes($_POST['tbEmail']);
$company = stripcslashes($_POST['tbCompany']);
$comment = stripcslashes($_POST['taMessage']);
$subject = stripcslashes($_POST['tbSubject']);
// SMTP Configuration
$mail->SMTPAuth = true;
$mail->Host = "gator3209.hostgator.com"; // SMTP server
$mail->Username = "****#*****.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->AddAddress('****#*****.com');
$mail->From = "****#*****.com";
$mail->FromName = "Website Contact Form - " . $name;
$mail->Subject = $subject;
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("Name:" . $name . "<br /><br />Email:" . $emailAddr. "<br /><br />Company:" . $company. "<br /><br />Subject:" . $subject. "<br /><br />" . $comment);
$message = NULL;
if(!$mail->Send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Message sent!";
}
}
?>
I have spoken to my host and they said its not within there support to deal with these things, but said my port and host are correct.
So now I'm rather confused. is there anything obvious I'm missing?
Just to let you know if anyone finds this, my problem was i was missing $mail->IsSMTP(); from my config.
the SMTP config section should be as follows
<?php
if(isset($_POST['submit'])){
require 'PHPMailer/PHPMailerAutoload.php';
// Send mail
$mail = new PHPMailer();
// Data received from POST request
$name = stripcslashes($_POST['tbName']);
$emailAddr = stripcslashes($_POST['tbEmail']);
$company = stripcslashes($_POST['tbCompany']);
$comment = stripcslashes($_POST['taMessage']);
$subject = stripcslashes($_POST['tbSubject']);
// SMTP Configuration
$mail->SMTPAuth = true;
$mail->IsSMTP();
$mail->Host = "gator3209.hostgator.com"; // SMTP server
$mail->Username = "****#*****.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->AddAddress('****#*****.com');
$mail->From = "****#*****.com";
$mail->FromName = "Website Contact Form - " . $name;
$mail->Subject = $subject;
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("Name:" . $name . "<br /><br />Email:" . $emailAddr. "<br /><br />Company:" . $company. "<br /><br />Subject:" . $subject. "<br /><br />" . $comment);
$message = NULL;
if(!$mail->Send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Message sent!";
}
}
?>

Cant set sender name phpmailer

So I'm setting up a script to send emails from a contact us form. Everything is working great besides the fact that I can't set sender's name properly. I get an error:
Warning: Creating default object from empty value
My code looks like this:
<?php $eemail = $_REQUEST['email'] ;
$message = $_REQUEST['message']; $subject = $_REQUEST['subject']; $fname =
$_REQUEST['name'];
require("class.phpmailer.php"); $mail = new PHPMailer();
$mail->Username = "email"; // SMTP username
$mail->Password = "password"; // SMTP password $mail->IsSMTP();
$mail->SMTPAuth = true; $mail->Host = 'aspmx.l.google.com';
$mail->Port = 25;
$mail->From = $eemail; $name->FromName = $fname; $mail->Subject =
$subject;
$mail->AddAddress("email", "name");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Body = $message; $mail->AltBody = $message;
if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo
"Mailer Error: " . $mail->ErrorInfo; exit; }
echo "Message has been sent"; ?>
add this code
$mail->SetFrom("$eemail ", "$fname");

Two versions of same email to two different recipients using class.phpmailer.php

I'm trying to send two versions of the same email to two recipients using phpMailer
Depending on the email I need to send one version or another.
At the moment I'm able to send only the same version to both email address
<?php
require_once('class.phpmailer.php');
if(isset($_POST['Submit'])) {
$fname = $_POST['fname'];
$maileremail = $_POST['maileremail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer();
$text = 'The person that contacted you is '.$fname.' E-mail: '.$maileremail.' Subject: '.$subject.' Message: '.$message.' :';
$body = eregi_replace("[\]",'',$text);
$text2 = 'The person that contacted you is '.$fname.' E-mail: REMOVED - To get email address you must login Subject: '.$subject.' Message: '.$message.' :';
$body2 = eregi_replace("[\]",'',$text2);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxxxx.xxxxx.xxx"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$mail->Host = "xxxxx.xxxxx.xxx"; // sets the SMTP server
$mail->Port = XXXXXXXX; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxx#xxxxx.xxx"; // SMTP account username
$mail->Password = "XXXXXXXX"; // SMTP account password
$mail->SetFrom('xxxxx#xxxxx.xxx', 'XXXXXXXX');
$mail->Subject = $subject;
$add = array("first#email.xxx", "second#email.xxx");
foreach($add as $address) {
if($address == "first#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body;
}
elseif($address == "second#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body2;
}
}
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}}?>
If you're looking to do all the setup once for minimal repetition, simply clone your $mail object in your foreach($add) loop:
foreach ( $add as $address ) {
$current = clone $mail;
if ( $address == 'first#mail.com' ) {
$current->AddAddress($address, "xxxxxxxxx");
$current->Body = $body;
$current->send();
} elseif....
This creates a separate clone of your $mail object for every loop, allowing you to add different email bodies, subjects, etc. without having to rewrite all connection settings.
I think you want to seach the $maileremail inside $add and send the desired email.
$add = array("first#email.xxx", "second#email.xxx");
$mail->AddAddress($maileremail, "xxxxxxxxx");
if($maileremail === $add[0])
$mail->Body = $body;
if($maileremail === $add[1])
$mail->Body = $body2;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}
EDITED:
I misunderstood the question. Brian is right.

Not Able to Send Mail Using PHP?

I am trying to send a mail through core Php, I have created a file email.php and included PHPMailer_v5.1 in it.. here is my php code,
<?php if($_POST){
$email = $_REQUEST['email'] ;
$comments = $_REQUEST['comments'] ;
$phone = $_POST['telephone'] ;
$name = $_REQUEST['name'] ;
$mail = new PHPMailer();
$mail->Host = "localhost";
$mail->From = "scorpion.schizo#gmail.com";
$mail->FromName = "Showket";
$mail->AddAddress("manusurya9139#gmail.com");
$mail->Subject = "Feedback form results";
$mail->Body="
Name: $name
Email: $email
Telephone: $telephone
Comments: $comments";
$mail->WordWrap = 50;
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo 'Thank you for your feedback.';
}
}
?>
when i post this form it gives me the right message Thank you for your feedback. but i didnt recieve the message..do i need to configure anything else ?
Try setting Gmail as your SMTP
<?php if($_POST){
$email = $_REQUEST['email'] ;
$comments = $_REQUEST['comments'] ;
$phone = $_POST['telephone'] ;
$name = $_REQUEST['name'] ;
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "ssl://smtp.gmail.com:465"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "scorpion.schizo#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->Host = "localhost";
$mail->From = "scorpion.schizo#gmail.com";
$mail->FromName = "Showket";
$mail->AddAddress("manusurya9139#gmail.com");
$mail->Subject = "Feedback form results";
$mail->Body="
Name: $name
Email: $email
Telephone: $telephone
Comments: $comments";
$mail->WordWrap = 50;
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo 'Thank you for your feedback.';
}
}
?>
Run "sendmail -bp" to check if there is anything in the queue. Also, check the email folder for the user that your webserver is running under to see if you have had any undeliverable notifications.

Categories