phpmailerException with message 'Invalid address' - php

I am new to PHPMailer. It is working well on my localhost but not on live.
I get this error:
Fatal error: Uncaught exception 'phpmailerException' with message 'Invalid address: ' in /directoryhere/test/public_html/scripts/phpmailer/class.phpmailer.php:498 Stack trace: #0 /directoryhere/test/public_html/scripts/phpmailer/class.phpmailer.php(438): PHPMailer->AddAnAddress('to', '', '') #1 /directoryhere/test/public_html/scripts/include.php(189): PHPMailer->AddAddress('', '') #2 /directoryhere/test/public_html/forgot.php(49): send_mail() #3 /directoryhere/test/public_html/index.php(141): include('/home1/test...') #4 {main} thrown in /directoryhere/test/public_html/scripts/phpmailer/class.phpmailer.php on line 498
This is the line of code
/directoryhere/test/public_html/scripts/phpmailer/class.phpmailer.php:498:
if (!self::ValidateAddress($address)) {
$this->SetError($this->Lang('invalid_address').': '. $address);
if ($this->exceptions) {
throw new phpmailerException($this->Lang('invalid_address').': '.$address);
}
if ($this->SMTPDebug) {
echo $this->Lang('invalid_address').': '.$address;
}
return false;
}
/directoryhere/test/public_html/scripts/phpmailer/class.phpmailer.php(438):
public function AddAddress($address, $name = '') {
return $this->AddAnAddress('to', $address, $name);
}
Could anyone help me what may be the possible cause of this?

#Synchro #m02ph3u5 Thank you for pointing that out! It is working well now. The problem is in the submit code . The email value is empty and yet the submit button is firing when the page loads. So what I did is to load first the form, catch if email input was empty, if not empty then the submit button was enable and ready to fire.
I just added this code of line here before the submission and calling the mail function:
if(isset($_POST["retrieve"])) { //name of button
//submission and calling mail function here
}

Related

"An error occurred while handling another error: yii\web\HeadersAlreadySentException"

I am trying to submit a comment on a guestbook application based on the Yii 2 Framework. On localhost on my PC works everything fine, but on the shared hosting when I want to submit a comment in View, I get this error.
Here is the error:
An error occurred while handling another error:
exception 'yii\web\HeadersAlreadySentException' with message 'Headers already sent in /home/mahdikas/public_html/guestbook/controllers/PostController.php on line 117.' in /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send()
#2 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
Previous exception:
exception 'yii\web\HeadersAlreadySentException' with message 'Headers already sent in /home/mahdikas/public_html/guestbook/controllers/PostController.php on line 117.' in /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response->send()
#2 /home/mahdikas/public_html/guestbook/web/index.php(12): yii\base\Application->run()
#3 {main}
In the postController I have this code:
public function actionAdd_comment()
{
//print_r($_POST);
$model = new \app\models\Comments;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->comment_date = date('Y-m-d H:i:s');
if ($model->save()) {
echo 'Thanks for your comment.';
} else {
echo 'Failed!';
}
}
}
which line 117 in the error is:
echo 'Thanks for your comment.';
How can I solve this problem?
Since Yii 2.0.14 you cannot echo in a controller. A response must be returned:
public function actionAdd_comment() {
$model = new \app\models\Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->comment_date = date('Y-m-d H:i:s');
if ($model->save()) {
return 'Thanks for your comment.';
} else {
return 'Failed!';
}
}
}
You may also call exit at the end of your method to prevent further processing or wrap your code with ob_start() and ob_get_clean(), if you're not able to avoid echo.
public function actionAdd_comment() {
$model = new \app\models\Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$this->someMagicWithEcho();
exit;
}
}
or
public function actionAdd_comment() {
$model = new \app\models\Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
ob_start();
$this->someMagicWithEcho();
return ob_get_clean();
}
}
Although I accept rob006's solution as correct, I have encountered a situation where there was no echo in the controller, but I also got the error. After going through several sites looking for a solution, I discovered an alternative.
You can check the php.ini file and ensure the output buffer is enabled. If not, you can enable it by adding this line in php.ini if it does not exist:
output_buffering = on
And turn it off for just the script - the script where it is not required by either...
calling ob_end_flush(), or
calling ob_end_clean()

I suddenly get a fatal error. I don't understand it

