【发布时间】:2015-07-12 17:25:10
【问题描述】:
我正在使用以下 php 代码向预定义邮件发送带有附件的电子邮件。
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$body = $_POST['message'];
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "dimal.chandrasiri@gmail.com";
$mail->Password = "****";
$mail->SMTPSecure = 'tls';
$mail->SetFrom('dimal.chandrasiri@gmail.com', 'Your name');
$mail->AddReplyTo("dimal.chandrasiri@gmail.com","Your name");
$mail->Subject = "Abstract submission for" .$_POST['fname'];
$mail->AltBody = $_POST['message'];
if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK){
$mail->AddAttachment($_FILES['file']['tmp_name'],$_FILES['file']['name']);
}
$mail->MsgHTML($body);
$address = 'dimal.chandrasiri@gmail.com';
$mail->AddAddress($address, $name);
if($mail->Send()) {
echo 'done';
} else {
echo 'error';
}
?>
密码和其他 smtp 详细信息正确,但我无法发送任何邮件。我在这里做错了什么?我使用下面的 html 表单来发布数据。
<form id="main-contact-form" class="abstract-form" name="contact-form" method="post" action="/script/submit.php">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input type="text" name='fname' class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" name='lname' class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input type="text" name='email' class="input-block-level" required="required" placeholder="Your email address">
<label>File</label>
<input type="file" name='file' class="input-block-level" required="required" placeholder="Your Abstract File">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required="required" class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
<p> </p>
</form>
我在回显错误信息时收到以下错误。
The following From address failed: dimal.chandrasiri@gmail.com : Called Mail() without being connected
【问题讨论】:
-
这段代码之前有效吗?
-
echo $mail->ErrorInfo -
我从教程中获取代码进行测试!
-
您可以从浏览器使用此电子邮件登录吗?您是否使用其他电子邮件(谷歌或其他帐户)测试了此代码?
标签: php html email email-attachments