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);