Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a form with input fields that when submited sends an email out through a WebAPI from SendGrid. Without writing out the whole come here are the important parts:

HTML:

<form action="sent.php" method="post">

<label>To:</label>

<input name="to" type="text" size="95" />

<label>Subject:</label>

<input name="subject" type="text" size="95" />

<label>Message:</label>

<textarea name="message" type="text" cols="71" rows="30"></textarea>

<input name="submit" type="submit" value="Send"/>

</form>

PHP:

<?php
require 'SendGrid_loader.php';

print "I'm setting up variables here\n";
$user = 'XXXX';
$password = 'XXXX';
$to_email = array('[email protected]','[email protected]','XXXXhotmail.com');


print "I'm creating a new SendGrid account\n";
$sendgrid = new SendGrid($user,$password);


$mail = new SendGrid\Mail();
$mail->setTos($to_email)
    ->setFrom('[email protected]')
    ->setSubject($_POST['subject'])
    ->setText($_POST['message'])
    ->setFromName('Jacob');

print "about to send email\n";
$result=$sendgrid->web->send($mail);

print_r($result);

What I am trying to do is allow someone to enter multiple email addresses in the "To:" input field seperate by a comma (i.e. To: "[email protected], [email protected], [email protected]") and have them entered into the array in the PHP file. I am trying to do this without having to create more than one To: input field.

Any suggestions?

Thank You

!UPDATE:

The PHP explode() Function worked. Here is my updated working code:

HTML:

<form action="sent.php" method="post">

<label>To:</label>

<input name="to" type="text" size="95" />

<label>Subject:</label>

<input name="subject" type="text" size="95" />

<label>Message:</label>

<textarea name="message" type="text" cols="71" rows="30"></textarea>

<input name="submit" type="submit" value="Send"/>

</form>

PHP:

<?php
require 'SendGrid_loader.php';

print "I'm setting up variables here\n";
$user = 'XXXXXXX';
$password = 'XXXXXXX';
$str = $_POST['to'];
$to_email = explode(",", $str);


print "I'm creating a new SendGrid account\n";
$sendgrid = new SendGrid($user,$password);


$mail = new SendGrid\Mail();
$mail->setTos($to_email)
    ->setFrom('[email protected]')
    ->setSubject($_POST['subject'])
    ->setText($_POST['message'])
    ->setFromName('Jacob');

print "about to send email\n";
$result=$sendgrid->web->send($mail);

print_r($result);
share|improve this question

1 Answer 1

up vote 4 down vote accepted

http://php.net/explode You have a comma separated list you want to turn into an array? Use explode.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.