我是否可以通过在HTML页面上创建一个简单的表单然后将action属性指向.php文件来测试这个php脚本?我创建了.PHP文件,只是不知道如何发送它。
<?php
$recipients = array('name@domain.com','name@domain.com',
'name@domain.com',
'name@domain.com',
); // i will be inserting the test email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$mail_from = $_POST['sender_email'];
$mail_to = $_POST[$recipients];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'College Newsletter: ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Message: ' . $message;
// Construct email headers
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail(implode(',',$mail_to), $subject, $body_message, $headers);
?>
服务器似乎不喜欢我的implode方法。 (见上)我一直收到错误:警告:implode()[function.implode]:传入的参数无效 我将PHP更改为:
<?php
$to = array('name@domain.com','name@yahoo.com');
// specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['name'];
$mail_from = $_POST['mail'];
$message = $_POST['message'];
// Construct email subject
$subject = 'Breaking News: ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Message: ' . $message;
// Construct email headers
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
mail($to, $name, $mail_from, $message, $headers);
?>
现在我收到错误: 警告:mail()期望参数1是字符串,在第70行的bulk_mailer_test.php中给出数组 任何人? -Cheers,-qs
答案 0 :(得分:0)
您的问题在于此代码
<?php
$recipients = array('name@domain.com','name@domain.com',
'name@domain.com',
'name@domain.com',
); // i will be inserting the test email here
$mail_to = $_POST[$recipients];
$mail_sent = mail(implode(',',$mail_to)...
implode()想要一个数组。你传递的可能是null值。
删除此$mail_to = $_POST[$recipients];
,然后使用mail(implode(',',$recipients)
$ _ POST是一个超级全局,用于从提交的表单中收集数据。如果您已在数组中列出您的收件人地址..那么就没有必要。如果您想从表单中收集多封电子邮件,请执行以下操作...
$recipients = array($_POST['email_1'], $_POST['email_2'], $_POST['email_3'], $_POST['email_4'],
);