PHPMailer具有基于域的Gmail帐户

时间:2015-02-26 04:37:05

标签: php gmail phpmailer contact-form

我已经使用PHPMailer多年了,并且对此感到非常满意,但现在我正在尝试使用基于域的电子邮件地址处理联系表单,但通过Google Apps绑定到Gmail 。即MX记录指向Google。我已经添加了PHPMailer的Gmail示例,试图使设置正常工作,但此时,我还没有连接;我收到这条消息:

SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
Mailer Error: SMTP connect() failed.

这是我的表单处理器代码:

<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */

// CUSTOM: collect data from our web form
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
//date_default_timezone_set('Etc/UTC');

require 'mailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;


//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 = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "myemailaddress.com";

//Password to use for SMTP authentication
$mail->Password = "mypassword";

//Set who the message is to be sent from
//$mail->setFrom('from@example.com', 'First Last');

//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('myemailaddress', 'my name');

//Set the subject line
$mail->subject = $subject;

 $mail->Body = "Name : $name\n\n"
  . "Email : $email\n"
  . "Telephone : $tel\n"
  . "Message :\n\n $message\n"
  . "";

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';

//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

提前回答一些问题:是的,Gmail中启用了SSL而不是TLS,并且主机上的MX记录已更改。我也注释了一些东西,但据我所知,我需要把表格实际发送给我。

除此之外,我甚至没有资格成为PHP的黑客;我的主要技能是设计,HTML和CSS,所以我可能错过了一些明显的东西......

提前致谢。

0 个答案:

没有答案