Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm wondering how do people handle emailing their userbase. I'm using PHP and SendGrid is handling the emails. The use case is a newsletter.
When I send myself 50 emails in testing the script already takes quite a long time and I worry that the application will time out if go for more emails at a time.
I don't really want to send myself 100, then 1,000, then 10,000 emails to see what the upper limit is testing this out.
I'm running this on a medium EC2 Linux if that is helpful.
There are several ways to accomplish this with SendGrid.
Send Emails in Parallel
It's likely that you're currently sending your emails in a series (i.e. send one email, wait for it to send, then send the next). This means if it takes you 1 second to send an email, it will take you 100 seconds to send one hundred.
Luckily, PHP's cURL Library allows you to send many POST requests in parallel, rather than in a series.
// An array of the emails you want to send to:
$emails = array("joe#example.org", "sue#example.com");
$credentials = array("api_user" => YOUR_SENDGRID_USERNAME, "api_key" => YOUR_SENDGRID_PASSWORD);
$multi_handle = curl_multi_init();
$handles = array();
// Loop through each email and create a cURL Handle for the email
// The cURL handle will POST an email to SendGrid with the proper info
foreach ($emails as $key => $email) {
// Create an email data object with your credentials in it,
// we'll POST this to SendGrid
$email_data = $credentials;
// Fill out all the information you want for the email
$email_data = array(
"to" => $email,
"from" => "you#example.net",
"subject" => generate_subject(),
"html" => generate_html(),
"text" => generate_text()
);
$handles[$key] = curl_init();
curl_setopt($handles[$key], CURLOPT_URL, "https://api.sendgrid.com/api/mail.send.json");
curl_setopt($handles[$key], CURLOPT_POST, 1);
curl_setopt($handles[$key], CURLOPT_POSTFIELDS, $email_data);
curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($multi_handle, $handles[$key]);
}
$running = null;
// Run through each cURL handle and execute it.
do {
curl_multi_exec($multi_handle, $running);
} while ($running);
curl_multi_close($multi_handle);
This method works well if you have lots of hyper-unique emails that you want to send, however it still results in a bunch of POST requests from your server, which are slow and resource consuming. There's often a better way.
Send Emails Using the SMTPAPI
SendGrid has an SMTPAPI which allows you to send up to 10,000 emails with one POST request using the to parameter. You can make these emails semi-unique by using substitution tags.
For this you'd do something like the following:
// An array of the emails you want to send to:
$emails = array("joe#example.org", "sue#example.com");
// Encode it into the SendGrid SMTPAPI Format
$smtpapi = array( "to" => $emails );
$params = array(
"api_user" => YOUR_SENDGRID_USERNAME,
"api_key" => YOUR_SENDGRID_PASSWORD,
"to" => "you#example.com",
"subject" => "testing from curl",
"html" => "testing body",
"text" => "testing body",
"from" => "you#example.com",
"x-smtpapi" => json_encode($smtpapi);
);
$request = "https://api.sendgrid.com/api/mail.send.json";
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
However, it's probably better to do this with SendGrid's PHP Library
$emails = array("joe#example.org", "sue#example.com");
$sendgrid = new SendGrid(YOUR_SENDGRID_USERNAME, YOUR_SENDGRID_PASSWORD);
$email = new SendGrid\Email();
$email->setTos($emails)->
setFrom('me#bar.com')->
setSubject('Subject goes here')->
setText('Hello World!')->
setHtml('<strong>Hello World!</strong>');
$sendgrid->send($email);
Queueing
Finally, you could utilize a queue and send all the emails from a different process to speed up your execution time, information on how to do that is in this StackOverflow response.
Related
I want to send FCM push notifications in specific android users only using their token saved in mysql database as identification. here's my current progress
PHP Script Snippet Code: Report_Status.php (File 1)
//Gets the token of every user and sends it to Push_User_Notification.php
while ($User_Row = mysqli_fetch_array($Retrieve_User, MYSQLI_ASSOC)){
$User_Token = $User_Row['User_Token'];
include "../Android_Scripts/Notifications/Push_User_Notification.php";
$message = "Your Report has been approved! Please wait for the fire fighters to respond!";
send_notification($User_Token, $message);
}
PHP code for File 2: Push_User_Notification.php
<?php //Send FCM push notifications process
include_once("../../System_Connector.php");
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = API_ACCESS_KEY',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
}
?>
Problem:
The page is always stuck in Report_Status.php every time I ran the
script. It is supposed to go in Push_User_Notification and return to Report_Status once the process is done. Am I wrong in the implementation of calling the
Push_User_Notification.php or the receiving parameters to
Push_User_Notification.php?
P.S.
Here's my full source code of Report_Status.php in case anyone wants to check it: Report_Status.php
I think the problem you may be having is that you are sending a lot of notifications to several devices in short amount of time. I think it might be being picked up as spaming. My suggestion is sending one notification to multiple devices.
Try changing your code in report_status.php to this.
include "../Android_Scripts/Notifications/Push_User_Notification.php";
$message = "Your Report has been approved! Please wait for the fire fighters to respond!";
while ($User_Row = mysqli_fetch_array($Retrieve_User, MYSQLI_ASSOC)){
$User_Token[] = $User_Row['User_Token'];
}
$tokens = implode(",", $User_Token);
send_notification($tokens, $message);
the idea is that you will collect the user tokens in $User_Token[] array. Then you would comma seperate the tokens and send the message once to all the devices that associate to the tokens. FCM allows you to send to multiple tokens in one go.
updated
$User_Token needs to be an array. so remove the implode. That was my mistake.
Secondly the $message needs to be in the following format.
$message = array(
'title' => 'This is a title.',
'body' => 'Here is a message.'
);
Also another thing to note is that there are 2 types of messages you can send using FCM. Notification Messages or Data Messages. Read more here: https://firebase.google.com/docs/cloud-messaging/concept-options
I dont know if your app is handling the receipt of messages (i dont know if you have implemented onMessageRecieve method) so i would probably suggest making a small change to the $fields array in send_notification function. Adding the notification field allows android to handle notifications automatically if your app is in the background. So make sure you app is in the background when testing. https://firebase.google.com/docs/cloud-messaging/android/receive
$fields = array(
'registration_ids' => $tokens,
'data' => $message,
'notification' => $message
);
So try the code below. I have tried and tested. It works for me. If it does not work. In send_notification function echo $result to get the error message. echo $result = curl_exec($ch); Then we can work from there to see what is wrong. You can see what the errors mean here: https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes
include "../Android_Scripts/Notifications/Push_User_Notification.php";
$message = array(
'title' => 'Report Approved',
'body' => 'Your Report has been approved! Please wait for the fire fighters to respond!'
);
while ($User_Row = mysqli_fetch_array($Retrieve_User, MYSQLI_ASSOC)){
$User_Token[] = $User_Row['User_Token'];
}
send_notification($User_Token, $message);
I have developed a system which will send SMS to the customer after making insert to the database
but the system which I worked on is public ie. when my client asked me to buy the system he must insert his SMS API configuration to send a message to his customers
when I searched on the internet I found that every API have a differnt way to send SMS message
I have account in click tell API but when I send message by curl there is nothing happen
the code is
$url = 'https://platform.clickatell.com/messages/http/send?' . http_build_query(
[
'apiKey' => 'oGjnzDdSRhqdhhgnjFj3ZmzYA==',
// 'api_secret' => '4c98619ffb9af51585',
'to' => '94233698311783',
'content' => 'Hello from Netcom'
]
);
echo($url);echo('<br/>');
echo'https://platform.clickatell.com/messages/http/send?apiKey=oGkmzDdSRgekvjFjaZnzYA==&to=905373545631&content=Test+message+text"';
$ch = curl_init($url);
echo($ch);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
Where my code is wrong and what is the way to send SMS dynamically depending on the client's API info
For quick fix and testing add this in your curl request and check response.(Because url is https)
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
But it is recomonded to downloading a CA root certificate bundle at the curl website and saving it on your server which protect your site
I am not aware of Clickatell, but Aws SNS is quite simple,
$payload = array(
'Message' => $message,
'PhoneNumber' => $mobile,
'MessageAttributes' => $msgAttributes
);
$result = $sns->publish($payload);
I'm using SendGrid for a customer project with curl method.
All works fine but the(ir) file(s) attached on my email sending with SendGrid are broken.
Here is my code :
$documentList = array(
"DOC1.php" => "http://www.customerdomain.com/my/path/where/my/attachment/file/is/myfile.pdf"
);
$params = array(
'api_user' => $user;
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'from' => $from,
'to' => $to,
'subject' => $subject,
'html' => $mailHtml,
'text' => $mailText
);
if(count($documentList)>0){
foreach($documentList as $fileName=>$documentPath){
$params['files['.$fileName.']'] = $documentPath;
}
}
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
When I don't have extension file on my key of array, I've a text file containing the related value.
I think I'm not alone to have this problem, well, if you've any idea to solve this problem, thanks for your help !
The issue you're experiencing is because you are giving SendGrid a URL for file, rather than the file itself, and SendGrid's API needs the file.
To get your code to work, simply change the $documentList variable to:
$documentList = array(
"DOC1.pdf" => "#" . realpath("/path/where/my/attachment/file/is/myfile.pdf")
);
Instructions on this kind of file upload can be found in this StackOverflow Question, but you might otherwise want to use curl_file_create, to do this.
However, perhaps the best/easiest way to do this is to use SendGrid's PHP Library which makes sending attachments, trivially simple.:
require("path/to/sendgrid-php/sendgrid-php.php");
$sendgrid = new SendGrid('username', 'password');
$email = new SendGrid\Email();
$email->addTo('foo#bar.com')->
setFrom('me#bar.com')->
setSubject('Subject goes here')->
setText('Hello World!')->
setHtml('<strong>Hello World!</strong>')
addAttachment("../path/to/file.txt");
$sendgrid->send($email);
My initial issue was because the path is an autogenerated link and that's why I didn't use an url than realpath.
Anyway, I changed my code and I use now the realpath with mimetype mentioned after the file realpath (and # before).
It seems work fine now.
I'll like to thank you for your help.
Regards
I am woking on my final year project and I would like to send sms to a client via php.
Is there any way in which I can use my cell phone as a GSM modem and send sms via php?
I tried "Ozeki NG - SMS Gateway" but not sure how to send sms via php.
if anyone has used/tried "Ozeki NG - SMS Gateway" then please let me know how to use it properly?
Any suggestions please do let me know.
I have a nokia 701. Can it be used as a gsm modem?.
Thank you
Try out this example code from Twilio, I used it during one of the hackathons.
Just for demo purpose this may help.
Depends on what country though.
http://www.twilio.com/docs/quickstart/php/sms
American Telcos let you send text messages as emails. I haven't played with this is many years, but I remember Verizon being [Phone #]#vtext.com, where [Phone #] is the 10-digit phone number of the cell phone. There are different domains for the different Telcos that you can lookup. Best of all this is free, rather than having to pay per text message.
If your phone is android you could use an app such as SMS Gateway API which will turn your phone into a SMS gateway and would allow you to both send and receive messages via PHP.
$URL = "http://v2.smsgateway.me/API/Send/Single.php";
$postdata = http_build_query(
array(
'Username' => "foo#bar.co.uk",
'Password' => "password",
'Number' => "+447791064782",
'Message' => "Hello World!",
'Device' => 1, //Send from device 1 (optional)
'SendAt' => (time() + (60 * 60)), //Send in 1 hours time (optional)
'ExpireAt' => (time() + (60 * 60 * 2)) //Expire in 2 hours time (optional)
)
);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
$context = stream_context_create($opts);
$result = file_get_contents($URL, false, $context);
echo $result;
Your can try https://www.clickatell.com/ it is easy to use & integrate into your php code you can use http or rest api.
I assume that you already have sms service from msg91 using above setup or you can use any sms service providers like Twilio, Nexmo etc. Now i am creating a common function which you can call anywhere in your PHP code to send any type of text sms.
//send otp message
public static function sendMessage($mobile, $message)
{
$message = urlencode($message);
$url = sprintf("http://api.msg91.com/api/v2/sendsms?authkey=%s&mobiles=%s&message=%s&sender=%s&route=%s",
env('MSG91_AUTH_KEY'), $mobile, $message, env('MSG91_SENDER_ID'), env('MSG91_ROUTE'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
For more details and step by step execution follow below link :
https://www.lelocode.com/posts/sending-sms-like-otp,-welcome-message,mobile-verification-using-php---lelocode
I've created an employment application that allows applicants to upload their resume in pdf, doc, or docx extensions. I then email their information and resume to Human Resources using Sendgrid.
I've found through testing that when sending the email I receive an error:
Parameter attachment[resume.pdf] is not utf8
How can I fix this issues, should I encode every file that is uploaded to utf-8 before attaching it to the email? Will this create any issues or severally modify an users uploaded resume?
Here is my PHP Curl code I use to send via the SendGrid API:
(Note: I have to use the REST API, SMTP is not configured on the clients web server)
<?php
$mail['from'] = 'humanresources#email.org';
$mail['fromname'] = 'Human Resources';
$mail['to'] = 'person#email.com';
$mail['subject'] = character_limiter('Employment: '. $application['position'], 50);
$mail['html'] = '<p><strong>Name:</strong> '.$application['firstname'].' '.$application['lastname'].'</p>';
$mail['html'] .= '<p><strong>Position:</strong> '.$application['position'].'</p>';
$mail['html'] .= '<p><strong>Date:</strong> '.mdate('%m/%d/%Y %g:%i %A', $application['timestamp_saved']).'</p>';
$mail['html'] .= '<p><strong>Email:</strong> '.$application['email'].'</p>';
$mail['files['.$application['pdf'].']'] = '#saved_applications/'. $application['pdf'];
//Sendgrid Credientals
$mail['api_user'] = 'sendgrid_user';
$mail['api_key'] = 'sendgrid_pass';
print_r($mail);
// Generate curl request
$session = curl_init('https://sendgrid.com/api/mail.send.json');
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $mail);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
$output = json_decode($response, TRUE);
print_r($output);
?>
The code you have should work (and worked when I tested it).
You may want to use the PHP library, rather than flat cURL. You'll be able to send attachments, from the web Web API by doing the following:
<?php
$sendgrid = new SendGrid('username', 'password');
$mail = new SendGrid\Mail();
$mail->
addTo('humanresources#email.org')->
...
addAttachment('saved_applications/'. $application['pdf']);
$sendgrid->web->send($mail);
?>
Perhaps instead of using sendgrid's web services API to send these messages, simply use phpmailer and send the messages through sendgrid by way of SMTP using their outgoing mail server smtp.sendgrid.net. I'm sure you won't get this error if you send the messages this way. See example at https://github.com/PHPMailer/PHPMailer.