Sendgrid php send to multiple recipients
I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.
function sendEmail($subject, $to, $message) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;
$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);
$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
php sendgrid
add a comment |
I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.
function sendEmail($subject, $to, $message) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;
$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);
$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
php sendgrid
1
Call the function for each email address.
– muttonUp
Apr 25 '17 at 21:44
Code sample please ?
– Super Mario's Yoshi
Apr 25 '17 at 21:44
1
really? loop through your addresses and call the function.
– muttonUp
Apr 25 '17 at 21:46
add a comment |
I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.
function sendEmail($subject, $to, $message) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;
$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);
$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
php sendgrid
I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.
function sendEmail($subject, $to, $message) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;
$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);
$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
php sendgrid
php sendgrid
asked Apr 25 '17 at 18:28
Super Mario's YoshiSuper Mario's Yoshi
5911919
5911919
1
Call the function for each email address.
– muttonUp
Apr 25 '17 at 21:44
Code sample please ?
– Super Mario's Yoshi
Apr 25 '17 at 21:44
1
really? loop through your addresses and call the function.
– muttonUp
Apr 25 '17 at 21:46
add a comment |
1
Call the function for each email address.
– muttonUp
Apr 25 '17 at 21:44
Code sample please ?
– Super Mario's Yoshi
Apr 25 '17 at 21:44
1
really? loop through your addresses and call the function.
– muttonUp
Apr 25 '17 at 21:46
1
1
Call the function for each email address.
– muttonUp
Apr 25 '17 at 21:44
Call the function for each email address.
– muttonUp
Apr 25 '17 at 21:44
Code sample please ?
– Super Mario's Yoshi
Apr 25 '17 at 21:44
Code sample please ?
– Super Mario's Yoshi
Apr 25 '17 at 21:44
1
1
really? loop through your addresses and call the function.
– muttonUp
Apr 25 '17 at 21:46
really? loop through your addresses and call the function.
– muttonUp
Apr 25 '17 at 21:46
add a comment |
3 Answers
3
active
oldest
votes
The SendGridMail
class supports adding multiple to
addresses through the SendGridPersonalization
class.
You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35
Think of a Personalization
as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail
object, must have at least one Personalization
.
Through the constructor you are using, a Personalization
object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958
You can create a Mail
object without this and later add your own Personalization
.
Thank you that helped me to do this, i pasted my final code below.
– Super Mario's Yoshi
Apr 28 '17 at 19:41
1
@SuperMario'sYoshi You should accept this answer if it helped you.
– ceejayoz
Apr 28 '17 at 19:42
add a comment |
At the end this is how i managed to do this and it's working good.
function sendEmail($subject, $to, $message, $cc) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;
$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);
foreach ($cc as $value) {
$to = new SendGridEmail(null, $value);
$mail->personalization[0]->addCC($to);
}
$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
add a comment |
function makeEmail($to_emails = array(),$from_email,$subject,$body) {
$from = new SendGridEmail(null, $from_email);
$to = new SendGridEmail(null, $to_emails[0]);
$content = new SendGridContent("text/plain", $body);
$mail = new SendGridMail($from, $subject, $to, $content);
$to = new SendGridEmail(null, $to_emails[1]);
$mail->personalization[0]->addTo($to);
return $mail;
}
function sendMail($to = array(),$from,$subject,$body) {
$apiKey = 'your api key';
$sg = new SendGrid($apiKey);
$request_body = makeEmail($to ,$from,$subject,$body);
$response = $sg->client->mail()->send()->post($request_body);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());
}
$to = array('test1@example.com','test2@example.com');
$from = 'from@example.com';
$subject = "Test Email Subject";
$body = "Send Multiple Person";
sendMail($to ,$from,$subject,$body);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43618340%2fsendgrid-php-send-to-multiple-recipients%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The SendGridMail
class supports adding multiple to
addresses through the SendGridPersonalization
class.
You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35
Think of a Personalization
as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail
object, must have at least one Personalization
.
Through the constructor you are using, a Personalization
object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958
You can create a Mail
object without this and later add your own Personalization
.
Thank you that helped me to do this, i pasted my final code below.
– Super Mario's Yoshi
Apr 28 '17 at 19:41
1
@SuperMario'sYoshi You should accept this answer if it helped you.
– ceejayoz
Apr 28 '17 at 19:42
add a comment |
The SendGridMail
class supports adding multiple to
addresses through the SendGridPersonalization
class.
You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35
Think of a Personalization
as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail
object, must have at least one Personalization
.
Through the constructor you are using, a Personalization
object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958
You can create a Mail
object without this and later add your own Personalization
.
Thank you that helped me to do this, i pasted my final code below.
– Super Mario's Yoshi
Apr 28 '17 at 19:41
1
@SuperMario'sYoshi You should accept this answer if it helped you.
– ceejayoz
Apr 28 '17 at 19:42
add a comment |
The SendGridMail
class supports adding multiple to
addresses through the SendGridPersonalization
class.
You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35
Think of a Personalization
as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail
object, must have at least one Personalization
.
Through the constructor you are using, a Personalization
object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958
You can create a Mail
object without this and later add your own Personalization
.
The SendGridMail
class supports adding multiple to
addresses through the SendGridPersonalization
class.
You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35
Think of a Personalization
as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail
object, must have at least one Personalization
.
Through the constructor you are using, a Personalization
object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958
You can create a Mail
object without this and later add your own Personalization
.
edited Apr 27 '17 at 11:58
answered Apr 27 '17 at 9:32
Sebastian-Laurenţiu PlesciucSebastian-Laurenţiu Plesciuc
1,4041530
1,4041530
Thank you that helped me to do this, i pasted my final code below.
– Super Mario's Yoshi
Apr 28 '17 at 19:41
1
@SuperMario'sYoshi You should accept this answer if it helped you.
– ceejayoz
Apr 28 '17 at 19:42
add a comment |
Thank you that helped me to do this, i pasted my final code below.
– Super Mario's Yoshi
Apr 28 '17 at 19:41
1
@SuperMario'sYoshi You should accept this answer if it helped you.
– ceejayoz
Apr 28 '17 at 19:42
Thank you that helped me to do this, i pasted my final code below.
– Super Mario's Yoshi
Apr 28 '17 at 19:41
Thank you that helped me to do this, i pasted my final code below.
– Super Mario's Yoshi
Apr 28 '17 at 19:41
1
1
@SuperMario'sYoshi You should accept this answer if it helped you.
– ceejayoz
Apr 28 '17 at 19:42
@SuperMario'sYoshi You should accept this answer if it helped you.
– ceejayoz
Apr 28 '17 at 19:42
add a comment |
At the end this is how i managed to do this and it's working good.
function sendEmail($subject, $to, $message, $cc) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;
$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);
foreach ($cc as $value) {
$to = new SendGridEmail(null, $value);
$mail->personalization[0]->addCC($to);
}
$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
add a comment |
At the end this is how i managed to do this and it's working good.
function sendEmail($subject, $to, $message, $cc) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;
$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);
foreach ($cc as $value) {
$to = new SendGridEmail(null, $value);
$mail->personalization[0]->addCC($to);
}
$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
add a comment |
At the end this is how i managed to do this and it's working good.
function sendEmail($subject, $to, $message, $cc) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;
$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);
foreach ($cc as $value) {
$to = new SendGridEmail(null, $value);
$mail->personalization[0]->addCC($to);
}
$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
At the end this is how i managed to do this and it's working good.
function sendEmail($subject, $to, $message, $cc) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;
$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);
foreach ($cc as $value) {
$to = new SendGridEmail(null, $value);
$mail->personalization[0]->addCC($to);
}
$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
answered Apr 28 '17 at 19:39
Super Mario's YoshiSuper Mario's Yoshi
5911919
5911919
add a comment |
add a comment |
function makeEmail($to_emails = array(),$from_email,$subject,$body) {
$from = new SendGridEmail(null, $from_email);
$to = new SendGridEmail(null, $to_emails[0]);
$content = new SendGridContent("text/plain", $body);
$mail = new SendGridMail($from, $subject, $to, $content);
$to = new SendGridEmail(null, $to_emails[1]);
$mail->personalization[0]->addTo($to);
return $mail;
}
function sendMail($to = array(),$from,$subject,$body) {
$apiKey = 'your api key';
$sg = new SendGrid($apiKey);
$request_body = makeEmail($to ,$from,$subject,$body);
$response = $sg->client->mail()->send()->post($request_body);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());
}
$to = array('test1@example.com','test2@example.com');
$from = 'from@example.com';
$subject = "Test Email Subject";
$body = "Send Multiple Person";
sendMail($to ,$from,$subject,$body);
add a comment |
function makeEmail($to_emails = array(),$from_email,$subject,$body) {
$from = new SendGridEmail(null, $from_email);
$to = new SendGridEmail(null, $to_emails[0]);
$content = new SendGridContent("text/plain", $body);
$mail = new SendGridMail($from, $subject, $to, $content);
$to = new SendGridEmail(null, $to_emails[1]);
$mail->personalization[0]->addTo($to);
return $mail;
}
function sendMail($to = array(),$from,$subject,$body) {
$apiKey = 'your api key';
$sg = new SendGrid($apiKey);
$request_body = makeEmail($to ,$from,$subject,$body);
$response = $sg->client->mail()->send()->post($request_body);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());
}
$to = array('test1@example.com','test2@example.com');
$from = 'from@example.com';
$subject = "Test Email Subject";
$body = "Send Multiple Person";
sendMail($to ,$from,$subject,$body);
add a comment |
function makeEmail($to_emails = array(),$from_email,$subject,$body) {
$from = new SendGridEmail(null, $from_email);
$to = new SendGridEmail(null, $to_emails[0]);
$content = new SendGridContent("text/plain", $body);
$mail = new SendGridMail($from, $subject, $to, $content);
$to = new SendGridEmail(null, $to_emails[1]);
$mail->personalization[0]->addTo($to);
return $mail;
}
function sendMail($to = array(),$from,$subject,$body) {
$apiKey = 'your api key';
$sg = new SendGrid($apiKey);
$request_body = makeEmail($to ,$from,$subject,$body);
$response = $sg->client->mail()->send()->post($request_body);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());
}
$to = array('test1@example.com','test2@example.com');
$from = 'from@example.com';
$subject = "Test Email Subject";
$body = "Send Multiple Person";
sendMail($to ,$from,$subject,$body);
function makeEmail($to_emails = array(),$from_email,$subject,$body) {
$from = new SendGridEmail(null, $from_email);
$to = new SendGridEmail(null, $to_emails[0]);
$content = new SendGridContent("text/plain", $body);
$mail = new SendGridMail($from, $subject, $to, $content);
$to = new SendGridEmail(null, $to_emails[1]);
$mail->personalization[0]->addTo($to);
return $mail;
}
function sendMail($to = array(),$from,$subject,$body) {
$apiKey = 'your api key';
$sg = new SendGrid($apiKey);
$request_body = makeEmail($to ,$from,$subject,$body);
$response = $sg->client->mail()->send()->post($request_body);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());
}
$to = array('test1@example.com','test2@example.com');
$from = 'from@example.com';
$subject = "Test Email Subject";
$body = "Send Multiple Person";
sendMail($to ,$from,$subject,$body);
answered Jul 14 '17 at 14:24
Nanhe KumarNanhe Kumar
9,13934744
9,13934744
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43618340%2fsendgrid-php-send-to-multiple-recipients%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Call the function for each email address.
– muttonUp
Apr 25 '17 at 21:44
Code sample please ?
– Super Mario's Yoshi
Apr 25 '17 at 21:44
1
really? loop through your addresses and call the function.
– muttonUp
Apr 25 '17 at 21:46