繁体   English   中英

将电子邮件发送到带有附件的多封电子邮件

[英]Sending email to multiple email with an attachment

我试图在用户单击注册页面上的“提交”按钮后向管理员发送电子邮件。 下面给出的代码工作正常,我在管理员电子邮件地址上获得了所有详细信息。 我被困在两个地方:

  1. 我也想发送抄送至superadmin@mywebsite.com。 我尝试在$receiver = "admin@mywebsite.com,superadmin@mywebsite.com";类的变量中添加逗号$receiver = "admin@mywebsite.com,superadmin@mywebsite.com"; 但它不起作用。 邮件未发送。 需要知道如何将此电子邮件发送给两个不同的人。
  2. $panph1变量返回服务器上的文件名(在此示例中为1467896354.jpg)。 它在电子邮件正文中显示文件名以及所有其他数据。 该文件存储在/pancard/1467896354.jpg-我应该进行哪些更改以获取此电子邮件附带的实际文件。

谢谢

$subject = "New registration from $name ";  
$receiver = "admin@mywebsite.com";
$prod_id1=$rowcc['prod_id'];
$mobile1=$rowcc['mobile'];
$reg=$rowcc['reg_date'];
$ip1=$rowcc['ip'];
$panno1=$rowcc['pan_no'];
$panph1=$rowcc['pan_photo'];
$message = "    
<html>
<body>
<table>
  <tr>
    <td>Product ID</td>
    <td>:</td>
    <td>$prod_id1</td>
  </tr>
  <tr>
    <td>Name</td>
    <td>:</td>
    <td>$name</td>
  </tr>
  <tr>
    <td>Email Address</td>
    <td>:</td>
    <td>$to</td>
  </tr>
  <tr>
    <td>Mobile</td>
    <td>:</td>
    <td>$mobile1</td>
  </tr>
  <tr>
    <td>Registration Date</td>
    <td>:</td>
    <td>$reg</td>
  </tr>
  <tr>
    <td>IP Address</td>
    <td>:</td>
    <td>$ip1</td>
  </tr>
  <tr>
    <td>Pan Card Number</td>
    <td>:</td>
    <td>$panno1</td>
  </tr>
  <tr>
    <td>Pan Card Photo</td>
    <td>:</td>
    <td>$panph1</td>
  </tr>
</table>
</body>
</html>
";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:'.$from."\r\n"
.'Reply-To: $receiver'."\r\n";

mail($receiver,$subject, $message, $headers); 

为什么不使用PhpMailer

许多PHP开发人员在其代码中使用电子邮件。 支持此功能的唯一PHP函数是mail()函数。 但是,它对使用流行功能(例如基于HTML的电子邮件和附件)没有任何帮助。

这是使用PhpMailer的示例:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtpserver.com';  // Specify main SMTP server. If you dont have one us GMAL or mandrill
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@youremail.com';                 // SMTP username
$mail->Password = 'pass';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@youremail.com', 'Mailer');
$mail->addAddress('joe@youremail.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@youremail.com');               // Name is optional
$mail->addReplyTo('info@youremail.com', 'Information');
$mail->addCC('cc@youremail.com');
$mail->addBCC('bcc@youremail.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM