Hi we have a CRM System that needs to change SMTP server once updated by the admin.
We have a table that returns a set of data that contains the SMTP Settings that has a status of 1 needed. The problem is it still relays to the localhost smtp even i've sent the SMTP settings to the htmlMimeMail.php (class) Here is my code:
function sendEmail($txtFrom, $subject, $to, $cc = NULL, $bcc = NULL, $strHTML, $txtModel='')
{
$sql = "SELECT * FROM smtp_server WHERE status='1'";
$rstemp = $this->cn2->Execute($sql);
while(!$rstemp->EOF)
{
$SMTPid = $rstemp->fields['id'];
$SMTPServer = $rstemp->fields['server'];
$SMTPPort = $rstemp->fields['port'];
$SMTPusername=$rstemp->fields['user'];
$SMTPpass=$rstemp->fields['pass'];
$SMTPauth = $rstemp->fields['auth'];
$rstemp->MoveNext();
}
$rstemp->Close();
$fromEmail = $txtFrom;
$from = $fromEmail;
$mail = new htmlMimeMail();
$mail->setTextCharset('utf-8');
$mail->setHtmlCharset('utf-8');
$mail->setHeadCharset('utf-8');
$mail->setSMTPParams($SMTPServer, $SMTPPort,$SMTPid,base64_encode($SMTPusername),base64_encode($SMTPpass),$SMTPServer);
$mail->setReturnPath($fromEmail);
$mail->setFrom($from);
$mail->setSubject($subject);
$mail->setHTML($strHTML);
if(trim($txtModel) != '')
{
$file = strtoupper($txtModel).".pdf";
if(file_exists($this->dir.$file))
{
$attachment = $mail->getFile($this->dir.$file);
$mail->addAttachment($attachment, $file, "application/pdf");
}
}
if (!is_null($cc) && $cc != '')
{
//$arrEmail = explode(",", $cc);
$mail->setCc($cc);
//unset($arrEmail);
}
$mail->setBcc($bcc.', sample#email.com');
$arrEmail = explode(",", $to);
ini_set("SMTP",$SMTPServer);
$result = $mail->send($arrEmail);
return $result;
}
Am i missing something in my code?
I also added ini_set
Here is the function that receives the parameters.
function setSMTPParams($host = null, $port = null, $auth = null, $user = null, $pass = null,$helo = null)
{
if (!is_null($host)) $this->smtp_params['host'] = $host;
if (!is_null($port)) $this->smtp_params['port'] = $port;
if (!is_null($helo)) $this->smtp_params['helo'] = $helo;
if($auth == 4){
if (!is_null($auth)) $this->smtp_params['auth'] = true;
}else{
if (!is_null($auth)) $this->smtp_params['auth'] = false;
}
if (!is_null($user)) $this->smtp_params['user'] = $user;
if (!is_null($pass)) $this->smtp_params['pass'] = $pass;
}
Table definition
________________________________________
id|server |port|user|pass|auth |status
________________________________________
1 |localhost |25 |null|null|false|0
4 |somesmtp#mail.com|25 |user|pass|true |1
________________________________________
From what I can see, you sent the SMTP data to the class, but you neglected to tell the class to use that data:
$result = $mail->send( $a_to, 'smtp' )
Failing to set the second parameter in send results in the class defaulting to 'mail' as send type.
function send($recipients, $type = 'mail')
which then results in your system using the default PHP:
$result = mail($to, $subject, $this->output, implode(CRLF, $headers));
in the:
case 'mail':
of function send()
I'm not clear on why the setup resorted to the SMTP defaults that are set when htmlMimeMail class is launched, there may be some further modification of the code that creates that result, I can't say.
I realize this question is a bit old and the current htmlMimeMail won't run without errors on current PHP anyway, but figured might as well give an answer.
Related
I am using PHPmailer version 5.2.22 and losing some part of string from FromName.
For e.g.
Mail sent with FromName : "CONC, abcø"
Outputs FromName : "abcø"
String part "CONC," is removed from FromName of mail received.
Code sample :
<?php
$email = 'waleedAhmed#mymail.com';
$name = 'waleed';
$event_info[0]['organizer_name'] = 'CONC, abcø';
$subject = 'Testing..';
$content = ' content : '. $event_info[0]['organizer_name'];
$objName = new \PHPMailer();
$objName->CharSet = 'UTF-8';
$objName->Mailer = "smtp";
$objName->Host = "mail.mymail.com";
$objName->Port = 25;
$objName->Mailer = "sendmail";
$objName->From = 'donotreply#mymail.com';
$objName->Sender = 'donotreply#mymail.com';
$objName->FromName = $event_info[0]['organizer_name']
$objName->IsHTML(true);
$objName->Body = $content;
$objName->Send();
unset($objName);
// ' \"CONC, abcø\" < donotreply#mymail.com > ';
$objName->FromName = '\"'.$event_info[0]['organizer_name'].'\" < donotreply#mymail.com >';
Working fine.
I need to send order confirmation SMS message to my users via my SMS gateway API in PrestaShop. Is that possible?
I am using the ValueFirst SMS gateway.
Here is my code:
<?php
$username="username";
$password="password";
$to="mobilenumber";
$sender="senderid";
$message="message";
if($username!='' && $password!='' && $to!='' && $sender!='' && $message!='')
{
class sendSms
{
var $serverURL = 'http://api.myvaluefirst.com/psms/servlet/psms.Eservice2';
var $gsmSender = 'MYGSMID';
function GetSenderID()
{
return $this->cdmaNumber;
}
function get_address($user_id,$to)
{
$address_info = sprintf('<ADDRESS FROM="%s" TO="%s" SEQ="%s" />',$user_id,$to,1);
return $address_info;
}
function postdata($url,$data)
{
$objURL = curl_init($url);
curl_setopt($objURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($objURL,CURLOPT_POST,1);
curl_setopt($objURL, CURLOPT_POSTFIELDS,$data);
$retval = trim(curl_exec($objURL));
curl_close($objURL);
return $retval;
}
function sendSmsToUser($prefix='',$contents='', $toAddr='',$sms_user_name='',$sms_password='',$sms_user_id='' )
{
$xmlstr ='<!DOCTYPE MESSAGE SYSTEM "http://127.0.0.1/psms/dtd/message.dtd"><MESSAGE><USER USERNAME="%s" PASSWORD="%s"/><SMS UDH=" 0 " CODING=" 1 " TEXT="%s" PROPERTY=" 0 " ID=" 2 ">%s</SMS></MESSAGE>';
$contents = stripslashes($prefix.$contents);
$contents = htmlentities($contents,ENT_COMPAT);
$username = stripslashes($sms_user_name);
$username = htmlentities($username,ENT_COMPAT);
$password = stripslashes($sms_password);
$password = htmlentities($password,ENT_COMPAT);
$user_id = stripslashes($sms_user_id);
$user_id = htmlentities($user_id,ENT_COMPAT);
$xmldata = sprintf($xmlstr,$username,$password,$contents,$this->get_address($user_id,$toAddr));
$data='data='. urlencode($xmldata);
$action='action=send';
$str_response = $this->postdata($this->serverURL,$action.'&'.$data);
$str_request = $this->serverURL.'?'.$action.'&'.$data;
}
}
$mclass = new sendSms();
$mclass->sendSmsToUser("Dear Guest,",$message,$to,$username,$password,$sender);
}
?>
This is my ValueFirst API script. I want to use this script into PrestaShop. How can I do that?
I think this is kinda duplicate with this one : How to integrate msg91 php api with Prestasms or Prestashop?
For instance, create a module at https://validator.prestashop.com/ then add Hooks (function triggered at a certain time automaticly, like on order update) with it.
my RESTapi is outputting following json string
{"chat":[{"id":100,"chat_id":38,"created_at":"2016-09-07 08:48:17","updated_at":"2016-09-07 08:48:17","messageContent":"Hi there","sender":"client"},{"id":101,"chat_id":38,"created_at":"2016-09-07 08:48:29","updated_at":"2016-09-07 08:48:29","messageContent":"hello sir","sender":"admin"},{"id":102,"chat_id":38,"created_at":"2016-09-07 09:14:24","updated_at":"2016-09-07 09:14:24","messageContent":"test","sender":"client"},{"id":103,"chat_id":38,"created_at":"2016-09-07 09:16:06","updated_at":"2016-09-07 09:16:06","messageContent":"test","sender":"client"}],"currentChatId":38,"senderName":"Client name"}
Notice the sender column. These are all coming from db. Now I am writing an algorithm which checks this and finds the sender name to the view instead of just sender type.
I am having problem regarding this..any suggestion?
Here is the controller function
public function getAllChat(Request $request)
{
// $chatList = chatMessage::where();
$clientId = $request->session()->get('userId');
//current chat id7
$currentChatId = $request->session()->get('currentChatId');
//find who sent it admin/client
$chatMessageList = chatMessage::where('chat_id',$currentChatId)->get();
//sender
foreach($chatMessageList as $cht)
{
//find out sender
$sender = $cht->sender;
if($sender=="client")
{
$chtP = chatParticipants::find($currentChatId)->first();
$clientId = $chtP->client_id;
//find client name
$client = Client::find($clientId);
$ch->sender = $client->name;
}
elseif($sender=="admin")
{
$chtP = chatParticipants::find($currentChatId)->first();
$adminId = $chtP->admin_id;
//find client name
$admin = Admin::find($clientId);
$name = $admin->name;
$ch->sender = $admin->name;
}
}
return response()->json([
"chat"=> $chatMessageList,
"currentChatId" => $currentChatId,
"senderName"=>$name
]);
}
Error : Creating default object from empty value empty value
You don't have $ch variable defined anywhere - I guess you wanted to use $cht variable instead.
Replace
$ch->sender = $client->name;
with
$cht->sender = $client->name;
and
$ch->sender = $admin->name;
with
$cht->sender = $admin->name;
It was a typo.
$cht->sender=$name;
instead of
$ch->sender=$name;
I am using codeigniter-restserver by Phil Sturgeon,
https://github.com/chriskacerguis/codeigniter-restserver
here is an problem I encountered:
When I do PUT request, everything works, but when I do POST, I got
"500 Internal Server Error"
<div id="container">
<h1>An Error Was Encountered</h1>
<p>The action you have requested is not allowed.</p>
</div>
my code is the following:
function test_post()
{
$this->response('ok', 200); // 200 being the HTTP response code
}
function test_get()
{
$this->response('ok', 200); // 200 being the HTTP response code
}
the working GET handelling can be found in the following URL
https://manage.pineconetassel.com/index.php/api/example/test/
NOTE that I allowed https only.
I used hurl.it to test the POST method, and it does not work.
here is the rest.php config:
$config['force_https'] = TRUE;
$config['rest_default_format'] = 'json';
$config['rest_status_field_name'] = 'status';
$config['rest_message_field_name'] = 'error';
$config['enable_emulate_request'] = TRUE;
$config['rest_realm'] = 'REST API';
$config['rest_auth'] = false;
$config['auth_source'] = 'ldap';
$config['auth_library_class'] = '';
$config['auth_library_function'] = '';
$config['rest_valid_logins'] = array('admin' => '1234');
$config['rest_ip_whitelist_enabled'] = false;
$config['rest_ip_whitelist'] = '';
$config['rest_ip_blacklist_enabled'] = false;
$config['rest_ip_blacklist'] = '';
$config['rest_database_group'] = 'default';
$config['rest_keys_table'] = 'keys';
$config['rest_enable_keys'] = FALSE;
$config['rest_key_column'] = 'key';
$config['rest_key_length'] = 40;
$config['rest_key_name'] = 'X-API-KEY';
$config['rest_logs_table'] = 'logs';
$config['rest_enable_logging'] = FALSE;
$config['rest_access_table'] = 'access';
$config['rest_enable_access'] = FALSE;
$config['rest_logs_json_params'] = FALSE;
$config['rest_limits_table'] = 'limits';
$config['rest_enable_limits'] = FALSE;
$config['rest_ignore_http_accept'] = FALSE;
$config['rest_ajax_only'] = FALSE;
Did I do something wrong or use a wrong way to test the POST or I need to configure something?
set $config['csrf_protection'] = FALSE; in config/config.php
Note it is not uder config/rest.php
The accepted answer is right.
But if you don't want to disable $config['csrf_protection'] in case you need it for web form, you need to exclude the REST API URIs, for example, your REST url starts with /api/, simply set
$config['csrf_exclude_uris'] = array(
'api/[a-z0-9/_-]+'
);
It is also under config/config.php. I use regular expression to make it simple.
I am trying to change the filter status for the 'subscriptiontrack' using sendgrid. I think I am sending the headers incorrectly, but not totally sure. Working inside a symfony 1.4 framework.
First I create an object of the header settings
$hdr = new SmtpApiHeader();
$hdr->addFilterSetting('subscriptiontrack', 'enable', 0);
$hdr->as_string();
which sets the filter settings and encodes the string
Then I send it off the email class
sendTestEmail::sendEmail($contents, $mailFrom, $testGroup, $subject, $hdr);
SvaSmtpApiHeader.class.php
class SmtpApiHeader
{
function addFilterSetting($filter, $setting, $value)
{
if (!isset($this->data['filters'])) {
$this->data['filters'] = array();
}
if (!isset($this->data['filters'][$filter])) {
$this->data['filters'][$filter] = array();
}
if (!isset($this->data['filters'][$filter]['settings'])) {
$this->data['filters'][$filter]['settings'] = array();
}
$this->data['filters'][$filter]['settings'][$setting] = $value;
}
function asJSON()
{
$json = json_encode($this->data);
// Add spaces so that the field can be folded
$json = preg_replace('/(["\]}])([,:])(["\[{])/', '$1$2 $3', $json);
return $json;
}
function as_string()
{
$json = $this->asJSON();
$str = "X-SMTPAPI: " . wordwrap($json, 76, "\n ");
return $str;
}
}
myEmail.class.php
<?php
class sendTestEmail
{
public static function sendEmail($contents, $mailFrom, $mailTo, $subject, $sgHeaders = null, $attachments = null)
{
try {
/*
* Load connection for mailer
*/
$connection = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 465, 'ssl')->setUsername(sfconfig::get('app_sendgrid_username'))->setPassword(sfconfig::get('app_sendgrid_password'));
// setup connection/content
$mailer = Swift_Mailer::newInstance($connection);
$message = Swift_Message::newInstance()->setSubject($subject)->setTo($mailTo);
$message->setBody($contents, 'text/html');
// if contains SMTPAPI header add it
if (null !== $sgHeaders) {
$message->getHeaders()->addTextHeader('X-SMTPAPI', $sgHeaders);
}
// update the from address line to include an actual name
if (is_array($mailFrom) and count($mailFrom) == 2) {
$mailFrom = array(
$mailFrom['email'] => $mailFrom['name']
);
}
// add attachments to email
if ($attachments !== null and is_array($attachments)) {
foreach ($attachments as $attachment) {
$attach = Swift_Attachment::fromPath($attachment['file'], $attachment['mime'])->setFilename($attachment['filename']);
$message->attach($attach);
}
}
// Send
$message->setFrom($mailFrom);
$mailer->send($message);
}
catch (Exception $e) {
throw new sfException('Error sending email out - ' . $e->getMessage());
}
}
}
The email is getting sent properly, but the unsubscribe option is still showing up at the bottom. Is this an issue with the header object or a problem with encoding for the header? Is the variable is still an object when getting added to the headers?
You're misunderstanding how JSON encoding works. Let's take a look at your as_string method:
function as_string()
{
$json = $this->asJSON();
$str = "X-SMTPAPI: " . wordwrap($json, 76, "\n ");
return $str;
}
This would output something to the effect of:
X-SMTPAPI: { "filters": { "subscriptiontrack": { "settings": { "enable": 0 } } } }
You should note that this isn't valid JSON because it is prefixed with "X-SMTPAPI". Instead, you should be calling asJSON, but SwiftMailer doesn't know that.
Try switching the header line to:
$message->getHeaders()->addTextHeader('X-SMTPAPI', $sgHeaders->asJSON());
If that doesn't work, can you give us a dump of:
$headers = $message->getHeaders();
echo $headers->toString();
And have you thought about using the official PHP library instead? https://github.com/sendgrid/sendgrid-php