I am using SMTP to send email in my CAKEPHP Project.
My email config as follows
class EmailConfig {
public $Smtp = array(
'transport' => 'Smtp',
'from' => array('contact#mydomainname.com' => 'domainname.com'),
'host' => 'myhostingserver',
'port' => 2525,
'timeout' => 60,
'username' => 'username#mydomainname.com',
'password' => 'secret',
'client' => null,
'log' => false
);
and my Mail Functionality code as follows
$email = new CakeEmail('Smtp');
$result = $email->template('welcome_mail','default')
->emailFormat('html')
->to($to_email)
->from('contact#mydomainname.com')
->subject('Welcome to my domain name')
->viewVars($contents);
if($email ->send('Smtp'))
{
echo ('success');
}
While i am sending mail its throwing following error SMTP timeout. My SMTP Server details are correct its working fine in an existing server. I don't know where I am wrong
Check the encryption type (if applicable), e.g. ssl or tls
Your host URL should look something like this in such case
'host' => 'ssl://myhostingserver'
or
'host' => 'tls://myhostingserver'
If your SMTP server has SSL,you have to enable php_openssl in php.ini to use this service.
You can use this code to test
if(!in_array('openssl',get_loaded_extensions())){
die('you have to enable php_openssl in php.ini to use this service');
}
beside what here already was sugested here that the module must be loaded. i found that some servers have some ports blocked. i used this script to test some servers:
<?php
if(!in_array('openssl',get_loaded_extensions())){
die('you have to enable php_openssl in php.ini to use this service');
} else {
echo "php_openssl in php.ini is enabled <br />";
}
// fill out here the smpt server that you want to use
$host = 'ssl://smtp.gmail.com';
// add here the port that you use for for the smpt server
$ports = array(80, 465);
foreach ($ports as $port)
{
$connection = #fsockopen($host, $port);
if (is_resource($connection))
{
echo $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.<br />' . "\n";
fclose($connection);
} else {
echo $host . ':' . $port . ' is not responding.<br />' . "\n";
}
}
?>
Related
I have written a drupal module with custom form api which will send email to my inbox on each submit. I have written a condition under drupal_mail which returns true but shows an error message "Unable to send e-mail. Contact the site administrator if the problem persists."
Below my code:
function my_module_name_mail($key, &$message, $params)
{
$headers = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8;',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
foreach ($headers as $key => $value) {
$message['headers'][$key] = $value;
}
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
}
function my_module_name_form_submit($form, &$form_state)
{
$from = $form_state['values']['email'];
$body= 'Name: '.$name.'<br />Email: '.$email;
$to = "my_mail_id#example.com";
$params = array(
'body' => $body,
'subject' => 'Website Information Request',
);
drupal_mail('my_module_name', 'some_mail_key', $to, language_default(), $params, $from, TRUE);
}
Check your Drupal logs for more clues. Enable devel / devel-admin modules. Once you do, drupal_mail will pipe emails to your temp directory, which will help you narrow down if the problem is with your email server or the config.
I was using the following code snippet to send mails, but moving to another host messed everything up:
public function forgetpwd(){
//code omitted
if($this->User->saveField('token_hash',$fu['User']['token_hash'] )){
//============Email================//
/* SMTP Options */
$this->Email->smtpOptions = array(
'host' => 'smtp.gmail.com',
'port'=>'465',
'transport' => 'Smtp',
'username'=>'email#gmail.com',
'password'=>'secret',
'tls' => true
);
$this->Email->template = 'resetpw';
$this->Email->from = 'noreply#email.com';
$this->Email->to = $fu['User']['name'].'<'.$fu['User']['email'].'>';
$this->Email->subject = __('Recover your password');
$this->Email->sendAs = 'both';
$this->Email->delivery = 'smtp';
$this->set('ms', $ms);
$this->Email->send();
$this->set('smtp_errors', $this->Email->smtpError);
$this->Session->setFlash(__('Check your email to recover your password'));
}
//============EndEmail=============//
So there is this view forgetpwd.ctp where user is asked to enter his/her email address, and when trying to send the email, I am given the two following errors:
Error: The view for UsersController::forgetpwd() was not found.
Error: Confirm you have created the file: /home/public_html/development/app/View/Emails/text/resetpw.ctp
I am sure that the resetpw.ctp exists in the right place.
I have edited the email config and tried to adopt the 2.3 cakePHP way, and this is what I have ended up with:
if($this->User->saveField('token_hash',$fu['User']['token_hash'] )){
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail('default');
$Email->to($fu['User']['email'])
->subject('Reset your password')
->message($key)
->send();
$this->Session->setFlash(__('Check your mail to reset your password'));
$this->redirect(array('controller'=>'users','action'=>'reset'));
But now, when I try to send an email, I get the following error:
Call to a member function send() on a non-object
change
'host' => 'smtp.gmail.com',
to
'host' => 'ssl://smtp.gmail.com',
also put your config info in email.php inside App/Config/ as referenced here:
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#configuration
I have been asked to use an external SMTP server with the following instructions:
"We expect you to connect to port 25 in cleartext, then issue a STARTTLS to commence TLS/SSL. Then log in"
Is this possible and if so, how should I do it? As I understand it, this is different to setting the TLS to true in the smtp connection array, is that right?
UPDATE
Plodding towards victory here. Looking at the code in SmtpTransport.php I can see it matches up with the spec linked in the comments (obviously) but the block 102-107 seems to set the host to either client or localhost - I'm setting the remote ip in the host configuration. Am i doing it wrong?
Cakes code:
protected function _connect() {
$this->_generateSocket();
if (!$this->_socket->connect()) {
throw new SocketException(__d('cake_dev', 'Unable to connect to SMTP server.'));
}
$this->_smtpSend(null, '220');
if (isset($this->_config['client'])) {
$host = $this->_config['client'];
} elseif ($httpHost = env('HTTP_HOST')) {
list($host) = explode(':', $httpHost);
} else {
$host = 'localhost';
}
try {
$this->_smtpSend("EHLO {$host}", '250');
if ($this->_config['tls']) {
$this->_smtpSend("STARTTLS", '220');
$this->_socket->enableCrypto('tls');
$this->_smtpSend("EHLO {$host}", '250');
}
} catch (SocketException $e) {
if ($this->_config['tls']) {
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.'));
}
try {
$this->_smtpSend("HELO {$host}", '250');
} catch (SocketException $e2) {
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection.'));
}
}
}
My Email Config:
public $smtp = array(
'transport' => 'Smtp',
'from' => array('me#example.com' => 'my name'),
'host' => 'secretipaddress',
'port' => 25,
'timeout' => 30,
'username' => 'me#example.com',
'password' => 'secretpassword',
'client' => null,
'log' => true,
'tls' => true
);
Enable the 'tls' to true in your config file app.php.
It work for me and in cakephp 3.1 with gmail smtp
I'm using cakephp 2.3.0 and i want send email using devocot + postfix. SMTP work fine because i have installed roundCube and hi received emails and sends emails.
class EmailConfig {
public $default = array(
'host' => 'poczta.example.com',
'port' => 25,
'auth' => 'plain',
'username' => 'username#poczta.example.com',
'password' => '**********',
'tls' => true,
'transport' => 'Smtp',
'from' => array('username#poczta.example.com' => 'Username'),
'returnPath' => 'username#poczta.example.com',
'layout' => false,
'emailFormat' => 'html',
'template' => 'only_text',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
}
Code for send emails:
try {
$oEmail->reset();
$oEmail->config('default');
$oEmail->to(preg_replace('/&#?[a-z0-9]+;/i', '' , trim($v['Email']['email']) ));
$oEmail->subject($subject);
$content = str_replace('${id}', md5($v['Email']['id']), $context);
$content = str_replace('queueId', $queueId, $content);
$oEmail->viewVars(array('email' => $content));
if($oEmail->send()) {
$last = $v['Email']['id'];
$send++;
}
}
catch(Exception $e) {
$this->out($e->getMessage());
}
And i see i console:
SMTP Error: 535 5.7.8 Error: authentication failed: Invalid authentication mechanism
So quick test with telnet:
telnet 127.0.0.1 25
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 poczta.example.com ESMTP Postfix (Debian/GNU)
ehlo testing
250-poczta.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
So please help me how i sent email using cake ?
EDIT: i have tls in email config file.
It is not tsl as you have it in your config, it is tls
I tried out Zend Framework 2 skeleton application and its working fine in
Zend Server 5.6 (PHP Version 5.4.0 apache 2.2.21 MYSQL 5.0.10). But i want Zend Framework 2 to connect with MS SQL 2008. I tried the following but it doesn't work and throws exception
" An invalid parameter was passed to sqlsrv_execute. "
'db' => array(
'driver' => 'sqlsrv',
'hostname' => 'testserver\test',
'Database' => 'payroll',
'UID' => 'sa',
'PWD' => '123456'
),
whats wrong with above db array? please suggest me with correct connection string
FYI :
i have tested php 5.4 and MS SQL 2008 connection and it works fine, the following connection was established successfully.
/*
$serverName = "testserver\test"; //serverName\instanceName
$connectionInfo = array( "Database"=>"payroll", "UID"=>"sa", "PWD"=>"123456");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "---------- Connection established --------------------.<br />";
$sql = "select * from users";
$stmt = sqlsrv_query($conn, $sql);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['id'].", ".$row['username']."<br />";
}
} else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
*/
As you did not technically "answered" your own question, I will do it for you.
Try these connection parameters, they might work using PDO :
'db' => array(
'driver' => 'pdo',
'dsn' => 'sqlsrv:database=payroll;Server=testserver\test',
'username' => 'sa',
'password' => '123456'
),
In your global.php file add the below connection detail:
'db' => array(
'driver' => 'Sqlsrv',
'hostname' => 'SERVERNAME',
'Database' => 'DBNAME',
'uid' => 'username',
'pwd' => 'password',
)
To add sqlsrv driver, download it from url : http://www.microsoft.com/en-us/download/details.aspx?id=20098
Add extension in your php ini file like this and then restart apache server:
extension=php_sqlsrv_53_ts.dll