PHPmailer how to send to 2 emails from html form inputs when one is blank - phpmailer

I am sending email from a form with 2 email address inputs where either or both may be used or not. When I leave the second one blank, though, I get an error message. I assume it is because I am not validating the second one. But if I add the code to validate the second one then no email is sent because it gets aborted. I think I am not understanding the concept of how to do this.
Fatal error: Uncaught exception 'phpmailerException' with message 'Invalid address: (addAnAddress cc): ' in /var/www/html/PHPMailer/class.phpmailer.php:869 Stack trace: #0 /var/www/html/PHPMailer/class.phpmailer.php(821): PHPMailer->addOrEnqueueAnAddress('cc', '', '') #1 /var/www/html/innoedit.php(71): PHPMailer->addCC('') #2 {main} thrown in /var/www/html/PHPMailer/class.phpmailer.php on line 869
$mail = new PHPMailer(true);
$mail->CharSet = 'utf-8';
ini_set('default_charset', 'UTF-8');
class phpmailerAppException extends phpmailerException {}
try {
$to = $responsiblepost;
if(!PHPMailer::validateAddress($to)) {
throw new phpmailerAppException("Email address " . $to . " is invalid -- aborting!");
}
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = "smtp.example.com";
$mail->Port = "25";
$mail->SMTPSecure = "none";
$mail->SMTPAuth = true;
$mail->Username = "noreply#example.com";
$mail->Password = "password";
$mail->addReplyTo("andrew#example.com");
$mail->setFrom("noreply#example.com");
$mail->addAddress($to); //from first <input>
$mail->addAddress($informedpost); //from second <input>
$mail->Subject = "subject";
$body = "<html>...

Related

PhpMailer sends the registration form to the e-mail 3 times

The same registration form information comes to my mail more than once.
My Mailbox is filled with exactly the same forms.
Can you help me solve this problem?
PHP CODE:
 <?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer(); // create a new object
$mail = new PHPMailer(); // create a new object
$subject = "[XX] Registration Form";
$question = nl2br(stripcslashes($_POST['question']));
$question = trim($question);
$email_message = "<b>Name:</b> ".stripcslashes($_POST['fname'])."<br><br>";
$email_message .= "<b>Surname:</b> ".stripcslashes($_POST['lname'])."<br><br>";
$email_message .= "<b>Birthday:</b> ".stripcslashes($_POST['dob'])."<br><br>";
$email_message .= "<b>Phone:</b> ".stripcslashes($_POST['phone'])."<br><br>";
$email_message .= "<b>Email:</b> ".stripcslashes($_POST['email'])."<br><br>";
$email_message .= "<b>Address:</b> ".stripcslashes($_POST['adres'])."<br><br>";
$email_message .= "<b>Location:</b> ".stripcslashes($_POST['Location'])."<br><br>";
$body = $email_message;
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->Host = "smtp.yandex.com.tr";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465; // or 465
$mail->IsHTML(true);
$mail->CharSet ="utf-8";
$mail->SetFrom("info#xx.com", "XX REGISTRATION FORM"); // Mail adresi
$mail->Username = "info#xx.com"; // Mail adresi
$mail->Password = "xxx"; // Parola
$mail->Subject = $subject;
$mail->body = $body;
$mail->MsgHTML($body);
$mail->AddAddress("info#xx.com");
$mail->addReplyTo(stripcslashes($_POST['emailer']), "");
if(!$mail->Send()){
echo "Mail Error".$mail->ErrorInfo;} else {
echo "Mesaj Gönderildi";
}
It's most likely that the script is simply being run more than once. This can be caused by browser plugins, or by not preventing processing of empty forms. Protect against sending empty messages during page load by wrapping it in a condition like this:
if (isset($_POST['emailer'])) {
//Rest of your script
The main thing to check first is whether a single run of your script is sending multiple messages, or whether the script is being run more than once (sending one message each time). Try writing something to a log file, or making it obvious in received messages by appending a random number to the end of the subject line. If you receive two messages with the same number in the subject, they are true duplicates; if the numbers are different subjects, your script is being run more than once:
$mail->Subject = 'Hi ' . rand();
Some other problems:
$mail->body = $body;
PHP is case-sensitive for properties, so that should be $mail->Body = $body;, but you're also calling $mail->MsgHTML($body);, which sets the Body property for you, so you don't need to set body yourself as well.
While you're working on a script, $mail->SMTPDebug = 1 isn't very useful as it doesn't show what the server is saying; set $mail->SMTPDebug = 2. Don't forget to set it to 0 in production!
I can also see that you're using a very old version of PHPMailer, and have based your code on an obsolete example, so update.
The problem was solved when I edited my codes like this.
The code executed when the form is submitted includes the following:
<script type="text/javascript">
function checkForm(form) // Submit button clicked
{
//
// check form input values
//
form.myButton.disabled = true;
form.myButton.value = "Please wait...";
return true;
}
</script>
While the HTML for the form itself looks something like:
<form method="POST" action="..." onsubmit="return checkForm(this);">
...
<input type="submit" name="myButton" value="Submit">
</form>

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'bobbygotecha07#.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'bobbygotecha07#example.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->SMTPSecure = 'ssl';
$mail->setFrom('bobbygotecha07#gmail.com', 'bobby gotecha');
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('bobbygotecha07#gmail.com'); // Name is optional
$mail->addReplyTo('bobbygotecha07#gmail.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$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';
}
?>
This is the code that's been written by me for sending the mail but its not working for me and i have also tried varoius thing thats been mentioned in stackoverflow but its not woking and i have also updated the dll file but still not able to send so if anybody could help me.

phpmailer No Relay Access Allowed Error

My website is hosted on godaddy, and I just figured out that my website is no more sending emails through phpmailer since last few months. I uploaded the latest version of phpmailer but still no success. The online web mail of my website runs fine. If I use php's "mail" function, it does send emails to gmail, but not to yahoo accounts.
I tried all the three ports 25, 465, and 587, but no luck
I am getting the following error from phpmailer:
SERVER -> CLIENT: 554 p3plsmtpa07-10.prod.phx3.secureserver.net ESMTP No Relay Access Allowed From 50.63.196.51
CLIENT -> SERVER: EHLO lostandfound.pakproject.com
SERVER -> CLIENT:
SMTP ERROR: EHLO command failed:
SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Following is my code that I am trying to test. (User name, passwords, emails are changed...)
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "smtpout.... my_server";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "here_i_used_my_website_email";
$mail->Password = "here_password";
$mail->setFrom('website_email', 'From name');
$mail->addReplyTo('website_email', 'From name');
$mail->addAddress('another_email', 'name_used_here');
$mail->Subject = 'About the task';
$mail->Body = 'This is email body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
It worked for me.
Keep in mind
https://co.godaddy.com/help/mail-server-addresses-and-ports-for-business-email-24071**
I was able to resolve my issue with the following code/settings of phpmailer
<?php
$recipient = "abc#def.com"
$subject = "Subject here";
$emailBody = "This is body";
// PHP MAILER CODE STARTS FROM HERE //
require '../phpmailermaster/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtpout.secureserver.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx#yyyy.com'; // SMTP username
$mail->Password = '3344123'; // SMTP password
//$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 465;
$mail->Port = 80; // TCP port to connect to [THIS PORT ALLOWING EMAILS]
$mail->setFrom('xxx#yyyy.com', 'hello');
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress($recipient); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addBCC('bcc#example.com');
//$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 = $subject;
$mail->Body = $emailBody;
$mail->AltBody = $emailBody;
if(!$mail->send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent';
}
// PHP MAILER CODE ENDS HERE ==
?>

Error with PHPMailer SMTP connect() failed

I'm using PHPMailer from https://github.com/PHPMailer/PHPMailer and using code below:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->port = '25';
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx#gmail.com'; // SMTP username
$mail->Password = 'xxx'; // SMTP password
$mail->From = 'xxx#gmail.com';
$mail->FromName = 'xxx';
$mail->addAddress('xxx#xxx.com', 'xxx'); // Add a recipient
$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;
} else {
echo 'Message has been sent';
}
When I run this, it gives an error:
Message could not be sent.Mailer Error: SMTP connect() failed.
but when I run it on localhost (windows and xampp) it's successful!

PHPMailer sometimes send mails sometimes not. I use gmail smtp

I use PHPMailer class to send letter and I use gmail account as smtp server.
problem is what sometimes it work well and visitors get letters, but sometimes no and show me error "Message body is empty" or like that.
I think if be problem in code, so don't sent any letter for websites visitors.
why happien like this? what can be problem?
Change these lines:
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Port = 465;
With:
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Port = 587;
and hopefully all will be okay from now on.
my code is
function mail_send($type, $id, $mailadd, $maillname) {
global $lang;
global $site_adress;
global $pavadinimas;
global $tekstas;
global $ivadas;
global $full_date;
global $short_date;
require_once ('includes/phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$siteuser_name = SQL_ROW("users", "WHERE user_id='1'", "user_name");
$siteadminemail = SQL_ROW("users", "WHERE user_id='1'", "user_email");
$alttext = users_langs(altmailtext);
if ($type == "mailactiveletter") {
$subj = users_langs("mailactivesubject");
$linktur = "$site_adress/$lang/usermail_$type-$id.html#kat";
// var_dump($linktur,$type, $siteuser_name,$siteadminemail);
}
if ($type == "mailchangeadress") {
$subj = users_langs("prisijduomenupriminimas");
$linktur = "$site_adress/$lang/usermail_$type-$id.html#kat";
}
$body = file_get_contents("$linktur");
$body = eregi_replace("[\]", '', $body);
define('GUSER', 'uxxxx#gmail.com'); // Gmail username
define('GPWD', 'xxx'); // Gmail password
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->CharSet = 'UTF-8';
//$mail->AddReplyTo("$siteadminemail", "$siteuser_name");
$mail->AddReplyTo("no-replay#xxx.co.uk", "$siteuser_name");
$mail->AddAddress("$mailadd", "$maillname");
// $mail->SetFrom("$siteadminemail", "$siteuser_name");
$mail->SetFrom("no-replay#xxx.co.uk", "$siteuser_name");
// $mail->AddReplyTo("$mailadd", "$maillname");
$mail->AddReplyTo("no-replay#xxx.co.uk", "$siteuser_name");
$mail->Subject = "$subj";
$mail->AltBody = "$alttext $linktur";
// optional, comment out and test
$mail->MsgHTML($body);
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "";
}
}
Add a $mail->Sender property.
For exemple:
$mail->Sender is the same value of a $mail->From and I add the two lines.
$mail->From = "noreply#test.com";
$mail->Sender = "noreply#test.com";

Resources