CakePHP 3 - How to check whether an Email has been delivered successfully? - cakephp

i am trying to sending mail using cakephp 3.0 .
My code is :
Email::configTransport('WebMail',[
'className' => 'Smtp',
'host' => $host,
'port' => $port,
'timeout' => 30,
'username' => $username,
'password' => $password,
'client' => null,
'tls' => null]
);
$transport = ['transport' => 'WebMail'];
$email = new Email($transport);
$email
->from([$username => $senderName])
->to($email_to)
->subject('Password Reset Code');
$response = $email->send('hello');
its working fine but the problem is how to check if the email was delivered successfully or not to the recipient.
if I debug $response variable I got the array of all mail related data.
Now how can I check if the email was delivered or not .

You can't... at least not reliably. All CakePHP can tell you is whether sending/queing the mail was successful, and depending on the transport (Smtp/Mail/...) that you're using you can get the last response received from the E-Mail server.
If sending/queing was unsuccessful, a \Cake\Network\Exception\SocketException exception will be thrown, so catch that if you want to evaluate this problem. Other than that there's no further information CakePHP/PHP can provide you with.
try {
$email->send();
} catch (\Cake\Network\Exception\SocketException $exception) {
// sending/queing failed
// the last response is available when using the Smtp transport
$lastResponse = $email->transport()->getLastResponse();
}
If applicable you could use a custom Smtp transport and implement requesting DSNs (Delivery status notifications, which you could then evaluate later on, however this isn't foolproof either, as notifications aren't guaranteed.
See also
API > \Cake\Mailer\Transport\SmtpTransport::getLastResponse()
API > \Cake\Mailer\Email::transport()

You'll need to add some form of tracking pixel to the email, or just use a transactional email service like Mandrill (MailChimp) or SendGrid...etc that will do this for you. You can then see whether they received it, and if they opened it...etc.

You can manually open the inbox of sending email to check if the mail was sent. If due to any reason the mail could not be sent, you'll receive a revert mail stating the problem.
Now, I know this is not a very efficient method so you need to keep these things in mind :-
1. You need to lower the security of your email.
2. You should not send codes (JavaScript etc) on email.
3. Keep in mind the size of content you are sending.
With these points checked you can assume your mail was sent. For a safer side you can check you mail once a week/month to see if all the mails were sent or if you got any error.

Related

Call to POST to https://graph.microsoft.com/v1.0/me/events not working

I tried search most of today and not finding the root of my issue. I registered an app and copied the ids as needed to make calls to the Microsoft Graph API.
On making a GET call to users/xxxxxxx-f192-4758-xxxx-3f1c27ee5ef9/events, I do get back all my events in the Calendar.
But the issue is, when making a POST call to me/events using the data below:
$calendarEventData = array
(
'subject' => 'Test event in my calendar',
'start' => array
(
'dateTime' => '2020-12-22T15:14:14.524Z',
'timeZone' => 'UTC'
),
'end' => array
(
'dateTime' => '2020-12-22T17:14:14.524Z',
'timeZone' => 'UTC'
),
'body' => array
(
'content' => 'This is some hard coded body content',
'contentType' => 'text'
)
);
I get back a HTTP/1.1 403 Forbidden returned for "https://graph.microsoft.com/v1.0/me/events". I have ensure that the API permissions for Calendars.ReadWrite does exist and consent has been granted too (even redid that to seee if that makes a difference)
Please advise where I am going wrong?
Thank you
The problem may be caused by you assign the permission Calendars.ReadWrite in Application type(but not Delegated type) and you use client credential flow to do authentication. As client credential flow doesn't require you input username and password, so the access token will not contain the user information. When you request the api https://graph.microsoft.com/v1.0/me/events, the api backend don't know who is me. So it shows 403 error message.
To solve this problem, you can use the object id of "me" to replace me in your request api. Like POST https://graph.microsoft.com/v1.0/{user object id}/events.

Sending Email Via CakePHP - Configuration email override the sender email

I am having problem on sending email via cake php 2.7. Well, the email gets forwarded to the desired email address but there is problem in getting sender email address, as I am getting configuration email address in the place of sender email address. I have problem in getting how to fix this issue. It would be grateful if anybody could suggest anything regarding the issue.
Some parts of code are:
pages controller
public function sendEmail($data) {
$email = new CakeEmail();
$email->config('gmail');
$email->emailFormat('html');
$email->sender($data['User']['email_address'],$data['User']['name']);
$email->from(array($data['User']['email_address'] => $data['User']['name']));
$email->to('configuration_email');
$email->replyTo($data['User']['email_address']);
$email->subject($data['User']['subject']);
$message = 'Name: '.$data['User']['name'].'<br> How Did You Hear: '. $data['User']['how_did_you_hear'].
'<br>Message: '.$data['User']['body'];
if ($email->send($message))
{
return true;
}else {
return false;
}
}
email.php
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'configuration_email',
'password' => 'password',
'transport' => 'Smtp'
);
'From' field is using configuration email instead of using $data['User']['email'].
Thank you in advance!
Gmail lets you send messages on behalf of an email address different from your Gmail address only if you already own that email account and it is properly registered in your Gmail account.
Instructions are described in Gmail: Send mail from a different address or alias:
I'm a Gmail or Google Apps user sending from an external address.
Click the gear in the top right .
Select Settings.
Click the Accounts and Import tab.
Under Send mail as, click Add another email address.
In the 'Email address' field, enter your name and alternate email address you own.
Enter the SMTP server (e.g. smtp.domain.com), your username on that domain, and your password for that account. You may also need to adjust your port setting or SSL setting (talk to your other ISP if you need this information).
Click Add account >>
Open your other account and either click the link in the message Gmail sent or enter the confirmation code in the Accounts and Import section of your Gmail settings.
If Gmail sent a verification email and you didn’t receive it, try checking your Spam or Bulk Mail folders for a message from account-verification-noreply#google.com to see if the email ended up in there.

