我有一份报名表:
<h2>PHP Form Validation Example</h2>
<form method="post" action="sendmail.php">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Website: <input type="text" name="website">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
我写了一个PHP代码,邮件可以像这样发送:
<?php
$name = $_POST['name'];
$to = $_POST['email'];
$msg = $_POST['comment'];
sendMail($to);
function sendMail($to){
$subject = "Thanks for registring.";
$message = "We are glad that you have posted your problems with us.";
//$header = "From:".$to;
$headers = array(
'From' => $email,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.live.com',
'port' => '587',
'auth' => true,
'username' => 'USERNAME',
'password' => 'PASSWORD'
));
$pmailing = $smtp->send($to, $headers, $body);
//$retval = mail ($to,$subject,$message,$header);
//$mmsg = 'Receiver: mymail@gmail.com';
if( $pmailing == true )
{
echo "Message sent successfully...";
//$mmsg = "Mail sent.";
}
else
{
echo "Message could not be sent...";
//$mmsg = "Mail not send.";
echo'<script> window.location="re_enter.html"; </script> ';
}
}
?>
显示“邮件”是不可接受的。所以建议我,我已经使用您共享的代码编辑了我的代码。我是否需要添加任何库或其他任何库来执行此SMTP代码。
答案 0 :(得分:0)
用于在xmapp Localhost上配置邮件
转到xampp / php / php.ini打开并搜索
[mail function]
SMTP = mail.yourserver.com
smtp_port = 25
并保存文件并重新启动Apache 有关详细信息: - How to configure XAMPP to send mail from localhost?
发送mail() php函数的是
$retval = mail($to, $subject, $message);
if($retval)
// your stuff here
或使用phpmailer
答案 1 :(得分:0)
尝试此代码。这是演示代码。根据您的要求进行更改。
<?php
$from = $_POST['EmailAddr']; //email id of applicant.
$to = 'YOUR MAIL ID';
$subject = 'Call Back Enquiry';
$fullname=$_POST['Name'];
$mobile=$_POST['PhoneMobile'];
$email=$_POST['EmailAddr'];
$message=$_POST['YourMessage'];
$body="Full Name:".$fullname."\n\n"."Mobile:".$mobile."\n\n"."Email:".$email."\n\n"."Message:".$message;
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'YOUR MAIL ID',
'password' => 'PASSWORD'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
// echo('<p>Message successfully sent!</p>');
}
?>