您是PHP的新手我需要使用PHP通过 SMTP 发送电子邮件。实际上我是用普通代码做的,但邮件是垃圾邮件。 (关注mycode)
$to = 'xxx@gmail.com';
$subject = 'Test mail';
$from = 'zzz@domain.com';
$header = "From: $from \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '//some HTML Code...';
if (mail($to, $subject , $message, $headers)) {
echo 'Success';
} else {
echo 'Error';
}
所以我引用一些网站,他们说使用 SMTP(简单邮件传输协议)邮件将登陆收件箱。请帮助我通过 SMTP 发送电子邮件。
提前致谢
答案 0 :(得分:2)
感谢您帮助我这是我的示例代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test 3 mail</title>
</head>
<body>
<?php
if(isset($_POST['send']))
{
require_once'Swift-5.1.0\lib\swift_required.php';
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
$body = "Name:".$name."<br>Email:".$email."<br>Message:".$message."";
//tarnport
$transport = Swift_SmtpTransport::newInstance('d9.privatewebsolution.com', 465, "ssl")
->setUsername('test@dimain.com')
->setPassword('XXXXX');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Uni-Tech')
->setFrom(array('test@dimain.com' => "Name"))
->setTo($email)
->setBody($body, 'text/html');
$result = $mailer->send($message);
}
?>
<form action="test3.php" method="post">
<label>Name</label>
<input type="text" name="name" /><br />
<label>Email</label>
<input type="email" name="email" /><br />
<label>Message</label>
<textarea name="message"></textarea><br />
<input type="submit" name="send" value="Send" />
</form>
</body>
</html>
答案 1 :(得分:0)
PHP没有使用配置良好的SMTP服务器,这就是它立即发送垃圾邮件的原因。您可以尝试使用PhpMailer着名的邮件程序来处理PHP。
答案 2 :(得分:0)
我认为SwiftMailer适合使用SMTP协议在PHP中发送电子邮件
http://swiftmailer.org/docs/introduction.html
请确保在PHP中启用了https://pear.php.net/package/Net_SMTP包。
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('***@gmail.com')
->setPassword('***'); */
$mailer = Swift_Mailer::newInstance($transport);
$subject="<subject of email";
$body="<body goes Here";
$message = Swift_Message::newInstance($subject)
->setFrom(array('from@example.com' => "Name"))
->setCc(array("cc@example.com" => "ccName"))
->setTo($to_email)
->setBody($body, 'text/html')
// Add alternative parts with addPart()
->addPart($body, 'text/plain');
$result = $mailer->send($message);