1

我有一个带有输入字段的表单,提交时会通过 SendGrid 的 WebAPI 发送电子邮件。没有写出整个来这里是重要的部分:

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('XXXX@yahoo.com','XXXX@gmail.com','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('jacob@jacob.com')
    ->setSubject($_POST['subject'])
    ->setText($_POST['message'])
    ->setFromName('Jacob');

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

print_r($result);

我想做的是允许某人在“收件人:”输入字段中输入多个电子邮件地址,用逗号分隔(即收件人:“john@gmail.com,sue@gmail.com,gill@gmail.com”)并将它们输入到 PHP 文件中的数组中。我试图做到这一点,而不必创建多个 To: 输入字段。

有什么建议么?

谢谢你

!更新:

PHP explode() 函数起作用了。这是我更新的工作代码:

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('jacob@jacob.com')
    ->setSubject($_POST['subject'])
    ->setText($_POST['message'])
    ->setFromName('Jacob');

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

print_r($result);
4

1 回答 1

4

http://php.net/explode 你有一个逗号分隔的列表想要变成一个数组?使用爆炸。

于 2013-06-06T23:49:01.087 回答