I make API for send email using cakephp. This is my code:
App::uses('CakeEmail', 'Network/Email');
$this->autoLayout = false;
$this->autoRender = false;
$data = $this->request->data;
$title = $data['title'];
$content = $data['content'];
$Email = new CakeEmail('smtp');
$Email->from('myemail#gmail.com');
$Email->to($data['email'][0]);
$Email->subject($title);
$Email->send($content);
And it show error php_network_getaddresses: getaddrinfo failed: No address associated with hostname. Please help me in this case
The error message indicates that php cannot communicate with the host hostname - this comes from the configuration for that class:
class EmailConfig {
public $smtp = array(
'host' => 'hostname', // <---
...
);
}
Either it's badly configured, or the domain name does not resolve.
Related
I read all the other answers related to that question, but none of them help.
When I try to run the following setup either on my localhost or my production server, I get the following error message:
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
I installed CodeIgniter 4 and added the following to .env:
email.production.protocol = smtp
email.production.SMTPHost = my.server.com
email.production.SMTPUser = My#Mail.com
email.production.SMTPPass = MyPassword
email.production.SMTPCrypto = ssl
email.production.SMTPPort = 465
email.production.SMTPFromName = "Foo Bar"
For port 465 or 587 and crypto ssl or tsl I tried every possible option.
In the app/Config/Email.php the setting public $newline = "\r\n"; is already set (Suggestion coming from here.
I am successfully able to run
telnet my.server.com 465
telnet my.server.com 587
Then I added the following code to the end of app/Config/Email.php:
public function __construct()
{
$this->protocol = $_ENV['email.production.protocol'];
$this->SMTPHost = $_ENV['email.production.SMTPHost'];
$this->SMTPUser = $_ENV['email.production.SMTPUser'];
$this->SMTPPass = $_ENV['email.production.SMTPPass'];
$this->SMTPPort = $_ENV['email.production.SMTPPort'];
$this->SMTPCrypto = $_ENV['email.production.SMTPCrypto'];
$this->fromEmail = $_ENV['email.production.SMTPUser'];
$this->fromName = $_ENV['email.production.SMTPFromName'];
}
In my Controller I added a function with:
$email = \Config\Services::email();
$email->setSubject("Test");
$email->setMessage("Test");
$email->setTo("myaddress#example.com");
if ($email->send(false)) {
return $this->getResponse([
'message' => 'Email successfully send',
]);
} else {
return $this
->getResponse(
["error" => $email->printDebugger()],
ResponseInterface::HTTP_CONFLICT
);
}
Calling this function produces the error message described above.
I assume that this has nothing to do with the server configuration as the error message describes, because the is happening on localhost and production.
Update: This must have something to do with the CI setup. No matter what server I try, even with completely incorrect values (e.g. incorrect password) the error is exactly the same.
I usually use smtp gmail to send email for my client. the most important of 'send email by smtp gmail' is you must update your Gmail Security rules:
In your Gmail Account, click on Manage your Google Account
click tab Security
then, turn 'less secure app access' to 'ON'
After that, you set your 'app\config\EMail.php' like this:
public $protocol = 'smtp';
public $SMTPHost = 'smtp.gmail.com';
public $SMTPUser = 'your.googleaccount#gmail.com';
public $SMTPPass = 'yourpassword';
public $SMTPPort = 465;
public $SMTPCrypto = 'ssl';
public $mailType = 'html';
Last, you can create sendEmai function on controller like this:
$email = \Config\Services::email();
$email->setFrom('emailsender#gmail.com', 'Mr Sender');
$email->setTo('emailreceiver#gmail.com');
$email->setSubject('Test Subject');
$email->setMessage('Test My SMTP');
if (!$email->send()) {
return false;
}else{
return true;
}
I am trying to send email using Zend Smtp.
I have configured everything with zend and with my credential of google.
When I try to send a mail i am getting this error.
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
I am not sure what the mistake i did. Can any one help me out. This is my code.
IndexController.php
public function indexAction()
{
$mail = new Zend_Mail();
$mail->addTo('chaitanya5a2#gmail.com', 'Chaitanya Kanuri')
->setFrom('chaitanya#gmail.com', 'Myself')
->setSubject('My Subject')
->setBodyText('Email Body')
->send();
}
Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDefaultEmailTransport() {
$emailConfig = $this->getOption('email');
$smtpHost = $emailConfig['transportOptionsSmtp']['host'];
unset($smtpHost);
$mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $emailConfig['transportOptionsSmtp']);
Zend_Mail::setDefaultTransport($mailTransport);
}
}
application.ini
email.transportOptionsSmtp.host = "smtp.gmail.com"
email.transportOptionsSmtp.auth = "login"
email.transportOptionsSmtp.username = "mygmail#gmail.com"
email.transportOptionsSmtp.password = "mygmailpassword"
email.transportOptionsSmtp.ssl = "ssl"
email.transportOptionsSmtp.port = 465
Thanks In advance.
I have the following code in my controller trying to get it to work before adding validation etc
This code is from here however I will be adapting it anyway
$email = $_POST['Newsletter[email]'];
$session->save_email($email);
// Subscribe User to List
$api_key = new sammaye\mailchimp\Mailchimp(['apikey' => 'xxxxxxxxxxx']);
$list_id = "xxxxxxxxxxx";
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => $email ) );
However I get the following error (Does this mean my most data is in an array newbie)
Undefined index: Newsletter[email]
Is this something I need to set within my yii2 form so that instead of the name field being Newsletter[email] its just email?
You can do this as follows:
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$email = $data['NewsLetter']['email'];
}
As was already stated you can either use $_POST or the request-object.
That object encompasses everything that enters your application on startup. So yes, \Yii::$app->request->post() gives you all incoming POST-data and \Yii::$app->request->post('name') will give you a single one. Same with the get function.
However given your code that is not how you should be using Yii. The name of your variables suggests that the post is done using a model, so you might want to use that again, makes it a lot easier on validation.
If you don't have the model, it can look like so:
class Newsletter extends \yii\base\Model
{
public $email;
public function rules()
{
return array(
array('email', 'email', 'skipOnEmpty' => false)
);
}
}
The actual code in your controller could be more amongst the lines:
$request = \Yii::$app->request;
if ($request->isPost) {
$newsletter = new Newsletter;
if ($newsletter->load($request->post()) && $newsletter->validate()) {
// do your thing
}
}
It means, it is inside an array.
Try to do the following instead :
// Using isset will make sure, you don't trigger a Notice (when the variable does not exist)
$email = isset($_POST['Newsletter']['email']) ? $_POST['Newsletter']['email'] : null;
// Make sure you are receiving an email address
if ($email && filter_var($email, FILTER_VALIDATE_EMAIL))
{
$session->save_email($email);
// Subscribe User to List
$api_key = new sammaye\mailchimp\Mailchimp(['apikey' => 'xxxxxxxxxxx']);
$list_id = "xxxxxxxxxxx";
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => $email ) );
}
// Display an error message ?
else
{
// #todo
}
If the HTML form field is as below
<input name="Newsletter[email]" type="text">
then the code with in controller should be
$data = Yii::$app->request->post();
$email= $data['Newsletter']['email'];
The inline solution is:
if(is_null($email = Yii::$app->request->post('Newsletter')['email']))
throw new BadRequestHttpException('newsletter email must set');
if so if email isn't set it throws Bad Request and notice that $_POST isn't good solution when you are using a PHP framework and it's security isn't provided by framework but Yii::$app->request->post is secured by Yii.
You should simply try $_POST['Newsletter']['email'].
I'm pretty new to CakePHP and this is my first attempt setting up an email form.
Keeping the example simple:
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
class EmailController extends AppController {
public function send_email($from, $subject, $message) {
$Email = new CakeEmail();
$Email->from($from)
->to('[my personal email]')
->subject($subject);
if($Email->send($message)) {
$result = 'Your email has been sent.';
} else {
$result = 'Your email failed to send.';
}
$this->set('result', $result);
$this->set('params', '('.$from.'|'.$subject.'|'.$message.')');
}
}
send_email.ctp
<?php echo $result;?>
<br>
<?php echo $params;?>
I'm getting "Your email has been sent.", the $params look as I expect, and I am not seeing any errors... but I'm not getting the email. Any idea why this might happen?
Before this you need to define Email configuration in email.php under Config folder
Here we have gmail configuration for example
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'username#gmail.com',
'password' => '*****',
'transport' => 'Smtp'
);
}
then you can use this setting in controller like
$Email= new CakeEmail('gmail');
Inshort you have to configure SMTP according to requirement. I hope this will be handy for you. Thanks
I have been trying to get this Email code working in my CakePHP for ages now. It is meant gather data from a simple contact form and send an email to a specific address. For now I am simply trying to get the emailing sending.
When I load the page I get Could not send Email and when I look at the log I get [SocketException] Could not send email.
Also I have used the smtp settings in a different area for a reset password email that does work.
Any help would be greatly appreciated.
Here is my ContactController
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
class ContactController extends AppController {
public function sendEmail()
{
/*$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$visitor_email = $_POST['email'];
$telephone = $_POST['telephone']; */
// $comments = $_POST['comments'];
$Email = new CakeEmail();
/* SMTP Options */
$Email->smtpOptions = array(
'transport' => 'Smtp',
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'frankstoncsf#gmail.com',
'password'=>'xxxxxxx',
'log'=>true
);
$Email->template = 'resetpw';
$Email->from(array('frankstoncsf#gmail.com' => 'My Site'));
$Email->to('thomas.chambers5#gmail.com');
$Email->subject('Reset Your Frankson.net User Password');
$Email->sendAs = 'both';
$Email->delivery = 'Smtp';
// $Email->set('ms', 'hello');
$Email->send('hello');
set('smtp_errors', $Email->smtpError);
}
public function index() {
$this->sendEmail();
}
}