如何在PHPMailer中指定SMTP服务器? [英] How to specify the SMTP server in PHPMailer?

查看:129
本文介绍了如何在PHPMailer中指定SMTP服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何获取SMTP服务器以使用 PHPMailer 自动发送包含我创建的电子邮件(email@mydomain.com)的电子邮件.

I want to know how do I get a SMTP server to use PHPMailer to send automatic emails with my created email (email@mydomain.com).

我的虚拟主机没有提供一个.我将域名带到了OnlyDomains,我的虚拟主机是 000Webhost .

My webhost doesn't provide one. I brought my domain in OnlyDomains and my webhost is 000Webhost.

<?php
require './PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = '';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'noreply@mydomain.com';                 // SMTP username
$mail->Password = '********';                           // SMTP password
$mail->SMTPSecure = 'tls';                            
$mail->From = 'noreply@mydomain.com';
$mail->FromName = 'Admin';
$mail->addAddress('sdfsdf@hotmail.com');               // Name is optional

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'swag';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>

推荐答案

您应该查看您的虚拟主机是否允许您在其系统上使用sendmail. sendmail不需要STMP.这样做是有可能的,因为它是大多数Linux/Unix设置的标准部分.并且由于您说您的虚拟主机是000WebHost,因此似乎它们确实支持使用 sendmail :

You should see if your web host allows you to use sendmail on their system. STMP is not needed with sendmail. Chances are they do since it is a standard part of most any Linux/Unix setup. And since you say that your web host is 000WebHost it seems like they do support the use of sendmail:

您可以使用PHP的mail()函数为访问者发送邮件...

You can use PHP's mail() function to send mail for your visitors…

但是他们似乎也在其高级软件包中提供SMTP.

But they also seem to offer SMTP in their premium package.

过去,我建议您不要担心SMTP&现在坚持使用sendmail.查看PHPMailer的文档,似乎该库可以使用sendmail:

Past that, I would recommend not worrying about SMTP & sticking to sendmail for now. Looking at the documentation for PHPMailer it seems that the library can use sendmail:

PHP mail()函数通常通过本地邮件服务器发送, 通常在Linux,BSD和OS X上以sendmail二进制文件开头 平台,但是Windows通常不包含本地邮件 服务器; PHPMailer的集成SMTP实施允许电子邮件 在没有本地邮件服务器的Windows平台上发送邮件.

The PHP mail() function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD and OS X platforms, however, Windows usually doesn't include a local mail server; PHPMailer's integrated SMTP implementation allows email sending on Windows platforms without a local mail server.

然后在PHPMailer存储库中的examples/文件夹中查找显示有一个sendmail示例:

Then looking in the examples/ folder in the PHPMailer repository shows there is a sendmail example:

require '../PHPMailerAutoload.php';

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

// Set PHPMailer to use the sendmail transport
$mail->isSendmail();

//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('whoto@example.com', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';

//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!";
}

这篇关于如何在PHPMailer中指定SMTP服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