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();
}
}
Related
I am building this website and on one of the pages I have a contact form from which the user should be able to send a message to us.
.env :
###> symfony/google-mailer ###
# Gmail SHOULD NOT be used on production, use it in development only.
MAILER_DSN=gmail://myEmail.com:myPassword#default?verify_peer=0
###< symfony/google-mailer ###
mailer.yaml :
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
the code that is supposed to send the mail :
/**
* #Route("/contact", name="api_mail")
*/
public function mailAPI(Request $request, MailerInterface $mailer)
{
if($_SERVER["REQUEST_METHOD"] == "POST"){
$name = $_POST["Name"];
$mail = $_POST["Mail"];
$game = $_POST["Game"];
$category = $_POST["Categ"];
$message = $_POST["Msg"];
$email = (new Email())
->from($mail)
->to('myEmail#gmail.com')
->subject('Testing the mail sender from symfony.')
->text($message)
->html('<p> This is a message </p>');
$mailer->send($email);
}
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
]);
}
}
At first I used mailCatcher and it caught the mails, now I configured the gmail thingy to send the mails via the gmail transport but it doesn't do anything.
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.
I'm using Yii framework while my question is probably intended to PHP expert.
I have created a controller to send email from my web application, it works fine.
Given that, I intend to use email in several sontrollers in my app, I wanted to created a helper but that does not work. Email is not sent. (I'm using swiftmailer)
The code of the working controller is the following:
<?php
class MailController extends Controller
{
/**
* Declares class-based actions.
*/
public function actionSendemail() {
// Plain text content
$plainTextContent = "This is my first line ;-)\nThis is my second row of text";
// Get mailer
$SM = Yii::app()->swiftMailer;
// New transport mailHost= localhost, mailPort = 25
$Transport = $SM->smtpTransport(Yii::app()->params['mailHost'], Yii::app()->params['mailPort']);
// Mailer
$Mailer = $SM->mailer($Transport);
// New message
$Message = $SM
->newMessage('My subject')
->setFrom(array('test1#localhost.localdomain' => 'Example Name'))
->setTo(array('myemail#domain.com' => 'Recipient Name'))
// ->addPart($content, 'text/html')
->setBody($plainTextContent);
// Send mail
$result = $Mailer->send($Message);
}
}
The helper code is the following
<?php
// protected/components/Email.php
class Email {
public static function sendEmail($subject, $from, $to, $body)
{
// Get mailer
$SM = Yii::app()->swiftMailer;
// New transport
$Transport = $SM->smtpTransport(Yii::app()->params['mailHost'], Yii::app()->params['mailPort']);
// Mailer
$Mailer = $SM->mailer($Transport);
// New message
$Message = $SM
->newMessage($subject)
->setFrom(array($from => 'Example Name'))
->setTo(array($to => 'Recipient Name'))
// ->addPart($content, 'text/html')
->setBody($body);
// Send mail
$result = $Mailer->send($Message);
}
}
the way I call it is the following
$subject= 'My subject';
$from = Yii::app()->params['adminEmail']; // adminEmai is a globalparam like above controller
$to='xxxx#xxx.com';
$body='my body';
Email::sendEmail($subject, $from, $to, $body);
when I run this code, I have no error, but I dont receive the email.
Thank you for your help.
I found my error.
my global parameters in config.php was not set correctly.
So content I put in the from field was not recognized by my hmailserver which is configured for the moment with the content test1#localhost.localdomain
sorry for the question and thanks
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
Why does setReplyTo($reply_to_mail) send email to $reply_to_mail? Shouldn't it just add email adress to reply-to field in the email message?
Currenyly if sending mail from website form and filling reply-to field, message sends to reply-to email and to our admin email.
Why does it duplicates email? Should send only to our admin email.
class Helper_Mail extends Zend_Controller_Action_Helper_Abstract
{
public function direct($email,$from,$message,$title,$replyto='')
{
$this->sendmail($email,$from,$message,$title,$replyto);
}
private function sendMail($email,$from,$message,$title,$replyto)
{
/* Configuring SMTP settings */
$config = array(
'auth' => 'login',
'ssl' => 'tls',
'username' => 'adminmail#gmail.com',
'password' => 'password',
'port' => 587);
$smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$config);
Zend_Mail::setDefaultTransport($smtpHost);
$mail = new Zend_Mail('UTF-8');
$mail->setBodyHtml($message);
$mail->setFrom('adminmail#gmail.com', $from);
$mail->addTo($email);
$mail->setSubject($title);
if(!empty($replyto))
{
$mail->setReplyTo($replyto);
}
try
{
$mail->send();
}
catch(Zend_Mail_Exception $e)
{
echo $e->getMessage();
}
}
}
You can use Zend_Mail::setReplyTo() if you are using a version of Zend > 1.8
If not (<= 1.8) you should use Zend_Mail::addHeader('Reply-To', 'replymail#example.com')
It was a bug, fixed in new versions. ;)