Php邮件不使用ssmpt发送

时间:2016-01-29 10:05:45

标签: php

我正在处理我的一个小项目,我想添加一个联系表单,所以我做了。一切正常,但它似乎没有发送电子邮件。

我已经安装了ssmpt并将其配置正确:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com

mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
UseTLS=yes
AuthUser=myemail@gmail.com
AuthPass=mypasdword

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=hidingthisforsecurity

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES

我还配置了php.ini:

 ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
 ; http://php.net/sendmail-path
 sendmail_path = /usr/sbin/ssmtp -t

这是我的代码:

 <?php

 require 'config.php';

 $strContactEmail = "myemail@gmail.com"; //edit this to your email

 function sendError($strErr) {
         $strMsg = "<center><h2>Error: " . $strErr . "</h2></center>"; 
         die($strMsg);
 }

 $resDBCon= mysqli_connect($strDBHost, $strDBUser, $strDBPass, $strDBName) or sendError('Failed to connect to MySQL: ' . mysqli_connect_error());

 if (isset($_POST['submit'])) {
 $strUsername = $_POST['username'];
 $strEmail = $_POST['email'];
 $strSubject = $_POST['subject'];
 $strMessage = $_POST['comments'];

 if (!isset($strUsername) || !isset($strEmail) || !isset($strSubject) || !isset($strMessage)) {
     sendError('One or more fields has not been completed, please fill in everything');     
 }

 $strUsername = mysqli_real_escape_string($resDBCon, $strUsername);
 $strEmail = mysqli_real_escape_string($resDBCon, $strEmail);
 $strSubject = mysqli_real_escape_string($resDBCon, $strSubject);
 $strMessage = mysqli_real_escape_string($resDBCon, $strMessage);

 $strUsername = stripslashes($strUsername);
 $strEmail = stripslashes($strEmail);
 $strSubject = stripslashes($strSubject);
 $strMessage = stripslashes($strMessage);

 if (!filter_var($strEmail, FILTER_VALIDATE_EMAIL)) {
    sendError('Invalid email address! Please recheck your email');
 } elseif (!ctype_alnum($strUsername) && strlen($strUsername) > 10 && strlen($strUsername) <= 3) {
    sendError('Invalid username! Please make sure the username is alphanumeric and not too long or short');
 } elseif (!ctype_alnum($strSubject) && strlen($strSubject) < 5 && strlen($strSubject) > 10) {
    sendError('Invalid title! Please enter a valid subject, make sure it is alphanumeric and more than 5 and lesser than 10 characters long');
 } elseif (!ctype_alnum($strMessage) && strlen($strMessage) < 5 && strlen($strMessage) > 500) {
    sendError('Invalid message! Please enter a valid message, make sure it is alphanumeric and more than 5 and lesser than 500 characters long');
 }

 $strHeaders = "From: $strEmail" . "\r\n" . "CC: $strContactEmail";    
 $strMessage = wordwrap($strMessage, 70);

 $blnSent = mail($strContactEmail, $strSubject, $strMessage, $strHeaders);  

 if ($blnSent) {
    echo "<center><h2>Thank you for contacting us, you will receive an email from us within the next 48 hours</h2></center>";
 } else {
    sendError('Failed to send email');
 }

} else {

?>

<center>
<form class="form" name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="text" name="username" maxlength="10" placeholder="Enter Your Username">
   <input type="text" name="email" maxlength="25" placeholder="Enter A Valid Email">
   <input type="text" name="subject" maxlength="10" placeholder="Enter Your Subject">
   <textarea  name="comments" maxlength="500" cols="25" rows="6" placeholder="Enter Your Message"></textarea>
   <input type="submit" id="login-button" name="submit" value="Submit">
</form>
</center>

<?php 
 } 
 ?>

1 个答案:

答案 0 :(得分:1)

尝试以下

登录您的Google电子邮件帐户,然后转到此链接:https://www.google.com/settings/security/lesssecureapps并将“访问安全性较低的应用”设置为“开启”。