How do we add an attachment with mailgun ? Why can't I find how to do such a basic thing ? I've tried multiple recipes, and it never works.
For instance :
$msg = $mg->MessageBuilder();
$msg->setFromAddress($sender_email);
$msg->addToRecipient($to);
$msg->setSubject($subject);
$msg->setTextBody($body);
$msg->addAttachment('zzz.txt');
$files['attachment'] = array();
$files['attachment'][] = 'zzz.txt';
$mg->post("{$domain}/messages", $msg->getMessage(), $files);
Why the file zzz.txt is not included in the email ?
Why is there absolutely no clear instructions anywhere about how to do this ?
Related
I am currently trying to implement what should be a simple task and send a message to one and / or more users via Moodle.
In the process I found 2 guides, which confused me at first.
Messaging 2.0 and Message API. Both have some differences and I didn't understand if my plugin needs to register as a Message Producer or not.
Unfortunately, I just can't get my message to be sent. I logged in with the user and checked the mailbox, no message there. Any help is appreciated.
What I did:
Created message.php within my db - folder and inserted the following code:
<?php
defined('MOODLE_INTERNAL') || die();
$messageproviders = array (
'datenotification' => array (
)
);
After that I expanded my language file with this:
$string['messageprovider:datenotification'] = 'Reminder for a presentation';
Like the guide advised, I updated my plugin to insert my messageprovider in the table mdl_message_providers. Finally, I implemented the actual sending of the message.
$eventdata = new \core\message\message();
$eventdata->component = 'local_reminder'; // the component sending the message. Along with name this must exist in the table message_providers
$eventdata->name = 'datenotification'; // type of message from that module (as module defines it). Along with component this must exist in the table message_providers
$eventdata->userfrom = core_user::get_noreply_user(); // user object , no-reply
$eventdata->userto = $user; // user object from database
$eventdata->subject = 'Test message';
$eventdata->fullmessage = 'This is my test message';
$eventdata->fullmessageformat = FORMAT_PLAIN; // text format
$eventdata->fullmessagehtml = '<p>This is my test message</p>';
$eventdata->smallmessage = '';
$eventdata->courseid = $course_id; // This is required in recent versions, use it from 3.2 on https://tracker.moodle.org/browse/MDL-47162
$result = message_send($eventdata);
Debugging:
var_dump($eventdata); //-> All data is included
var_dump($result); // returns int(5), id of message, no error
Adding the comment as an answer for future reference
Check if the message is installed and enabled via site admin > messaging > notification settings or direct to yoursite/admin/message.php
I have a PhpMailer working code as follow: (short version)
(the variable already defined before hand)
// Sender and recipient settings
$mail->setFrom($pengirim_email, $pengirim_nama);
$mail->addAddress($untuk_email, $untuk_nama);
$mail->addReplyTo($pengirim_email, $pengirim_nama);
Next, I add multiple email address for CC mail:
$mail-->addCC('aaa#gmail.com','Abdul');
$mail-->addCC('bbb#gmail.com','Borat');
It work as expected.
Now since I'm planning that the email address will come from the SQL query, so for the time being I want to know how do I have to fill the SQL 'CarbonCopy' column table with multiple email addresses - by trying to make a "hardcoded" variable value. So I try like this as the replacement for the addCC above:
$tembusan="'aaa#gmail.com','Abdul';'bbb#gmail.com','Borat'"; //not working
$CC = explode(';', $tembusan); //not working
for ($i = 0; $i < count($CC); $i++) {$mail->addCC($CC[$i]);} //not working
But it throw me an error like this :
Error in sending email. Mailer Error: Invalid address: (cc):
'aaa#gmail.com','Abdul'
So I change the $tembusan into like this:
$tembusan="aaa#gmail.com,Abdul;bbb#gmail.com,Borat"; //not working
It gives me almost the same like the error before:
Error in sending email. Mailer Error: Invalid address: (cc):
aaa#gmail.com,Abdul
Next, I try also this kind of code :
$tembusan="'aaa#gmail.com','Abdul';'bbb#gmail.com','Borat'"; //not working
$CC = explode(';', $tembusan); //not working
foreach($CC as $CCemail){$mail->AddCC($CCemail;} //not working
And it also throw the same error:
Error in sending email. Mailer Error: Invalid address: (cc):
'aaa#gmail.com','Abdul'
If I echo the last code like this foreach($CC as $CCemail){echo $CCemail. '<br/>';}, it give me a result like this :
'aaa#gmail.com','Abdul'
'bbb#gmail.com','Borat'
In my real code, I have a valid email address. The email address in the code above is just for an example.
Where did I do wrong ?
PS
btw, if I remove the "name" for the email address:
$tembusan="aaa#gmail.com;bbb#gmail.com"; //working
$CC = explode(';', $tembusan); //working
foreach($CC as $CCemail){$mail->AddCC($CCemail;} //working
it runs as expected (but in the gmail, the CC name is aaa and bbb).
Please do a further explode . try
$tembusan="aaa#gmail.com,Abdul;bbb#gmail.com,Borat";
$CC = explode(';', $tembusan);
for ($i = 0; $i < count($CC); $i++) {
$DD = explode(',', $CC[$i]);
$mail->addCC($DD[0], $DD[1]);
}
Please note that I have removed the ' characters . (you may use str_replace of PHP to eliminate these characters)
I am using the OnlineCity SMPP client lib for sending SMS. It was working fine. But as per the new guideline of TRAI, we need to add the following new TLV parameters while sending SMS
group = smpp-tlv
name = EntityID
tag = 0x1400
type = octetstring
length = 30
smsc-id = ***
I tried this
// Prepare message
$ENTITY_ID = new SmppTag(0x1400, '****************');
$tags = array($ENTITY_ID);
$from = new SmppAddress($SMS_Params['senderid'],SMPP::TON_ALPHANUMERIC);
$to = new SmppAddress($SMS_Params['phone'],SMPP::TON_INTERNATIONAL,SMPP::NPI_E164);
$encodedMessage = utf8_encode($SMS_Params['message']);
// Send
$return_data = $smpp->sendSMS($from,$to,$encodedMessage,$tags);
I got the success response but didn't get any SMS. I checked with my smpp provider. They said that the additional TLV parameter is not there and that's why the SMS is not sent.
Do you guys have any idea, can we do it in my current code based on onlinecity library or should I do something else?.
You need to check if your octect strings are null terminated or not, by default the library is assuming it will be. So there is a variable $sms_null_terminate_octetstrings which needs to be reset if your provider does not end with null.
The above code change that Asterisk integrator has recommended says the same thing.
Rather than changing the code, if you can reset the flag based on your need, that should solve the problem.
For others who wanted to add new mandatory parameters should add like this using smpp-php library.
$tags = array(
new SmppTag(0x1400, your_pe_id),
new SmppTag(0x1401, your_template_id)
);
$message_id = $smpp->sendSMS($from, $to, $encodedMessage, $tags);
Remove "+(self::$sms_null_terminate_octetstrings ? 1 : 0)" from smppclient.class.php file
Actual Code :
$pdu = pack('a1cca'.(strlen($source->value)+1).'cca'.(strlen($destination->value)+1).'ccc'.($scheduleDeliveryTime ? 'a16x' : 'a1').($validityPeriod ? 'a16x' : 'a1').'ccccca'.(strlen($short_message)+(self::$sms_null_terminate_octetstrings ? 1 : 0))
Updated Code :
$pdu = pack('a1cca'.(strlen($source->value)+1).'cca'.(strlen($destination->value)+1).'ccc'.($scheduleDeliveryTime ? 'a16x' : 'a1').($validityPeriod ? 'a16x' : 'a1').'ccccca'.(strlen($short_message))
This is my second day messing with the PHP SDK, I've encountered a road block with uploading an attachment to an Estimate with my code. I wanted a PHP form where one can upload a file and shoot it into the QBO company; my first step was to first try and set a static variable to see if it works, my code look slike this:
$Estimate = $dataService->query("SELECT * FROM Estimate WHERE DocNumber in ('{$ID}')");
// Create a new IPPAttachable
$up = "http://www.somedomain.com/test.pdf";
$sendMimeType = "application/pdf";
$randId = rand();
$entityRef = new IPPReferenceType($Estimate->Id);
$attachableRef = new IPPAttachableRef($entityRef);
$objAttachable = new IPPAttachable();
$objAttachable->FileName = $randId."TEST02.pdf";
//$objAttachable->AttachableRef = $Estimate->DocNumber;
$objAttachable->AttachableRef = $attachableRef;
$objAttachable->Note = "Test";
$objAttachable->ContentType = $sendMimeType;
$resultObj = $dataService->Upload($up,
$objAttachable->FileName,
$sendMimeType,
$objAttachable);
This code fires, adds an attachment to the proper estimate but the attachment is less than 1k and is unreadable. Almost as if it never fetched the document to attach, it simply made some kind of a "generic" success.
Can anyone assist with updating the above code? I'm sure its right in front of me, but I keep missing.
Thank you!
I used php server side to connect with clickatell messages service , i used the soap api technique to make the connection . it is working .but in my code , i can send just one message at the same time , here is the code :
function actionSendSMS(){
$msgModel = new Messages();
$settModel = new Settings();
$setRows = $settModel->findAll();
$usr=$setRows[0]->clickatell_usr;
$pwdRows = $settModel->findAll();
$pwd=$pwdRows[0]->clickatell_pwd;
$api_idRows = $settModel->findAll();
$api_id=$api_idRows[0]->clickatell_api_id;
$msgModel->findAllBySql("select * from messages where is_sent=0 and
send_date=".date("m/d/Y"));
$client = new SoapClient("http://api.clickatell.com/soap/webservice.php?WSDL");
$params = array('api_id' => $api_id,'user'=> $usr,'password'=> $pwd);
$result = $client->auth($params['api_id'],$params['user'],$params['password']);
$sessionID = substr($result,3);
$callback=6;
// echo $result."<br/>";
// echo $sessionID;
$params2 = array('session_id'=>$sessionID, 'api_id' => $api_id,'user'=>
$usr,'password'=>$pwd,
'to'=>array('962xxxxxxx'), 'from'=>"thetester",'text'=>'this is a sample test
message','callback'=>$callback);
$result2 = $client->sendmsg($params2['session_id'],
$params['api_id'],$params['user'],$params['password'],
$params2['to'],$params2['from'],$params2['text'],$params2['callback']);
print_r( $result2)."<br/>";
$apimsgid= substr($result2[0],4);
$rowsx=Messages::model()->findAllBySql("select * from messages where is_sent=0 and
send_date='".date("m/d/Y")."'");
for($i=0;$i<count($rowsx);$i++)
{
$rowsx[$i]->clickatell_id=$apimsgid;
$rowsx[$i]->save();
}
//echo $apimsgid."<br/>";
if (substr($result2[0], 0,3)==='ERR' && (!(substr($result2[0], 0,2)==='ID'
) ))
{
echo 'Connot Routing Message';
}
.... now you see that this code will send one message at the same time , forget about the id , its for personal purpose , now this service i have to modify it , to send multiple messages at the same time , and i will give every message an unique ID , so now my problem is : is there any one knows if there is a service to send multiple sms at the same time ;
as in my code i fill the information for one message ,but i need a service to send multiple sms , does any body can give me a link to this service , i made many searches but there is no answer i have found
Try startbatch command to send multiple messages at the same time (it also supports personalized). However, it is not based soap, it is based http api.
Have you tried
$params2 = array('session_id'=>$sessionID, 'api_id' => $api_id,'user'=> $usr,'password'=>$pwd, 'to'=>array('962xxxxxxx', '962xxxxxxx', '962xxxxxxx'), 'from'=>"thetester",'text'=...
or
$params2 = array('session_id'=>$sessionID, 'api_id' => $api_id,'user'=> $usr,'password'=>$pwd, 'to'=>array('962xxxxxxx,962xxxxxxx,962xxxxxxx'), 'from'=>"thetester",'text'=...