PHPMailer wont send to email with Hyphen IE-#blah-blah.com - php

I have a simple contact form that uses PHPMailer to send it's information to the admin. Everything works fine when the results are sent to an e-mail with out a hypen such as blah#blah.com. As soon as I change the address to an e-mail with a hypen like blah#blah-blah.com, I get this error.
"Could not instantiate mail function. Mailer Error: Could not instantiate mail function."
Is this a bug with PHPMailer?
Here's my code
<?php
require_once('class.phpmailer.php');
$question_for = $_POST['question_for'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company = $_POST['company'];
$comment = $_POST['comment'];
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = eregi_replace("[\]",'',$body);
$mail->AddReplyTo("donotreply#blah-blah.com","DoNotReply");
$mail->SetFrom('donotreply#blah-blah.com', 'DoNotReply');
switch ($question_for) {
case "Sales":
$address = "Sales#blah-blah.com";
$mail->AddAddress($address, "Blah");
$mail->Subject = "Message from Sales";
break;
case "Service":
$address = "service#blah-blah.com";
$mail->AddAddress($address, "Blah");
$mail->Subject = "Message from Service";
break;
case "Career":
$address = "career#blah-blah.com";
$mail->AddAddress($address, "Blah");
$mail->Subject = "Message from Career";
break;
}
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("
Question For:".$question_for."<br />
Name:".$name."<br />
Phone:".$phone."<br />
Email:".$email."<br />
Company:".$company."<br />
Comment:".$comment."<br />");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header('Location: http://blah.com/contact/?sent');
}
?>
Checking even more, I see that this works
<?php
mail("test#blah.com", "Test Email", "Testing"); ?>
but this does not
<?php
mail("test#blah-blah.com", "Test Email", "Testing"); ?>

Related

PHPmailer not sending mail when message field is empty but is not required?

http://nodeiterator.pl/
Why my php mailer script is not sending mail when message field is empty but is not required ? I get the message "please try again later". What am I missing ?
This is my script:
$msg = "";
use PHPMailer\PHPMailer\PHPMailer;
include_once "phpmailer/src/PHPMailer.php";
include_once "phpmailer/src/Exception.php";
if (isset($_POST['submit'])) {
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != "") {
$file = "attachment/" . basename($_FILES['attachment']['name']);
move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
} else
$file = "";
$mail = new PHPMailer();
$mail->addAddress('piterdeja#gmail.com');
$mail->setFrom($email);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = $message;
$mail->addAttachment($file);
if ($mail->send())
$msg = "Your email has been sent, thank you!";
else
$msg = "Please try again!";
}
I don't think PHPMail by default will let you send an email with an empty body, but you can just go:
$mail->AllowEmpty = true;
Check the error returned:
if(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Normally phpmailer does not accept an empty body, you must force it using the attribute $mailer->AllowEmpty = true;

How to add form validation to phpmailer?

I am a newbie to PHP, and I am currently using phpmailer to let my clients send emails to me, but a bot is apparently taking advantage of it and sends 3 emails at exactly the same time everyday. So I've been trying to add validation to the form using PHP as javascript can be stopped to avoid validation.. Could you guys please help me add validation to this? I just need to validate name and email. Thank you so much in advance.
PS. I removed some parts of the code for privacy.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$proptypes = $_POST['proptypes'];
$units = $_POST['units'];
$purchaseOrRefi = $_POST['purchase'];
$loans = $_POST['loans'];
$income = $_POST['income'];
$apt = $_POST['apartment'];
$message = $_POST['message'];
$body = "<b>[From]</b><br>$name<br><br> <b>[E-Mail]</b><br>$email<br><br> <b>[Phone #]</b><br>$phone<br><br> <b>[Address]</b><br>$address<br><br> <b>[Type of Property]</b><br>$proptypes<br><br> <b>[# of Units]</b><br>$units<br><br> <b>[Purchase or Refi?]</b><br>$purchaseOrRefi<br><br> <b>[Amnt of Loans Requested]</b><br>$loans<br><br> <b>[Total Income]</b><br>$income<br><br> <b>[Total Apt Expense]</b><br>$apt<br><br> <b>[Message]</b><br>$message<br><br>";
$mail->Subject = 'Someone wants to hear more about your mortgage programs!';
$mail->Body = $body;
$mail->From = '';
$mail->FromName = '';
// Add a recipient
$mail->addAddress('');
$mail->addAddress('');
$mail->addAddress('');
$mail->addAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name'], 'base64', $_FILES['attachment']['type']); // Add attachments
$mail->isHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent. Please try again.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Your message has been sent! We will get back to you soon!';
}
?>

