如何将电子邮件发送到服务器电子邮件?

时间:2018-12-30 01:06:15

标签: php email smtp phpmailer

我正在使用PHPMailer库发送电子邮件,我想向3个服务器电子邮件发送电子邮件。

因此,假设有我的电子邮件:(“ info@example.com”,“ admin@example.com”,“ help@example.com”)。

我想向这3封电子邮件发送相同的电子邮件。

在这种情况下,我应该为这些参数输入什么?

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = '';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Username = '';                 // SMTP username
$mail->Password = '***';                          // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;   

我可以使用其他服务器电子邮件从其他3封电子邮件发送电子邮件吗?

还是我必须使用Gmail等服务?

代码如下:

$recipients = array('help@example.com', 'info@example.com', 'admin@example.com');

$mail = new PHPMailer();                              // Passing `true` enables exceptions

//Server settings
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = '';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Username = '';                 // SMTP username
$mail->Password = '';                          // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

//Recipients
$mail->setFrom('', '');
$mail->addReplyTo('', '');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

//Content
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Test';
$mail->Body    = 'Testing';
$mail->AltBody = 'Testing';

foreach ($recipients as $recipient) {

    $mail->addAddress($recipient);

    if (!$mail->send()) {
        echo "Mailer Error (" . str_replace("@", "&#64;", $recipient) . ') ' . $mail->ErrorInfo . '<br />';
        break; //Abandon sending
    } else {
        echo "Message sent to :"  . ' (' . str_replace("@", "&#64;", $recipient) . ')<br />';
    }

    // Clear all addresses for next loop
    $mail->clearAddresses();
}

2 个答案:

答案 0 :(得分:0)

如果要从自己的服务器发送到自己拥有的其他地址,则无需使用外部邮件服务-除非,您的ISP阻止出站SMTP,但这是另一个问题。在服务器上安装邮件服务器(后缀不错)-如果您使用的是Debian / Ubuntu,则只需要apt-get install postfix并回答它会问您的问题。

之后,您的PHP脚本可以通过本地邮件服务器发送,因此您的代码将减少为:

$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPKeepAlive = true;

您不需要进行任何加密或身份验证,因为您既要从本地主机也要向本地主机发送邮件。将邮件传递到本地邮件服务器后,它将处理转发,您可以通过邮件服务器自己的日志(通常在/var/log/mail.log中进行监视)。顺便说一句,这也是从PHP发送电子邮件的最快,最有效的方法。

答案 1 :(得分:-1)

您将必须与您的网络托管公司联系,以向您提供电子邮件的IMAP / POP3配置。如果您使用(示例)example.com,则它们将为您提供诸如mail.smtp.com之类的信息作为主机,您的电子邮件地址的用户名和密码,端口(可以是587或其他)以及ssl的设置。