CakePHP 2.1.0: Capture E-mail Output - email

I'm building a CakePHP website that sends an e-mail like this:
$email = new CakeEmail('default');
$email->template('test');
$email->emailFormat('html');
$email->to(array('john_doe#example.com' => 'John Doe'));
$email->subject('Test E-mail');
$email->helpers(array('Html', 'Text'));
$email->viewVars(
array(
...
)
);
if ($email->send()) {
$this->Session->setFlash('The e-mail was sent!', 'default', array('class' => 'alert alert-success'));
}
else {
$this->Session->setFlash('An unexpected error occurred while sending the e-mail.', 'default', array('class' => 'alert alert-error'));
}
I'd like to be able to capture the HTML rendered by the e-mail in a variable in addition to actually sending the e-mail. This way, I can record in the database the exact content of the e-mail's body. Is this doable?

Per line 50 of the MailTransport class, it appears the actual send() function returns the message and the header. So instead of:
if($email->send()) {
Try:
$mySend = $email->send();
if($mySend) {
//...
Then, $mySend should be an array:
array('headers' => $headers, 'message' => $message);

Thats what I do in my EmailLib:
https://github.com/dereuromark/tools/blob/2.0/Lib/EmailLib.php
it logs email attempts and captures the email output into a log file (email_trace.log) in /tmp/logs/ - if you are in debug mode it will only log (no emails sent - this has been proven quite useful for local delopment).
you can write a similar wrapper for your case.
but if you want to write it back into the DB Dave's approach seems to fit better.

Related

Store email body in table using Laravel 5.3 Mailable

I want to store the email body in my communications table. My controller:
Mail::to($user->email)->send(new WelcomeEmail($subscription));
Communication::create([
'to' => $user->email,
'subject' => 'Welcome Email',
'body' => '???'
]);
My email goes out (successfully) and I am able to create a Communication record, but have no idea how to retrieve the email body.
After reading the Mail manual, I thought I could work with an event:
protected $listen = [
'Illuminate\Mail\Events\MessageSending' => [
'App\Listeners\LogSentMessage',
],
];
But here I get only the full plain text email. If I create an instance of the mail, with:
$email = Mail::to($user->email)->send(new WelcomeEmail($subscription));
the outcome of dd($email); is null.
Some extra info, in my WelcomeEmail.php, I am using a view:
public function build()
{
return $this->view('emails.welcome_email');
}
You can directly do this to render Mailable and store it in a variable
$html = (new WelcomeEmail($subscription))->render();
I found a solution, hopefully it can help other people.
$body = View::make('emails.welcome_email')
->with('subscription', $this->subscription)
->with('template', $this->template)
->render();
I rendered the view and saved it in the body variable.

Yii2 email template problems

Hello I have two problems with email creation.
the first is that the footer is duplicated:
<div class="footer">With kind regards, <?= Yii::$app->name ?> team</div>
Printing two times the same text
the second is about variables:
Yii::$app->mailer->compose('#app/mail/layouts/html',
['content' => $text,
'name'=>'the name',
and in view template I just try to get $name
$name
and the error is:
Undefined variable: name
The same happen with $image but not with $content
I use swiftmailer and this is my confuguration:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
useFileTransport is set true but i think is not problem
this is the complete code for send email
Yii::$app->mailer->compose('#app/mail/layouts/html',
['content' => $text,
'name'=>'the name',
'image' => 'path/to/images/image.png',
])
->setFrom(Yii::$app->params['adminEmail'])
->setTo($email)
->setSubject($subject)
->send();
Thank to all
Gabriele
first problem :
maybe there's another file included in other file where that's file included in email layouts, check it again.
second problem :
you can disable error notice in php.ini or if you want to fast just add # before the variable.
example :
$name change to #$name
for the same error you just add # at beginning variable.
In your example, you need to replace name of template.
Becouse layouts/html does not need to be explicitly specified.
See my answer here
https://stackoverflow.com/a/42960246/1932494

Yii contact form not sending emails

Here's my controller action.
public function actionContact()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
$headers="From: {$model->email}\r\nReply-To: {$model->email}";
mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers)`enter code here`;
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
$this->refresh();
}
}
$this->render('contact',array('model'=>$model));
}
It validates the data and shows up the success message. but email isn;t sent to the adminEmail address which is mine.
Thanks.
If it is validating and showing flash messages, then it has to do with mail server, nothing to do with yii. I advise you look into mail server and see if it is running properly.
if you are doing in a localhost you must configure your mail server.you can use smtp for sending mails from localhost.If it is in mail server no need to configure.In yout config/main.php you can do like this for localhost
'mail' => array(
'class' => 'application.extensions.yii-mail.YiiMail',
'transportType' => 'php',
'transportOptions'=>array(
'host'=>'yourhostname',
'port'=>'your port no'
),
'viewPath' => 'application.views.mail',
'logging' => true,
'dryRun' => false
),
Note: i am using YiiMail Extension

