SMTP mail options with tls in cakephp 1.3 - email

I am sending mail using smtp options. When I sent mail via ssl it is working fine. But when I tried to send mail via tls, it is not working properly.
My application is in CakePHP 1.3.
function testemail() {
$from = 'test#domain.com';
$this->Email->delivery='smtp';
$this->Email->smtpOptions = array(
'port'=>'portnumber',
'auth' => true,
'host' => 'tls://smpt.xxxx.com',
'username'=>'test#domain.com',
'password'=>'xxxxxxxxx',
);
$this->Email->to = 'xxxxxx#gmail.com';
$this->Email->subject = 'Test Email';
$this->Email->from = $from;
$this->Email->replyTo = $from;
$this->Email->return = $from;
$this->Email->additionalParams = '-f'.$from;
$this->Email->sendAs = 'html';
$this->Email->template = 'template';
$this->set('template','Testing TLS mail');
if($this->Email->send()) {
$this->Session->setFlash(__('Mail sent.', true));
} else {
$this->Session->setFlash(__('Error.', true));
}
}
Thanks

try this alternative if your server supports SSL:
'port' => '465',
'host' => 'ssl://smpt.xxxx.com',

Related

error sending email in codeigniter 3 and php 7

$config = [
'protocol' => 'sendmail',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_user' => '***************',
'smtp_pass' => '********',
'smtp_port' => 465,
'smtp_timeout' => '30',
'mailtype' => 'html',
// 'ssl' => [
// 'verify_peer' => false,
// 'veriry_peer_name' => false,
// 'allow_self_sign' => true
// ],// ini hanya jika jaringa tidak ssl/https, jangan dilakukan di produksi
'starttls' => true,
'charset' => 'utf-8',
'newline' => "\r\n"
];
Hi All, I have
I have tried to send mail from my local host but it return error like bellow,
Message: stream_socket_enable_crypto(): SSL/TLS already set-up for this stream
Thank you for your help,
for sending mail from local i'm using SMTP. Please change your code with following code. It's working for me. Do not edit system file. That will create problem for you in future.
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = '********'; //your email id
$config['smtp_pass'] = '********'; //email id password
$config['smtp_port'] = 465;
$config['smtp_timeout'] = 5;
$config['wordwrap'] = TRUE;
$config['wrapchars'] = 76;
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['validate'] = FALSE;
$config['priority'] = 3;
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
$config['bcc_batch_mode'] = FALSE;
$config['bcc_batch_size'] = 200;

attachment is not sent in email in CodeIgniter [duplicate]

