我试图使用PHP 邮件()函数,但我不能让它工作,所以我” VE搜索一些答案和这个作品:
https://github.com/PHPMailer/PHPMailer
你可以使用它时,你会邮件发送到Gmail帐户或本地电子邮件服务器。
注:
确保您的PHPMailerAutoload.php路径是正确的,当你需要。例如:
require 'assets/api/PHPMailer-master/PHPMailerAutoload.php';
你必须知道主机名,如果你要发送到本地电子邮件服务器。
您必须拥有一个可用于发送邮件的帐户。
分析代码是如何工作的,并随时对其他问题发表评论。
我会在我开发的网站上附上一个示例代码。
<?php
$strFullname = $strEmail = $strMobile = $strPosition = "";
require 'assets/api/PHPMailer-master/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer(true);
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
//$mail->SMTPDebug = 1;
//Ask for HTML-friendly debug output
//a$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'secure.emailsrvr.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMsTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "[email protected]";
//Password to use for SMTP authentication
$mail->Password = "myaccountpassword";
//Set who the message is to be sent from, you can use your own mail here
$mail->setFrom('[email protected]', '@noreply.bpsource.com');
//Set an alternative reply-to address
//$mail->addReplyTo('[email protected]', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'Firstname Lastname');
//Set the subject line
$mail->Subject = 'New application form sent from ***** Career page';
$mail->IsHTML(true);
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$strFullname = $_POST['strFullname'];
$strEmail = $_POST['strEmail'];
$strMobile = $_POST['strMobile'];
$strPosition = $_POST['strPosition'];
//This part is where you will create your mail
$mail->msgHTML("Fullname: ".$strFullname."\nEmail: ".$strEmail."\nMobile Number: ".$strMobile."\nDesired Position: ".$strPosition);
//This part is for sending the mail
if (!$mail->send()) {
//If you want to check for errors. Uncomment the line below.
//echo "Mailer Error: " . $mail->ErrorInfo;
echo "<script>alert('Some error occured. Please try again later');</script>";
header("Refresh:2");
}
echo "<script>alert('Application form successfully sent!');</script>";
header("Refresh:2");
}
?>
希望我明白你的意思。问候!祝你好运!
请指出你遇到什么问题以及哪部分代码失败。 –
我没有收到我的收件箱中的电子邮件。我没有得到任何错误,所以我猜代码没有失败? – Dutch