【发布时间】:2014-01-27 11:49:03
【问题描述】:
我第一次尝试使用 PHPmailer,即使设置简单,我也无法通过电子邮件。我正在使用 phpmailer 使用的简单的默认 php mail() 函数,我之前在其他表单上使用了带有参数的 mail(),并且这些电子邮件通过了,但是在这个特定的站点上,我也希望发送附件。到目前为止,这是我的代码,请注意附件、名称、电子邮件或复选框尚未被拉出并添加到 php 代码中,我只是让该死的工作并发送 textarea 中的任何内容。请帮忙!!!
--HTML--------------------------------------------------------
<form role="form" action="_/includes/sendmail.php" enctype="multipart/form-data" method="POST" autocomplete="on">
<div class="form-group">
<label class="emph1">First and Last Name:</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="First and Last Name">
</div>
<div class="form-group">
<label class="emph1">Email Address:</label>
<input type="email" class="form-control" id="exampleInputPassword1" placeholder="Email">
</div>
<div class="form-group">
<label class="emph1">File/Form Attachments:</label>
<?php
//Maximum file size (in bytes) must be declared before the file input field and can't be large than the setting for
// upload_max_filesize in php.ini.
// PHP will stop and compain once file is exceeded
// 1 mb is actually 1,048,576 bytes.
?>
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
<input type="file" id="exampleInputFile" name="file_upload">
<p class="help-block">Please use .jpg, .pdf, or Word based files.</p>
<p> <?php echo $message;?>
</div>
<div class="form-group">
<label class="emph1">Reason for contacting Derek Davis, PLLC:</label>
<div class="checkbox">
<label>Estate Planning<input type="checkbox"></label>
</div>
<div class="checkbox">
<label>Family Law<input type="checkbox"></label>
</div>
<div class="checkbox">
<label>Criminal Defense<input type="checkbox"></label>
</div>
<div class="checkbox">
<label>Collections<input type="checkbox"></label>
</div>
<div class="checkbox">
<label>Landlord-Tenant<input type="checkbox"></label>
</div>
<div class="checkbox">
<label>Other<input id="checkbox-other" type="checkbox"></label>
</div>
<!-- jQuery input feature (displays when "other" checkbox is checked) --->
<div class="form-group" id="input-other" style="display:none;">
<label class="emph1" for="">If 'other' please specify:</label>
<input type="text" class="form-control" id="otherProject" name="otherProject" placeholder="Enter short description here." value="" />
</div>
</div>
<!-- End jQuery input feature -->
<div class="form-group">
<label class="emph1">Please leave us a brief message:</label>
<textarea class="full-width" type="text" name="message" rows="10" placeholder="Please be as specific as possible..." required>
</textarea><br />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
--PHPmailer---------------
<?php
require_once("_/includes/phpmailer/class.smtp.php");
require_once("_/includes/phpmailer/class.phpmailer.php");
if (isset($_POST['message'])){
$body = $_POST['message'];
}
// Set PHPMailer to use the sendmail transport
//Set who the message is to be sent from
$mail->setFrom('myemail@gmail.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('myemail@gmail.com', 'First Last');
//Set the subject line
$mail->addAddress('myotheremail@yahoo.com', 'John Doe');
$mail->Subject = 'PHPMailer sendmail test';
//Replace the plain text body with one created manually
$mail->Body = $body;
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment($temp_file, $temp_file_name);
//send the message, check for errors
$mail-Send();
?>
感谢您的快速回复!我对 send() 进行了更改并启动了课程,但它仍然没有向我的 yahoo.com 电子邮件地址发送废话。现在是代码....
<?php
require_once("_/includes/phpmailer/class.smtp.php");
require_once("_/includes/phpmailer/class.phpmailer.php");
$mail = new PHPMailer;
if (isset($_POST['message'])){
$body = $_POST['message'];
}
// Set PHPMailer to use the sendmail transport
//Set who the message is to be sent from
$mail->setFrom('myemail@gmail.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('myemail@gmail.com', 'First Last');
//Set the subject line
$mail->addAddress('myotheremail@yahoo.com', 'John Doe');
$mail->Subject = 'PHPMailer sendmail test';
//Replace the plain text body with one created manually
$mail->Body = $body;
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment($temp_file, $temp_file_name);
//send the message, check for errors
$mail->send();
?>
更新!!!
好的,现在这里是我的代码。它不仅不会向我的电子邮件发送消息,而且也不会将我重定向到任一页面...有什么建议吗?
<?php ob_start()?>
<?php
require_once("_/includes/phpmailer/class.smtp.php");
require_once("_/includes/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (isset($_POST['message'])){
$body = $_POST['message'];
}
// Set PHPMailer to use the sendmail transport
//Set who the message is to be sent from
$mail->setFrom('myemail@gmail.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('myemail@gmail.com', 'First Last');
//Set the subject line
$mail->addAddress('myotheremail@yahoo.com', 'John Doe');
$mail->Subject = 'PHPMailer sendmail test';
//Replace the plain text body with one created manually
$mail->Body = $body;
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment($temp_file, $temp_file_name);
//send the message, check for errors
if ($mail->send()) {
redirect_to("../../contact.php");
} else {
redirect_to("../../index.html");
}
?>
<?php ob_end_flush(); ?>
【问题讨论】:
-
您必须首先声明 $mail 不能像它存在那样使用它并且是 PHPMailer 类型。首先使用
$mail = new PHPMailer();创建$mail。您还包括 smtp 类,因此您可能必须在使用前对其进行配置。非常基本的例子phpmailer.worxware.com/index.php?pg=examplebsmtp -
啊啊啊,我忘了开括号和右括号,我试试看。