I am trying to send email on codeigniter with attach file.
I always receive email successfully. However , I never receive with attach file. Below is code and highly appreciate for all comments.
$ci = get_instance();
$ci->load->library('email');
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.gmail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "test#gmail.com";
$config['smtp_pass'] = "test";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('test#test.com', 'Test Email');
$list = array('test2#gmail.com');
$ci->email->to($list);
$this->email->reply_to('my-email#gmail.com', 'Explendid Videos');
$ci->email->subject('This is an email test');
$ci->email->message('It is working. Great!');
$ci->email->attach( '/test/myfile.pdf');
$ci->email->send();
$this->email->attach()
Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple times. For example:
public function setemail()
{
$email="xyz#gmail.com";
$subject="some text";
$message="some text";
$this->sendEmail($email,$subject,$message);
}
public function sendEmail($email,$subject,$message)
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'abc#gmail.com',
'smtp_pass' => 'passwrd',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('abc#gmail.com');
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach('C:\Users\xyz\Desktop\images\abc.png');
if($this->email->send())
{
echo 'Email send.';
}
else
{
show_error($this->email->print_debugger());
}
}
i have this problem before , the problem with the path file , so i change the path file to
$attched_file= $_SERVER["DOCUMENT_ROOT"]."/uploads/".$file_name;
$this->email->attach($attched_file);
And it works fine with me
With Codeigniter 3.1.0 I had same problem. Seems that there is missing a "\r\n":
Content-Type: application/pdf; name="test.pdf"<br>
Content-Disposition: attachment;<br>
Content-Transfer-Encoding: base64<br>
JVBERi0xLjYNJeLjz9MNCjQzNyAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA3OTUyMTYvTyA0Mzkv<br>
RSA2ODEwODcvTiA0L1QgNzk0ODA3L0ggWyA1NjQgMjYxXT4+DWVuZG9iag0gICAgICAgICAgICAg<br>
should be:
Content-Type: application/pdf; name="test.pdf"<br>
Content-Disposition: attachment;<br>
Content-Transfer-Encoding: base64<br>
<br>
JVBERi0xLjYNJeLjz9MNCjQzNyAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA3OTUyMTYvTyA0Mzkv<br>
RSA2ODEwODcvTiA0L1QgNzk0ODA3L0ggWyA1NjQgMjYxXT4+DWVuZG9iag0gICAgICAgICAgICAg<br>
I changed line 725 in system/libraries/Email from
'content' => chunk_split(base64_encode($file_content)),<br>
to
'content' => "\r\n" . chunk_split(base64_encode($file_content)),<br>
It works for me, but not the perfect fix.
Try putting the full path in $ci->email->attach();
On windows this would like something like
$ci->email->attach('d:/www/website/test/myfile.pdf');
This method has worked well for me in the past.
If you want to send attachment in email Without uploading file on server, please refer below.
HTML View file
echo form_input(array('type'=>'file','name'=>'attach_file','id'=>'attach_file','accept'=>'.pdf,.jpg,.jpeg,.png'));
Controller file
echo '<pre>'; print_r($_FILES); shows below uploaded data.
[attach_file] => Array
(
[name] => my_attachment_file.png
[type] => image/png
[tmp_name] => C:\wamp64\tmp\php3NOM.tmp
[error] => 0
[size] => 120853
)
We will use temporary upload path [tmp_name] where attachment is uploaded, because we DO NOT want to upload attachment file on server.
$this->email->clear(TRUE); //any attachments in loop will be cleared.
$this->email->from('your#example.com', 'Your Name');
$this->email->to('someone#example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
//Check if there is an attachment
if ( $_FILES['attach_file']['name']!='' && $_FILES['attach_file']['size'] > 0 )
{
$attach_path = $_FILES['attach_file']['tmp_name'];
$attach_name = $_FILES['attach_file']['name'];
$this->email->attach($attach_path,'attachment',$attach_name);
}
$this->email->send();
use path helper
$this->load->helper('path');
$path = set_realpath('./images/');
on email line
$this->email->attach($path . $your_file);
here i am using phpmailer to send mail
here full code is mention below
$this->load->library('My_phpmailer');
$mail = new PHPMailer();
$mailBody = "test mail comes here2";
$body = $mailBody;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1;// enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true;// enable SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // sets the SMTP server
$mail->Port = 465;// set the SMTP port for the GMAIL server
$mail->Username = "YourAccountIdComesHere#gmail.com"; // SMTP account username
$mail->Password = "PasswordComesHere";// SMTP account password
$mail->SetFrom('SetFromId#gmail.com', 'From Name Here');
$mail->AddReplyTo("SetReplyTo#gmail.com", "Reply To Name Here");
$mail->Subject = "Mail send by php mailer";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment($cdnStorage . '/' . $fileName);
$address ='WhomeToSendMailId#gmail.com';
$mail->AddAddress($address, "John Doe");
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Here Is Full Source Code
$validation_rules = array(
array('field' => 'name', 'rules' => COMMON_RULES),
array('field' => 'email', 'rules' => COMMON_RULES),
array('field' => 'message', 'rules' => COMMON_RULES),
);
$this->validation_errors($validation_rules);
$name = $this->input->post('name');
$email = $this->input->post('email');
$message = $this->input->post('message');
$this->load->library('email');
//upload file
$attachment_file = "";
if (!empty($_FILES) && isset($_FILES["attachment_file"])) {
$image_name = $_FILES["attachment_file"]['name'];
$ext = pathinfo($image_name, PATHINFO_EXTENSION);
$new_name = time() . '_' . $this->get_random_string();
$config['file_name'] = $new_name . $ext;
$config['upload_path'] = "uploads/email/";
$config['allowed_types'] = "*";
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('attachment_file')) {
$finfo = $this->upload->data();
$attachment_file = base_url() . 'uploads/email/' . $finfo['file_name'];
} else {
$error = $this->upload->display_errors();
$this->msg = $error;
$this->_sendResponse(5);
}
}
$this->email->from($email, "$name")
->to("example#gmail.com")
->subject("FeedBack From $name")
->message($message)
->attach($attachment_file);
if ($this->email->send()) {
// temp pass updated.
$this->msg = "Email send successfully.";
$this->_sendResponse(1);
} else {
$this->msg = "Internal server error/Something went wrong.";
$this->_sendResponse(0);
}
$this->load->library('email'); // Loading the email library.
$this->email->clear(TRUE);
$this->email->from($user_email, $name);
$this->email->to('email#gmail.com');
$this->email->subject("Some subject");
$this->email->message("Some message");
if($_FILES['userfile']['name']!='' && $_FILES['userfile'['size'] > 0){
$attach_path = $_FILES['userfile']['tmp_name'];
$attach_name = $_FILES['userfile']['name'];
$this->email->attach($attach_path,'attachment',$attach_name);
}
$this->email->send();

Setting mail template in ses client

I am developing a web application using cakephp 3.0 and I have an email sending function earlier I was use php mail function. Now I am using AWS ses client. So in that ses client how I can render a template. In cake php email function it was possible. But I don't know how to do it in aws ses client.
My code is
public function testMail() {
if ($this->request->is('post')) {
$client = SesClient::factory(Configure::read('AWScredentials'));
$formData = $this->request->data;
$body = $formData['body'];
$ToAddresses = $formData['ToAddresses'];
$request = array();
$request['Source'] = 's#gmail.com';
$request['Destination']['ToAddresses'] = array($ToAddresses);
$request['Destination']['CcAddresses'] = 'ss#gmail.com';
$request['Message']['Subject']['Data'] = 'Test mail from vcollect ses';
$request['Message']['Subject']['Charset'] = 'ISO-2022-JP';
$request['Message']['Body']['Text']['Data'] = $body;
$request['Message']['Body']['Text']['Charset'] = 'ISO-2022-JP';
try {
$result = $client->sendEmail($request);
$messageId = $result->get('MessageId');
$this->log("Email sent! Message ID: $messageId", "info");
} catch (Exception $e) {
$this->log("The email was not sent. Error message:" . $e->getMessage(), "error");
}
} }
You must create a debug sender in app.php, just add it to your EmailTransport array like this, it's allow you to do a fake email without send it
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'debug' => [
'className' => 'Debug',
],
],
after to generate the email with your your template you can use this function for exemple
public function setTemplate($template,$layout = null,$viewVars = array(),$debug = 0)
{
if(empty($layout)) $layout = 'default';
$Email = new Email();
$Email->template($template, $layout)
->transport('debug') //<---- Allows you to simulate the message without sending it
->to(array('foo#bar.org'=>'toto'))
->emailFormat('html');
->viewVars($viewVars);
$Email->send();
/* Once email is generate, personally i duplicate the object for access to the protected proprety ( it's not very clean but i don't have a greatest solution)
The idea is to get _htmlMessage in the object */
$refObj = new ReflectionObject($Email);
$refProp1 = $refObj->getProperty('_htmlMessage');
$refProp1->setAccessible(TRUE);
if($debug == 1)
{
die(debug($refProp1->getValue($Email)));
}
return $refProp1->getValue($Email); // <---- your html code
}