Is there no ways to retrieve bounce message from mailbox like yahoo, gmail?

I use Zend mail to read in the mail box ,
and i would like to filter out the bounce message from all other mail. So, below is the code i use to retrieve the header
The problem are:
1) Is gmail, yahoo etc.. not sending the actual bounce message? I can not detect any failed-receipent, diagnostic code from header
2) If not, then if i would like to notify the user what are the fail message, all i can do is only check whether there is a 'fail' or some similar phase in header, and using mail reg- exp to detect the fail recipient mail in the content?
Thank you for helping.
<?
set_include_path($_SERVER['DOCUMENT_ROOT'].'/fyp/plugin');
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
Zend_Loader::loadClass('Zend_Mail');
$mail = new Zend_Mail_Storage_Imap(array('host' => 'imap.gmail.com',
'user' => 'fff#gmail.com',
'password' => 'ffff',
'ssl' => 'SSL',
'port' => 993
));
$substring="Return-Path: <>";
foreach ($mail as $message) {
foreach ($message->getHeaders() as $name => $value) {
if (is_string($value)) {
echo "$name: $value\n";
echo "<br>";
}
}
}
?>

email sending from localhost in cakephp using emailcomponent

<?php
class EmailsController extends AppController
{
var $uses=null;
var $components=array(
'Email'=>array(
'delivery'=>'smtp',
'smtpOptions'=>array(
'host'=>'ssl://smtp.google.com',
'username'=>'username#gmail.com',
'password'=>'password',
'port'=>465
)
));
function sendEmail() {
$this->Email->to = 'Neil <neil6502#gmail.com>';
$this->Email->subject = 'Cake test simple email';
$this->Email->replyTo = 'neil6502#gmail.com';
$this->Email->from = 'Cake Test Account <neil6502#gmail.com>';
//Set the body of the mail as we send it.
//Note: the text can be an array, each element will appear as a
//seperate line in the message body.
if ( $this->Email->send('Here is the body of the email') ) {
$this->Session->setFlash('Simple email sent');
} else {
$this->Session->setFlash('Simple email not sent');
}
$this->redirect('/');
}
}
?>
above code is my controller responsible for sending emails...
but when i run this function sendEmail() using url http://localhost/authentication/emails/sendemail it shows nothing not even single error or any response... complete blank page. I don't know the reason.
I think I had the same issue a while ago. It might be that you need to change the to address into a value that holds just the address, so instead of Name <email#example.com> you should use email#example.com.
You can check for errors by logging (or debugging) the smtp errors with:
$this->log($this->Email->smtpError, 'debug');
or
debug($this->Email->smtpError);
Good luck. Hope this helps.
/* Auf SMTP-Fehler prüfen */
$this->set('smtp_errors', $this->Email->smtpError);
I would add the Email Config to your email.php file located in /app/Config/email.php , if it doesn't exist copy email.php.default to email.php, Change the smtp settings there
public $smtp = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret'
);
At the top of your Controller above class EmailsController extends AppController add,
App::uses('CakeEmail', 'Network/Email');
Then in your function sendEmail() try,
$Email = new CakeEmail();
$Email->from(array('me#example.com' => 'My Site'))
->to('you#example.com')
->subject('About')
->send('My message');
To test emails what I usually do is send them to the Cake Logs,
**In /app/Config/email.php, include: ( The log output should be /app/tmp/logs/debug.log )
public $test = array(
'log' => true
);
Also doing this add 'test' to your $Email variable like,**
$Email = new CakeEmail('test');
Actually in my case : I got a error message "SMTP server did not accept the password."
After that i follow the below link and issue has been resolved :
Step1 : https://blog.shaharia.com/send-email-from-localhost-in-cakephp-using-cakeemail/
Step2 : Sending Activation Email , SMTP server did not accept the password