通过任何帐户发送电子邮件

时间:2018-08-29 22:06:46

标签: php email smtp

我需要了解一些与“主机”相关的信息。仅当发送ID为Gmail ID时才有效。我希望这适用于任何电子邮件地址(不仅适用于gmail)。请指导我如何更改此代码。

require 'phpmailer/PHPMailerAutoload.php';

$from = $_POST['email'];                    
$to_id = "myaccount@yahoo.com";
$password = "mypassword";
$message = $_POST['message'];
$subject = $_POST['subject'];

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $from;
$mail->Password = $password;
$mail->setFrom('from@example.com', $from);
$mail->addReplyTo('replyto@example.com', $from);
$mail->addAddress($to_id);
$mail->Subject = $subject;

$mail->msgHTML($message);

if (!$mail->send()) {
   $error = "Mailer Error: " . $mail->ErrorInfo;
    ?><script>alert('<?php echo $error ?>');</script><?php
} 
else {
   echo '<script>alert("Message sent!");</script>';
}

0 个答案:

没有答案