I want to send an email using zend/mail in zend framework 2.
I already have some code but i don't know where to put it, and also not how to trigger this.
$mail = new Mail\Message();
$mail->setBody('This is the text of the email.');
$mail->setFrom('email#hotmail.be', 'email');
$mail->addTo('email#hotmail.be', 'email');
$mail->setSubject('Dit is een email verzonden van de computer');
$transport = new Mail\Transport\Sendmail('-freturn_to_email#hotmail.be');
$transport->send($mail);
I still new at Zend framework 2.
Can anybody help me with this?
I would advice you to take a look at Soflomo\Mail. Note, I am the author of Soflomo mail, but it helps you a lot with sending mails. It eases the config and dependency hassle.
Put "soflomo/mail": "~0.3" in your composer.json file and run the following command:
php composer.phar update soflomo/mail
Next, enable the module Soflomo\Mail in your application.config.php. When you use Zend\Mvc, you will have a controller/action which should trigger the email. For the most simplified use case, you can do this:
public function doAction()
{
// some of your logic
$service = $this->getServiceLocator()->get('Soflomo\Mail\Service\MailService');
$service->send(array(
'to' => 'email#hotmail.be',
'to_name' => 'email',
'from' => 'email#hotmail.be',
'from_name' => 'email',
'subject' => 'Dit is een email verzonden van de computer',
'template' => 'mail/message/default'
));
}
Now Soflomo\Mail sends a message by rendering a template and use that as message text. Here I defined a message mail/message/default so create that file (e.g. module/Application/view/mail/message/default.phtml) with this content:
This is the text of the email.
The last thing to do is to configure how Soflomo/Mail sends the message. Your question uses Sendmail, so I am using this as well in this example. Create a configuration file in config/autoload , e.g. config/autoload/soflomo_mail.global.php which contains the following content:
return array(
'soflomo_mail' => array(
'transport' => array(
'type' => 'sendmail',
),
),
);
If you want to switch to e.g. GMail as transport layer, replace above config with:
return array(
'soflomo_mail' => array(
'transport' => array(
'type' => 'smtp',
'options' => array(
'name' => 'gmail.com',
'host' => 'smtp.gmail.com',
'port' => 587,
'connection_class' => 'login',
'connection_config' => array(
'ssl' => 'tls',
'username' => '%USERNAME%',
'password' => '%PASSWORD%',
),
),
'variables' => array(
'username' => '',
'password' => '',
),
),
),
);
And create a new file config/autoload/soflomo_mail.local.php:
return array(
'soflomo_mail' => array(
'transport' => array(
'variables' => array(
'username' => 'my-address#gmail.com',
'password' => '1234secure7890',
),
),
),
);
I guess Hotmail would be similar to GMail.
You can use this within the zend directory,
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody#example.com', 'Some Sender');
$mail->addTo('somebody_else#example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
Related
I struck on this problem on many days. Please help. I have followed the cakephp documentation. but could not resolve issue.
Could not send email: unknown
Error: An Internal Error Has Occurred.
Following is Configuration emai.php
<?php
class EmailConfig {
public $default = array(
'transport' => 'Mail',
'from' => 'developer.support#sevenrocks.in',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
public $smtp = array(
'transport' => 'Smtp',
'from' => array('site#localhost' => 'SevenRocks'),
'host' => 'ssl://smtp.sevenrocks.in',
'port' => 465,
'timeout' => 30,
'username' => 'developer.support#sevenrocks.in',
'password' => 'developerofsevenrocks',
'client' => null,
'log' => true,
'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}
Following is code in controller
$email = new CakeEmail();
$email->emailFormat('html');
$email->from(array($from_email => SITE_NAME));
$email->to($to);
$email->subject($subject);
if ($files)
{
$email->attachments($files);
}
if ( !$email->send($content) )
{
return false;
}
First: to debug CakePHP 2x applications search for debug in your app/Config/core.php and change it to Configure::write('debug', 2); to see the full error message.
Second: Some providers may prevent you from sending Mails via PHP directly (default mail config). A better solution may to use the smtp configuration you provided in email.php.
To use your smtp configuration change your controller code to:
$email = new CakeEmail('smtp');
$email->emailFormat('html');
$email->to($to);
$email->subject($subject);
For more Info see https://book.cakephp.org/2.0/en/core-utility-libraries/email.html#configuration
I have a weird problem using CakePHP/CakeMail.
I try to send an email for my gmail from the website.
I don't get the email, but I don't get any error either.
The form send the message. The log don't hit anything. Passwords and emails are ok, so what could be wrong?
Here are the codes.
email.php
public $mymail = array(
'transport' => 'Smtp',
'from' => array('xpto#domain.com.br' => 'YourName'),
'sender' => 'my#gmail.com',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'my#gmail.com',
'password' => 'mygmailpassword',
'client' => null,
'log' => true,
'emailFormat' => 'both'
'charset' => 'utf-8',
'returnPath' => 'xpto#domain.com.br',
'additionalParams' => '-f'.'xpto#domain.com.br',
'headerCharset' => 'utf-8',
);
PagesController.php
public function admin_send_contato() {
if ($this->request->is('post')) {
if(!isset($this->data['email'])){
$this->data['assunto'] = 'Assunto';
}
$email = new CakeEmail('mymail');
$this->Email->return = 'my#gmail.com';
$email->from(array('xpto#domain.com.br' => 'John Doe'))
->to('my#gmail.com')
->subject($this->data['subject'])
->replyTo($this->data['email'])
->send("Name: ".$this->request->data['name']."\nPhone: ".$this->request->data['phone']."\nE-mail: ".$this->request->data['email']."\nMessage: ".$this->request->data['message']);
echo json_encode('ok');
}
$this->autoRender = false;
}
I don't create the site and never used Cake before.
I have no idea what is the problem.
Any thoughts?
Thank you!
I'd recommend using a third party solution to send transactional and marketing emails. Sendgrid is an option and they have documentation to integrate in CakePHP; https://sendgrid.com/docs/Integrate/Frameworks/cakephp.html
I'm trying to send email using Mandrill and PHP and I can't get it to send.
I've downloaded the PHP API wrapper from here:https://packagist.org/packages/mandrill/mandrill
Mandrill.php is in my root and the Mandrill folder is in the same directory.
Here's my code:
<?php
require_once 'Mandrill.php';
$mandrill = new Mandrill('MY API KEY IS USUALLY HERE');
$message = array(
'subject' => 'Test message',
'from_email' => 'jwjody#yahoo.com',
'from_name' => 'Sender person',
'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
'to' => array(array('email' => 'jwjody#yahoo.com', 'name' => 'Recipient 1')),
'merge_vars' => array(array(
'rcpt' => 'recipient1#domain.com',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Recipient 1 first name'),
array(
'name' => 'LASTNAME',
'content' => 'Last name')
))));
//print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
echo ("hello");
?>
But it won't send. I'm not sure where the failure is. Is it something obvious I'm missing?
I see the issue now.
I see what's going on now!
I changed
$mandrill->messages->sendTemplate($template_name, $template_content, $message));
to
$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);
And it works!
Instead of calling the sendTemplate() function I should have used
$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);
Once I changed the function call the mail was sent.
So I've made a template called 'Basic' within Mandrill. I did a test send and it looks great. I put Mandrill into test mode and used the test API key in my code. I'm just trying to get the php to send out a test transactional email, but no email gets sent. Here is the printed response I get:
Array ( [0] => Array ( [email] => amiecrutchley02#gmail.com [status] => sent [_id] => 89bfab4c3938486eb9e36564f79a3e9f [reject_reason] => ) )
So I'm really not sure why I'm not receiving anything.
Here is my code:
<?php
require_once('includes/mandrill/Mandrill.php');
$mandrill = new Mandrill('my_api_key');
$message = array(
'subject' => 'Thank You For Your Purchase',
'from_email' => 'no-reply#acq.com',
'from_name' => 'ACQ',
'to' => array(array('email' => 'amiecrutchley02#gmail.com', 'name' => 'Amie')),
'merge_vars' => array(array(
'rcpt' => 'amiecrutchley02#gmail.com',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Amie'),
array(
'name' => 'LASTNAME',
'content' => 'Crutchley')
))));
$template_name = 'Basic';
$template_content = array(
array(
'name' => 'main',
'content' => 'Hi *|FIRSTNAME|* *|LASTNAME|*, your profile has been updated!'),
array(
'name' => 'footer',
'content' => 'ACQ, Copyright 2014')
);
$response = $mandrill->messages->sendTemplate($template_name, $template_content, $message);
print_r($response);
?>
a test API key is specifically designed to not send email. It's designed so you can mimic sending email, but doesn't actually send. You're also not charged for sending tests. Here's the Mandrill KB about what test mode is, and how it works: Does Mandrill have a test mode or sandbox?
Well, weird, but my test API was the problem. I tried the public API and boom! It works!
hi friends how to send a email through kohana 3.0
i tried but not working
my code is like this
$subject = ' : Message to Leet Street';
$from = array('Clarence', 'ratnaraju.java#gmail.com');
email::send('ratnaraju.bonam#gmail.com', $from , $subject, 'hi how r u Brother ');
url::redirect();
config file is:
return array
(
'default' => array(
'transport' => 'smtp',
'options' => array
(
'hostname' => 'smtp.gmail.com',
'username' => 'ratnaraju.bonam#gmail.com',
'password' => 'Ratna',
'port' => '465',
),
)
);
thanks in advance
Use this module to send email:
https://github.com/shadowhand/email
it needs this vendor in the vendor-dir:
https://github.com/swiftmailer/swiftmailer