I have been working at a code that used to work. However, suddenly, this message pops up.
Fatal error: Uncaught exception 'Exception' with message 'Email is incorrect!' in C:\xampp\htdocs\Prototype\classes\User.php:23
Stack trace: #0 C:\xampp\htdocs\Prototype\index.php(14): User->setEmail(true) #1 {main} thrown in C:\xampp\htdocs\Prototype\classes\User.php on line 23
But I just don't understand what this means. I tried using try and catch, but it keeps popping up.
This is the code where the error occurs
public function setEmail($p_email)
{
if (empty($p_email)) {
throw new Exception('Email kan niet leeg zijn!');
}
$this->email = $p_email;
if (!filter_var($p_email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Email is incorrect!'); //here is the error (line 23)
}
}
this is the code where it is summoned
$user = new User();
$user->setEmail($_SESSION['login']); //line 14
$currentUser = $user->getProfile();
$userEmail = $user->getEmail();
$userName = $user->getUserName();
$userID = $currentUser['userID'];
Your error is that you are passing a boolean instead of a valid string email...
Stack trace: #0 C:\xampp\htdocs\Prototype\index.php(14): User->setEmail(true) #1 {main} thrown in C:\xampp\htdocs\Prototype\classes\User.php on line 23
So, this line is incorrect:
$user->setEmail($_SESSION['login']); //line 14

Parse.com php exception shows all errors

When i try to run php code using Parse.com PHP SDK it
the error is "Account already exists for this username"
but it throw all this error message:
Fatal error: Uncaught exception 'Parse\ParseException' with message 'Account already exists for this username' in /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseClient.php:357 Stack trace: #0 /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseObject.php(1038): Parse\ParseClient::_request('POST', 'classes/_User', NULL, '{"username":"my...', false) #1 /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseObject.php(947): Parse\ParseObject::deepSave(Object(Parse\ParseUser), false) #2 /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseUser.php(108): Parse\ParseObject->save() #3 /Users/yousef/Desktop/project/test.php(20): Parse\ParseUser->signUp() #4 {main} thrown in /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseClient.php on line 357
The Code i Use
<?php
require 'vendor/autoload.php';
use Parse\ParseClient;
ParseClient::initialize('YousefId', '', 'YousefMaster');
ParseClient::setServerURL('server-ip:1337/parse');
use Parse\ParseUser;
$user = new ParseUser();
$user->set("username", "my name");
$user->set("password", "my pass");
$user->set("email", "email#example.com");
$user->set("phone", "415-392-0202");
try {
$user->signUp();
} catch (ParseException $error) {
echo $error->getCode();
echo $error->getMessage();
}
?>
so how do i just show the error code and message instead of showing all this error.
You need to refer to the ParseException within the Parse namespace.
Try
catch (Parse\ParseException $error) {
// ...
}

'Invalid header value detected' when trying to fetch email with Zend_Mail_Storage_Imap

I am trying to receive emails with Zend_Mail_Storage_Imap but keep getting 'Invalid header value detected' on some e mails.
I have located the header which is causing the crash to the following:
Content-Type: application/vnd.oasis.opendocument.text;
name="=?UTF-8?Q?cc-demo-minutes-2014-09-09_-_=c3=a5_detta_=c3=a4r_en_kopia?=
=?UTF-8?Q?_av_det_vanliga_mellanslagsbefriade=2c_eller_mellanslagsl?=
=?UTF-8?Q?=c3=b6sa=2c_namnet_som_b=c3=b6r_bli_en_l=c3=a5ng_harang_p?=
=?UTF-8?Q?=c3=a5_flera_MIME-rader.odt?="
Although it's a ugly file name it should be a valid header as far as i understand. I've tried a couple of email validators and they all agree this is a valid header. Why does Zend throw this exception and how can I prevent it?
The code that causes the crash
$oMail = new Zend_Mail_Storage_Imap($aImapSettings);
foreach ($oMail as $iMessageNum => $oMessage) {
if ($oMessage->isMultipart()) {
// Multipart messages
foreach (new RecursiveIteratorIterator($oMessage) as $oPart) { // <--crash
}
}
}
Stack trace
[07-Apr-2016 10:40:12 Europe/Stockholm] PHP Fatal error: Uncaught exception 'Zend_Mail_Exception' with message 'Invalid header value detected' in /usr/local/zendsvr6/var/libraries/Zend_Framework_1/1.12.17/library/Zend/Mail/Header/HeaderValue.php:133
Stack trace:
#0 /usr/local/zendsvr6/var/libraries/Zend_Framework_1/1.12.17/library/Zend/Mail/Part.php(600): Zend_Mail_Header_HeaderValue::assertValid('application/vnd...')
#1 /usr/local/zendsvr6/var/libraries/Zend_Framework_1/1.12.17/library/Zend/Mail/Part.php(151): Zend_Mail_Part->_validateHeaders(Array)
#2 /usr/local/zendsvr6/var/libraries/Zend_Framework_1/1.12.17/library/Zend/Mail/Part.php(289): Zend_Mail_Part->__construct(Array)
#3 /usr/local/zendsvr6/var/libraries/Zend_Framework_1/1.12.17/library/Zend/Mail/Part.php(353): Zend_Mail_Part->_cacheContent()
#4 /usr/local/zendsvr6/var/libraries/Zend_Framework_1/1.12.17/library/Zend/Mail/Part.php(578): Zend_Mail_Part->countParts()
#5 /www/zendsvr6/website/application/app/exterior/email/AppMailReceiver.php(509): Zend_Mail_Part->rewind()
#6 /www/zendsvr6/website/applic in /usr/local/zendsvr6/var/libraries/Zend_Framework_1/1.12.17/library/Zend/Mail/Header/HeaderValue.php on line 133
I also found this post suggesting that changing internal_encoding would help. It did not solve the problem in my case.
Not sure if this solution will fix your issue but I believe so:
https://github.com/zendframework/zf1/pull/567
go to your library/Zend/Http/Client.php file and apply following fix as mentioned in link above.
## -1592,6 +1592,11 ## protected function _validateHeaderValue($value, $recurse = true)
return;
}
+ // Cast integers and floats to strings for purposes of header representation.
+ if (is_int($value) || is_float($value)) {
+ $value = (string) $value;
+ }
+
if (! is_string($value) && (! is_object($value) || ! method_exists($value, '__toString'))) {

Unable to delete phone numbers in Twilio REST API in PHP

Hi I keep geting the following error when I try to delete a number from my twilio subaccount using REST API in PHP
my code is;
$number = $twClient->account->incoming_phone_numbers->get($number_Sid);
$twClient->account->incoming_phone_numbers->delete($number->sid);
The error that I am getting is;
[22-Aug-2013 09:40:17 UTC] PHP Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The requested resource was not found' in C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php- 732e6f6\Services\Twilio.php:226
Stack trace:
#0 C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php- 732e6f6\Services\Twilio.php(145): Services_Twilio->_processResponse(Array)
#1 C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php-732e6f6\Services\Twilio.php(179): Services_Twilio->_makeIdempotentRequest(Array, '/2010-04-01/Acc...', 1)
#2 C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php-732e6f6\Services\Twilio\ListResource.php(71): Services_Twilio->deleteData('/2010-04-01/Acc...', Array)
#3 C:\Program Files (x86)\Zend\Apache2\htdocs\testers\web2call\application\controllers\clientphonenos_controller.php(518): Services_Twilio_ListResource->delete('PN397fc000ce6f8...')
#4 [internal function]: ClientPhoneNos_controller->data_form('delete', '+14139926551_AC...')
#5 C:\Program Files (x86)\Zend\Apache2\htdocs\system\core\Cod in C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php-732e6f6\Services\Twilio.php on line 226
If you have already successfully deleted the phone number, Twilio will return a 404 Not Found to you, which the PHP library will interpret as a RestException. You can only delete the phone number one time :)
You can override exception by using this syntax:
try
{
// do something that can go wrong
}
catch (Exception $e)
{
throw new Exception( 'Something really gone wrong', 0, $e);
}
Here's another live sample for sending messages with an exception catcher:
<?PHP
require "Services/Twilio.php";
// Set our AccountSid and AuthToken from twilio.com/user/account
$AccountSid = "{ACCOUNTSID}";
$AuthToken = "{AUTHTOKEN}";
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
/* Your Twilio Number or Outgoing Caller ID */
$from = '2126404004';
$people = array("212-716-1130");
$body = "Enter your text message here";
$errorIds = array(); //user ids array which had broken phones
foreach ($people as $to) {
try
{
$client->account->sms_messages->create($from, $to, $body);
echo "Sent message to: $to \n <br>";
}
catch (Exception $e)
{ //on error push userId in to error array
$count++;
array_push($errorIds, $to);
}
}
print_r($errorIds);
?>
Without catching the exception, the script will die with an error like:
<br>PHP Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The message From/To pair violates a blacklist rule.' in /var/www/vhosts/httpdocs/twilio/Services/Twilio.php:149

Categories