当有人在我的网页上填写表单时,我正在尝试向自己发送电子邮件。我的表格如下:
<form id="contactForm" action="#" method="post">
<fieldset>
<p>
<label for="name" >Name</label>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your full name" />
</p>
<p>
<label for="email" >Email</label>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
</p>
<p>
<label for="web">Website</label>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your website" />
</p>
<p>
<label for="comments">Message</label>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</p>
<!-- send mail configuration -->
<input type="hidden" value=EMAIL" name="to" id="to" />
<input type="hidden" value="Email received from Website" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
和PHP我看起来像这样:
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['email'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'EMAIL',
'password' => 'PASSWORD'
));
$mail = $smtp->send($to, $headers, $msg);
?>
EMAIL和PASSWORD被替换为值....
有人可以建议做什么吗?
答案 0 :(得分:1)
将主机更新为smtp.gmail.com并将端口更新为587并使用结果更新我们。
答案 1 :(得分:1)
PHP - 和HTML一起(并用您自己的SMTP替换mail()
函数)
I.e。:
$mail = $smtp->send($to, $subject, $msg, $headers);
新代码:
<?php
//vars
if(isset($_POST['submit'])){
$subject = $_POST['subject'];
$to = "email@example.com";
$from = $_POST['email'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
// insert with your SMTP
mail ($to, $subject,$msg, $headers);
echo "sent";
}
?>
<form id="contactForm" action="" method="post">
<fieldset>
<p>
<label for="name" >Name</label>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your full name" />
</p>
<p>
<label for="email" >Email</label>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
</p>
<p>
<label for="web">Website</label>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your website" />
</p>
<p>
<label for="comments">Message</label>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</p>
<!-- send mail configuration -->
<input type="hidden" value="Email received from Website" name="subject" id="subject" />
<!-- ENDS send mail configuration -->
<p><input type="submit" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
您的代码存在很多问题。
您的参数已关闭($to, $headers, $msg)
标题位于最后且$subject
未包括在内;它必须在那里。
语法为($to, $subject,$msg, $headers)
它是TO,然后是Subject,然后是Message,然后是Headers。根据{{3}}即使您使用SMTP, 邮件也是邮件。
我不知道你为什么这么做:
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
只需在表单action=
这对我有用:你可以替换:
mail ($to, $subject, $msg, $headers);
with(如果它适用于你)似乎还有其他答案。
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'EMAIL',
'password' => 'PASSWORD'
));
$mail = $smtp->send($to, $headers, $msg);
PHP - 经过测试(send-mail.php)
<?php
//vars
if(isset($_POST['submit'])){
$subject = $_POST['subject'];
$to = "email@example.com";
$from = $_POST['email'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
mail ($to, $subject,$msg, $headers);
echo "sent";
}
?>
HTML表单
<form id="contactForm" action="send-mail.php" method="post">
<fieldset>
<p>
<label for="name" >Name</label>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your full name" />
</p>
<p>
<label for="email" >Email</label>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
</p>
<p>
<label for="web">Website</label>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your website" />
</p>
<p>
<label for="comments">Message</label>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</p>
<!-- send mail configuration -->
<input type="hidden" value="Email received from Website" name="subject" id="subject" />
<!-- ENDS send mail configuration -->
<p><input type="submit" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
<强>脚注:强>
value=EMAIL"
中缺少引号,但我的PHP已将其替换为:
$to = "email@example.com";
答案 2 :(得分:0)
这是一种便宜而简单的方法。
在你的php.ini中用
替换邮件功能的相关部分[mail function]
; For Win32 only.
SMTP = smtp.gmail.com
smtp_port = 587
; For Win32 only.
sendmail_from = EMAILADDRESSHERE
用
替换sendmail.ini[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
auth_username=EMAILADDRESSHERE
auth_password=PASSWORDHERE
force_sender=EMAILADDRESSHERE
然后在您的代码中,发送一封相同的电子邮件,除非将其写为
mail($to, $subject, $msg, $headers)
或修改以适应。
答案 3 :(得分:0)
您必须首先检查变量$ to的值是否是正确的邮件方向。基于您发布的代码可能会出错。
在PHP代码中,您调用类Mail但不包含/ require,添加:
// Pear Mail Library
require_once "Mail.php";
发送邮件后,您无法显示结果:
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
答案 4 :(得分:0)