PHP mailer Error receiving an email

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;
}

How to do email form with multiple recipients and different body?

i have one contact form, when user submit all value will send(email) to admin.But now i want to do when user submit admin will receive the email and user also will receive an email but with different body.
here my previous code :
<?php
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
$name= $_POST["name"];
$email= $_POST["email"];
$phone= $_POST["phone"];
$company= $_POST["company"];
$message= $_POST["message"];
require_once('lib/class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->AddReplyTo("admin#gmail.com","I Concept");
$mail->SetFrom('admin#gmail.com', 'I Concept');
$mail->AddReplyTo("admin#gmail.com","I Concept");
$address = "admin#gmail.com";
$mail->AddAddress($address, "I Concept");
$mail->Subject = "MY - Request a Quote";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br>
Name : $name<br>
Email : $email<br>
Phone : $phone<br>
Company : $company<br>
Enquiry : $message<br> <br>
Thank You!<br>
";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
}
?>
Try the following. Didn't test but you basically need to get another PHPMailer object going and set the body and to information separately.
$address = "admin#gmail.com";
$mail->Subject = "MY - Request a Quote";
// keeps the current $mail settings and creates new object
$mail2 = clone $mail;
// mail to admin
$mail->AddAddress($address, "I Concept");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br>
Name : $name<br>
Email : $email<br>
Phone : $phone<br>
Company : $company<br>
Enquiry : $message<br> <br>
Thank You!<br>";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
// now send to user.
$mail2->AddAddress($email, $name);
$mail2->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail2->Body = "Separate email body for user filling form out.";
if(!$mail2->Send()) {
echo "Mailer Error: " . $mail2->ErrorInfo;
} else {
echo "Message sent!<br>";
}
Cloning the PHPmailer object is not necessary. Just use the ClearAllRecipients method that is built into PHPmailer before changing the body and sending the second email.
I'm sure you can't send different bodies in one SMTP call. However you can just send the first email and initiate a new PHPMailer.

PHPMailer doesn`t work with AddAddress property set

I'm trying to create a contact form using phpMailer and I get in firebug this:
NetworkError: 500 Internal Server Error - path/process.php
uncaught exception: [object Object]
each time I'm trying to run the code below.
Please note that the error is not shown anymore if I remove $mail->AddAddress line, that's why I suspect this line to be the cause.
Instead a new error is displayed: You must provide at least one recipient email address when I remove it.
<?php
$name = $_POST['firstName'];
$email = $_POST['email'];
require("phpmailer.inc.php");
$mail = new PHPMailer(true);
$mail->IsMail();
$mail->From = $email;
$mail->Subject = "subject ";
$mail->Body = "From $name with email: $email";
$mail->WordWrap = 50;
$mail->AddAddress('my_address#gmail.com','my name');
$mail->Send();
?>
Thanks!
Rewrite it to:
<?php
$name = $_POST['firstName'];
$email = $_POST['email'];
require 'phpmailer.inc.php';
try {
$mail = new PHPMailer(true);
$mail->IsMail();
$mail->From = $email;
$mail->Subject = "subject ";
$mail->Body = "From $name with email: $email";
$mail->WordWrap = 50;
$mail->AddAddress('my_address#gmail.com','my name');
$mail->Send();
} catch (Exception $e) {
echo $e->getMessage();
}
and you will catch the error

Categories