Send mail to multiple recipients from data base using zf2

This is my first application using Zend Framework 2. I want to send an email to recipients retreived from a database, but each time my code displays an error. This is my code:
$user = new Container('user');
$db = $this->getServiceLocator()->get('db1');
if (!$user->offsetExists('id')) {
$idconnected = '0';
} else {
$idconnected = $user->offsetGet('id');
$mail = $db->query("SELECT email FROM user WHERE id =" . $idconnected)->execute()->current();
$message = new Message();
$message->addTo($mail, 'eee#web.com')
->addFrom('xxxx#gmail.com')
->setSubject('Invitation à l’événement : Soirée Latino');
$message->addCc('xxxx#hotmail.com')
->addBcc("xxxx#hotmail.com");
// Setup SMTP transport using LOGIN authentication
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'host' => 'smtp.gmail.com',
'connection_class' => 'login',
'connection_config' => array(
'ssl' => 'tls',
'username' => 'xxxx#gmail.com',
'password' => '*********'
),
'port' => 587,
));
$html = new MimePart('<b>Invitation for the event: Latin Night, orgonized by Mr. Jony Cornillon <i>Date : 06/04/2015</i></b>');
$html->type = "text/html";
$body = new MimeMessage();
$body->addPart($html);
//$body->setParts(array($html));
$message->setBody($body);
$transport->setOptions($options);
$transport->send($message);
}
And this is the error:
5.1.2 We weren't able to find the recipient domain. Please check for any
5.1.2 spelling errors, and make sure you didn't enter any spaces, periods,
5.1.2 or other punctuation after the recipient's email address. a13sm19808164wjx.30 - gsmtp
I tried with this code, and now it works fine :
foreach ($mail as $recip) {
$message->addTo($recip);
$message->addTo('samehmay#hotmail.com', 'eee#web.com')
->addFrom('maysameh0#gmail.com')
->setSubject('Invitation à l’événement : Soirée Latino');
}

Why gmail smtp doesn't work when sending mail in cakephp using localhost

I get an error saying.
Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?
But yet I have enabled it in the php.ini file
Here is my EmailConfig
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'email#gmail.com',
'password' => 'password',
'transport' => 'Smtp',
'timeout' => 30,
);
and my controller
public function send_mail(){
$message = 'Test email';
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail('gmail');
$email->from('email#gmail.com');
$email->to('email#yahoo.com');
$email->subject('Test');
$email->send($message);
}
I fail to see where the problem lies
try this
public function send_mail(){
$message = 'Test email';
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->config('gmail')
->from('email#gmail.com')
->to('email#yahoo.com')
->subject('Test')
->send($message);
}
I Want to send email from cakephp 2.x in company domain email adress
CakePHP send email
Next step
From wamp Apache/Apache_modules/ tick ssl_module
At the end
Enable the php_openssl and php_opensockets
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/