I am creating a function to send a notification email to a user using the phpMailer lib.
public function notify($address,$subject = '',$body = null,$mailer_options = array()) {
try {
$phpmailer = new PHPMailer($exceptions = true);
$phpmailer->CharSet = 'UTF-8';
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;
$phpmailer->SMTPKeepAlive = true;
//$phpmailer->SMTPDebug = true;
$phpmailer->IsHTML(true);
$phpmailer->Host = ...
$phpmailer->Username = ...
$phpmailer->Password = ...
$phpmailer->From = ...
$phpmailer->FromName =...
$phpmailer->AddAddress($address);
$phpmailer->Subject = $subject;
$phpmailer->Body = $body;
$phpmailer->Send();
$phpmailer->ClearAllRecipients();
}
It works fine if i just send an email or sending multiple emails inside the class.
But if do
for($i=0;$i<3;$++)
{
$notification = new $Notification();
$notification->notify(...);
}
It retuns a blank page. No errors, messages, nothing.
Before you ask i have display_errors turned on.
What can it be?
It works fine if i just have one instance of phpmailer like this:
$phpmailer = new PHPMailer($exceptions = true);
(...)
for($i=0;$i<3;$i++)
{
$phpmailer->AddAddress('address');
$phpmailer->Subject = "";
$phpmailer->Body = "sasa";
$phpmailer->Send();
$phpmailer->ClearAllRecipients();
}
Remove the $ from new Notification:
for($i=0;$i<3;$++)
{
$notification = new Notification();
$notification->notify(...);
}
new $Notification will create a new instance from the value of variable $Notification.
That would only work if $Notification really contains "Notification" (assuming your class is named "Notification")
If you've turned display_errors on in your PHP script, but the server has disabled it by default, errors won't be displayed if there is a syntax error in your script.
Related
I'm writing a hook using the ProcessWire API... pretty common practice with it's powerful API.
The below works completely fine...
$this->addHookAfter('Pages::saved', function(HookEvent $event) {
$arguments = $event->arguments();
$page = $event->arguments(0);
if ($page->template == 'user') {
// Require relevent libraries
require_once($this->config->paths->root . 'api/sendgrid/sendgrid-php.php');
// SendGrid API init
$sgAPIKey = "XXXX";
// Set email confirmation settings
$email_admin = 'test#example.com';
$email_customer = $page->email;
$email_admin_subject = "You added a new user $page->name";
$email_customer_subject = 'Your login details';
$from = new \SendGrid\Email("Example User", $email_admin);
$subject = "Sending with SendGrid is Fun";
$to = new \SendGrid\Email("Example User", $email_customer);
$content = new \SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new \SendGrid\Mail($from, $subject, $to, $content);
$sg = new \SendGrid($sgAPIKey);
$response = $sg->client->mail()->send()->post($mail);
// Dump SendGrid object with TracyDebugger
bd($mail);
}
});
However, as soon as I add a function to send the email (in order to set up two separate send mail functions (one to admin, one to customer) it doesn't work at all. No errors... just $mail returns NULL.
$this->addHookAfter('Pages::saved', function(HookEvent $event) {
$arguments = $event->arguments();
$page = $event->arguments(0);
if ($page->template == 'user') {
// Require relevent libraries
require_once($this->config->paths->root . 'api/sendgrid/sendgrid-php.php');
// SendGrid API init
$sgAPIKey = "XXXX";
// Set email confirmation settings
$email_admin = 'test#example.com';
$email_customer = $page->email;
$email_admin_subject = "You added a new user $page->name";
$email_customer_subject = 'Your login details';
$email_customer_body = 'This is a test';
function send_email($from_email, $to_email, $subject, $body) {
global $sgAPIKey;
$from = new \SendGrid\Email(null, $from_email);
$to = new \SendGrid\Email(null, $to_email);
$content = new \SendGrid\Content("text/html", $body);
$mail = new \SendGrid\Mail($from, $subject, $to, $content);
$sg = new \SendGrid($sgAPIKey);
$response = $sg->client->mail()->send()->post($mail);
}
send_email($email_admin, $email_customer, $email_customer_subject, $email_customer_body);
// Dump SendGrid object with TracyDebugger
global $mail;
bd($mail);
}
});
Is there any reason why a function like this wouldn't work? Is it because technically there's a function inside a function? I would've at least have thought it would've returned an error.
Functions inside of functions are allowed in PHP, however the issue that you are experiencing is more of an issue with scoping. $mail is no longer in scope after that function is completed, meaning, among other things, that you can no longer access that variable.
One possible solution is to return that variable when the function completes, like so:
function send_email($from_email, $to_email, $subject, $body) {
global $sgAPIKey;
$from = new \SendGrid\Email(null, $from_email);
$to = new \SendGrid\Email(null, $to_email);
$content = new \SendGrid\Content("text/html", $body);
$mail = new \SendGrid\Mail($from, $subject, $to, $content);
$sg = new \SendGrid($sgAPIKey);
$response = $sg->client->mail()->send()->post($mail);
return $mail;
}
That way, when you execute the function, you can set it to a variable.
As an aside, I noticed you attempted to use the global keyword to access that $mail variable. Not only is that typically frowned upon as bad practice, it would never work in this context. The global keyword is used to make variables that are in the global namespace accessable. The typical use is as follows:
$global_variable = "foobar";
class Foo {
public static function test() {
return $global_variable;
}
public static function test_two() {
global $global_variable;
return $global_variable;
}
}
echo(Foo::test()); //echoes nothing, since in scope, the variable is not set
echo(Foo::test_two()); //echoes 'foobar', since we told PHP to put it in scope
TLDR: The global keyword makes variables that are in the global scope, be visible in the local scope.
I am using PHPmailer with Gmail API to send out mail. This process has worked very well for me for sending just standard emails, however, I want to also have the ability to send out an email with attachments using the Gmail API. When I tried using $mail->addAttachment($urlString, $name); Gmail would come back with the error Request Entity Too Large Error 413 (The size of the attachments never goes above 20MB so it should be well within the 35MB limits of Gmail API).
After some searching, I found out it was because I wasn't using "/Upload URI" for sending large Gmail Attachments (Anything above 5mb and below 35mb). Problem is, I am not very knowledgeable on how to work the Gmail API and only got what I have now from basically copying code from somewhere else and slightly modifying it for my uses and as such, I have no idea how to change the URI like that.
Here is what I have so far, that works with standard emails:
function($agent, $assistantName='', $assistantEmail='', $subject, $body, $attachments=''){
$key = realpath(dirname(__FILE__).'/Services/Google/Gmail.json');
$useremail = 'myuseremail#example.com';
$toAddress = $agent->email;
$agentFirst = $agent->first_name;
$client = new Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$key);
$client->useApplicationDefaultCredentials();
$user_to_impersonate = $useremail;
$client->setSubject($user_to_impersonate);
$client->addScope('https://www.googleapis.com/auth/gmail.compose');
if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
//prepare the mail with PHPMailer
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->Encoding = "base64";
$subject = $subject;
$msg = $body;
$from = $useremail;
$fname = "My Name";
$mail->From = $from;
$mail->FromName = $fname;
$mail->AddAddress($toAddress, $agentFirst);
$mail->AddCC($assistantEmail, $assistantName);
$mail->AddReplyTo($from,$fname);
if ($attachments != '') {
foreach ($attachments as $name => $urlString) {
$mail->addAttachment($urlString, $name);
}
}
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->IsHTML(true);
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$m = new Google_Service_Gmail_Message();
$data = base64_encode($mime);
$data = str_replace(array('+','/','='),array('-','_',''),$data); // url safe
$m->setRaw($data);
$service = new Google_Service_Gmail($client);
$email = $service->users_messages->send($useremail, $m);
return json_encode($email);
}
I don't really know where to go from here, so any help would be appreciated.
use EZCMail and build the email structure yourself... it is very pickey! I can post some details after.
you may also have to send the email in chunks.
If the email is over 4MB then you are going to have to send in chunks using Google_Http_MediaFileUpload
Your code using this should be something similar to this, there is examples for using Google_Http_MediaFileUpload elsewhere on the web which might be more complete:
$client->setDefer(true);
// Call the API with the media upload, defer so it doesn't immediately return.
$arrRequestParams = $arrRequestParams+['uploadType' => 'multipart'];
$result = $this->TransportPrepSwitch($strAction, $objGMessage, $arrRequestParams); // Make draft or message $service->users_messages->send('me', $objGMessage, $arrRequestParams);
$mailMessage = base64url_decode($strRaw);
// create mediafile upload
$chunkSizeBytes = 1*1024*1024; // send to google in 1MB chunks
$media = new Google_Http_MediaFileUpload($client,$result,'message/rfc822',$mailMessage,true,$chunkSizeBytes);
$media->setFileSize(strlen($mailMessage));
// Upload chunks. Status will contain the completed $result.
$status = false;
set_time_limit(300);
while(!$status)
$status = $media->nextChunk();
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);
$objResponce = $status;
Also the structure of the email parts MUST be as follows:
multipart/mixed => [
multipart/related => [
multipart/alternative => [
plain,
html
],
inline images,
],
attachments,
]
The only way I could achieve this was using EZCMail to build the email part by part.
I need to send mail to the admin with the inserted data using APi function ,
the function is look like that
public function requestbookingresort_post()
{
$languageid = $this->input->post('languageId');
$resort_id = $this->input->post('resortId');
$booking_from = $this->input->post('bookingFrom');
$booking_to = $this->input->post('bookingTo');
$contact_name = $this->input->post('contactName');
$contact_email = $this->input->post('contactEmail');
$contact_phone = $this->input->post('contactPhone');
$userid = $this->input->post('userId');
if (empty($languageid))
{
$languageRecord = getDefaultlanguage();
$languageid = $languageRecord->languageid;
}
$language_file = languagefilebyid($languageid);
$this->lang->load($language_file, $language_file);
if (empty($resort_id) || empty($booking_from) || empty($booking_to) || empty($contact_name) || empty($contact_email) || empty($contact_phone))
{
$arr['status'] = 'error';
$arr['statusMessage'] = lang('error_in_booking');
$arr['data'] = array();
}
else
{
$dataArray = array(
"languageid" => $languageid,
"userid" => empty($userid) ? "0" : $userid,
"resortid" => $resort_id,
"bookingfrom" => date("Y-m-d", strtotime($booking_from)),
"bookingto" => date("Y-m-d", strtotime($booking_to)),
"contactname" => $contact_name,
"contactemail" => $contact_email,
"contactphone" => $contact_phone,
"requestdatetime" => date("Y-m-d H:i:s"),
);
$this->load->model("Resort_model");
$booking_id = $this->Resort_model->saveBookingRequest($dataArray);
if (empty($booking_id))
{
$arr['status'] = 'error';
$arr['statusMessage'] = lang('error_occurred');
$arr['data'] = array();
}
else
{
$arr['status'] = 'success';
$arr['statusMessage'] = lang('booking_request_submit');
$arr['data'] = array();
}
}
$response = array(
"response" => $arr
);
$this->set_response($response, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
}
But i'm new at codeigniter and didn't know how to get this passed data from the database to send mail with that to the admin mail or something ?
Try this.
public function requestbookingresort_post()
{
// Your operations
$response = array(
"response" => $arr
);
$this->sendMail($response)
$this->set_response($response, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
}
public function sendMail($response)
{
$settings=$this->Some_model->getEmailSettings();
$mail = new PHPMailer();
$mail->IsSMTP(); // we are going to use SMTP
$mail->SMTPAuth = true; // enabled SMTP authentication
$mail->SMTPSecure = "ssl"; // prefix for secure protocol to connect to the server
$mail->Host = $settings->host; // setting GMail as our SMTP server
$mail->Port = $settings->port; // SMTP port to connect to GMail
$mail->Username = $settings->email; // user email address
$mail->Password = $settings->password; // password in GMail
$mail->SetFrom($settings->sent_email, $settings->sent_title); //Who is sending the email
$mail->AddReplyTo($settings->reply_email,$settings->reply_email); //email address that receives the response
$mail->Subject = "Your Booking has been confirmed";
$mail->IsHTML(true);
$body = $this->load->view('path/email_template', $response, true);
$mail->MsgHTML($body);
$destination = $response['contactEmail']; // Who is addressed the email to
$mail->AddAddress($destination);
if(!$mail->Send()) {
$data['code']=300;
$data["message"] = "Error: " . $mail->ErrorInfo;
}
}
Make sure you have PHPMailer in your libraries and you are loading the library in your constructor and I hope you are keeping Email settings in your database. If not you can manually provide host, port, username and password fields
I'm using sendgrid to send out html emails from a document system I'm building. When a file uploads sendgrid needs to email all those associated with that case. I have everything working for individual emails and I can customs the template I have saved but cant send the email to multiple recipients
I have generated an array of recipients
$email = array(j.bloggs#bloggs.com, j.doe#me.net, d.smith#smith.co.uk);
I want to pass this into my sendgrid email object to send to them all
private function send() {
$sg = new \SendGrid(self::$key);
$response = $sg->client->mail()->send()->post(self::$mail);
return $response->statusCode();
}
public function file_saved($file="", $case="") {
self::$from = new SendGrid\Email($this->fromName, $this->fromEmail);
self::$to = new SendGrid\Email($this->toName, $this->toEmail);
self::$content = new SendGrid\Content("text/html", "Hello, Email!");
self::$mail = new SendGrid\Mail(
self::$from,
$this->subject,
self::$to,
self::$content
);
$str = "<p>A file has been successfully uploaded to {$case->case_name} ({$case->case_code}).</p>
<br />
<p>{$file->file_name} - ".size($file->file_size)."</p>";
self::$mail->personalization[0]->addSubstitution("-name-", $this->toName);
self::$mail->personalization[0]->addSubstitution("-str-", $str);
self::$mail->personalization[0]->addSubstitution("-btn-", "Download File");
self::$mail->personalization[0]->addSubstitution("-url-", HTTP.BASE_URL.DS.'uploads'.DS.$file->file_path);
self::$mail->setTemplateId("2f845487-6243-4562-b6fb-022185b7fde7");
if (!$this->send() == 202) {
return false;
}
else {
return true;
}
}
I tried to use the personalization->to and pass that the array but get the error
Call to a member function to() on null in includes/classes/mail.php on line <b>82</b><br />
I just created a loop that went through each recipient and sent them an email, not as efficient as generating a multiple to: list but for now its working until I get a response from the sendgrid dev team
$this->email = array(j.bloggs#bloggs.com, j.doe#me.net, d.smith#smith.co.uk);
private function send() {
$sg = new \SendGrid(self::$key);
$response = $sg->client->mail()->send()->post(self::$mail);
return $response->statusCode();
}
public function file_saved($file="", $case="") {
$str = "<p>A file has been successfully uploaded to {$case->case_name} ({$case->case_code}).</p>
<br />
<p>{$file->file_name} - ".size($file->file_size)."</p>";
self::$from = new SendGrid\Email($this->fromName, $this->fromEmail);
self::$content = new SendGrid\Content("text/html", "Hello, Email!");
foreach($this->email as $email_addr) {
self::$to = new SendGrid\Email($this->toName, $email_addr);
self::$mail = new SendGrid\Mail(
self::$from,
$this->subject,
self::$to,
self::$content
);
self::$mail->personalization[0]->addSubstitution("-str-", $str);
self::$mail->personalization[0]->addSubstitution("-btn-", "Download File");
self::$mail->personalization[0]->addSubstitution("-url-", HTTP.BASE_URL.DS.'uploads'.DS.$file->file_path);
self::$mail->setTemplateId("2f845487-6243-4562-b6fb-022185b7fde7");
if (!$this->send() == 202) {
$response[$address] = false;
}
else {
$response[$address] = true;
}
}
return $response; // contains true/false for each email address if sent or not.
}
I set up a component named CorreoComponent for sending mail in cakephp. It uses PHPMailer to send the mail, but it's not sending the mail. Strange thing is, apparently the send() function is sending the mail, because I have a condition like this `$result = $this->Correo->send();
if($result) {
//echo "Correo enviado";
$this->Session->setFlash(__('Gracias por enviarnos tus datos de contacto, nos comunicaremos contigo en breve', true));
}
else {
$this->Session->setFlash(__('Ha ocurrido un error al enviar el correo. Lamentamos el inconveniente.', true));
//echo "No se pudo enviar el correo";
}
$this->redirect(array('controller'=>'propiedades', 'action'=>'index'));` in the controller that sends the email. So this condition checks if the Send() method of PHPMailer class is sending, and it returns the message "Gracias por enviarnos tus datos de contacto, nos comunicaremos contigo en breve" alright. But I do not see any mail in the mailbox (I set it up so that it sends messages to another account of mine), I don't see any message in the sent mailbox of the account that's sending it (I use smtp.gmail as the host). Can somebody tell me what I'm missing here, please?
The complete component class:
<?php
/**
* This is a component to send email from CakePHP using PHPMailer
* #link http://bakery.cakephp.org/articles/view/94
* #see http://bakery.cakephp.org/articles/view/94
*/
App::uses('Component', 'Controller');
class CorreoComponent extends Component
{
/**
* Send email using SMTP Auth by default.
*/
var $from = 'informes#compraventarenta.com.mx';
var $fromName = "Informes en CompraVentaRenta.com.mx";
var $sitePrefix = 'compraventarenta';
var $useSMTPAuth = true;
var $smtpSecure = 'ssl';
var $smtpPort = 465;
var $smtpUserName = 'gerardo.v.flores#gmail.com';
var $smtpPassword = '*************';
var $smtpHostNames = "smtp.gmail.com";
var $text_body = null;
var $html_body = null;
var $to = null;
var $toName = null;
var $subject = null;
var $cc = null;
var $bcc = null;
var $template = 'email/default';
var $attachments = null;
var $controller;
function startup(Controller $controller)
{
$this->controller = &$controller;
}
/**
* Helper function to generate the appropriate template location
*
* #return string CakePHP location of the template file
* #param object $template_type
*/
function templateLocation($template_type)
{
return ('..'.DS.strtolower($this->controller->name).DS.$this->template.$template_type);
}
/**
* Renders the content for either html or text of the email
*
* #return string Rendered content from the associated template
* #param object $type_suffix
*/
function bodyContent($type_suffix)
{
$temp_layout = $this->controller->layout; // store the current controller layout
if ($type_suffix == 'html')
$this->controller->layout = 'custom';
else
$this->controller->layout = '';
$mail = $this->controller- >render($this>templateLocation('_'.strtolower($type_suffix)));
// render() automatically adds to the controller->output, we'll remove it
$this->controller->output = str_replace($mail, '', $this->controller->output);
$this->controller->layout = $temp_layout; // restore the controller layout
return $mail;
}
function send()
{
App::import('Vendor', 'PHPMailer', array ('file'=>'phpmailer'.DS.'class.phpmailer.php'));
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = $this->useSMTPAuth;
$mail->SMTPSecure = $this->smtpSecure;
$mail->Host = $this->smtpHostNames;
$mail->Username = $this->smtpUserName;
$mail->Password = $this->smtpPassword;
$mail->From = $this->from;
$mail->FromName = $this->fromName;
$mail->AddAddress($this->to, $this->toName);
$mail->AddReplyTo($this->from, $this->fromName);
$mail->CharSet = 'UTF-8';
$mail->WordWrap = 80; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $this->sitePrefix.' '.$this->subject;
$mail->AltBody = $this->bodyContent('text');
$mail->Body = $this->bodyContent('html');
$result = $mail->Send();
if ($result == false)
$result = $mail->ErrorInfo;
return $result;
}
}
?>
So, if I don't do the redirect on the controller, I see the view defined in default_html.ctp on the browser, which I think it's fine. But maybe there's something wrong in the way I render the controller output. I don't know and I'm new to cake. Can someone help?
Your controller code cannot properly tell whether sending was successful, as your EmailComponent::send() method returns PHPMailer::$ErrorInfo on failure, ie it might return a string, and as long as that string isn't empty or '0' (in case of an error it will usually contain an error message), your if($result) will evaluate to true.