Email is not going to deliever on yahoo server

I am using cakephp, and sending email through cakeemail with smtp. My hosting is on 1and1.com. Email is going to deliver on gmail but not on yahoo and hotmail.
Then I try the PHPMailer on same server and its email was going to deliver on gmail and hotmail as well. But unfortunatly I am unable to use PHPMailer with cakephp. I have try two tutorial but fails. One of them is here.
I will prefer to fix the problem with cakeemail, if some one can help regarding this.
Or if can get solution with PHPMailer for cakephp that is also ok.
Here is the my code
$email = new CakeEmail();
$email->smtp = array(
'port'=>'25',
'timeout'=>'30',
'host' => 'smtp.1and1.com',
'username'=>'quote#xxxxxx.com',
'password'=>'xxxxxx_',
'client' => 'smtp.1and1.com' ,
'transport' => 'Smtp'
);
$email->from(array('xxxx#a-xxxx.com' => 'A-Best Auto Parts Quote'));
$email->to("xxxx#yyy.com");
//$email->bcc("xxxx#yyy.com");
$email->subject('My Test Subject');
$email->emailFormat('html');
$body="the test message for email"
$email->send($body);

Sending Activation Email , SMTP server did not accept the password

I'm sending an email using CakePHP and I got an Error: SMTP server did not accept the password, along with an email in my inbox says that: sign-in attempt blocked! , we recently blocked a sign-in attempt to your Google Account.
Is that normal?
I'm using Xampp.
function sendActivationEmail($user_id)
{
Debugger::dump($user_id);
$user = $this->User->findById($user_id);
if ($user==false)
{
debug(__METHOD__." failed to retrieve User data for user.id: {$user_id}");
return false;
}
$this->set('username', $this->data['User']['username']);
$this->Email->to = $user['User']['email'];
$this->Email->subject = env('SERVER_NAME').'- Please confirm your email address';
$this->Email->from = 'laurent#gmail.com';
$this->Email->template = 'account_verification';
$this->Email->delivery = 'smtp';
$this->Email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'laurent#gmail.com',
'password'=>1234567
);
$this->Email->sendAs = 'text';
return $this->Email->send();
}
You need to allow "less secure" apps in your Google account settings:
https://www.google.com/settings/security/lesssecureapps
See this announcement as well http://googleonlinesecurity.blogspot.de/2014/04/new-security-measures-will-affect-older.html
You should implement OAuth2 instead of weakening the security!
Make sure that the field in 'username' => and 'password'=> is authenticate or valid. I had experienced the same issue and all I do is to go this link https://accounts.google.com/b/0/DisplayUnlockCaptcha and click the button 'Continue'.
make sure you're logged in in gmail using the 'username' and 'password' provided in your code. After that, try to send email again.
I was facing same issue. in my case Password was not so strong. It contains username in password. I change the password and try again. it works.
*The other thing you can do that you can use other email address with strong password. make sure captcha is and less secure is enabled. *
https://accounts.google.com/b/0/DisplayUnlockCaptcha

PHP mail() not sending, but no exception thrown

I'm using CakePHP to send an email. My controller code looks like:
if ($this->User->save($this->request->data)) {
$email = new CakeEmail();
$email->from(array('noreply#mydomain.com' => 'My Domain'));
$email->to($this->request->data['User']['email']);
$email->subject('My Domain Confirmation');
$email->replyTo('noreply#mydomain.com');
$email->sender('noreply#mydomain.com', 'My Domain');
$email->emailFormat('html');
$email->template('confirmation');
$email->send();
$email->viewVars(array(
'name' => $this->request->data['User']['username'],
'id' => $this->User->getLastInsertID(),
'code' => $this->request->data['User']['confirm_code']));
}
I also included at the top of this controller:
App::uses('CakeEmail', 'Network/Email');
If I print_r on $email->send(), I get:
Array
(
[headers] => From: My Domain
Reply-To: noreply#mydomain.com
X-Mailer: CakePHP Email
Date: Thu, 23 Feb 2012 00:40:00 -0800
Message-ID: <4f45fb60a0fc46cd926f305a32396397#mydomain.com>
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
[message] =>
Hi there,
Welcome to my site! While you can now vote on submissions and leave comments, your own submissions will be screened and not appear to the public until you click on the confirmation link below:
Click here to confirm your account
We hope to see you around and thanks for joining the community!
So it's obviously using my html email template and passing the right variables to it, and throwing no exceptions. So I decided to just do a basic mail() test within one of my view files e.g.:
$to = "mytestemail#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Which echoed "Mail Sent.", but nothing actually came to my mailbox. I checked my file in /var/spool/mail/root and the last email sent was on the same server on Jan. 9, 2012. So it's definitely worked before. I just recently upgraded to Cake 2.0, but this doesn't explain why plain ol' mail() isn't working.
What other debugging methods can I check to make sure it's not my server preventing the email from being sent?
PHP's mail() won't throw any exceptions. You need to check the return status. If that's false, then your MTA isn't accepting mail. Even if it returns true, that doesn't actually mean much of anything.
Take a look at the mail logs in /var/log/. Hopefully those can help you figure out more.
also check your firewall settings. sometimes it stops all outgoing smtp requests if not from specific sources.
The server you are running your Cake app on probably requires (SMTP) authentication before it allows you to send anything, which is a pretty common configuration.
Copy the app/Config/email.php.default file to app/Config/email.php and adjust it to match your setup (usually it's just localhost and you can use one of your mailbox logins for authentication).
Also see the book on this subject.

Resources