0

I am trying to send an email to admin once the user clicks submit button on the registration page. The code given below works fine and I get all the details on the admin email address. I am stuck at two places:

  1. I want to send a CC to superadmin@mywebsite.com too. I tried putting a comma in the variable like $receiver = "admin@mywebsite.com,superadmin@mywebsite.com"; but it does not work. The mail does not get sent. Need to know how to send this email to two different people.
  2. $panph1 variable returns a filename on the server (1467896354.jpg in this example). It displays the filename in the email body along with all other data. This file is stored at /pancard/1467896354.jpg - What changes I should make to get the actual file attached with this email.

Thanks

$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); 
Sabha
  • 621
  • 10
  • 32
  • Possible duplicate http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – ecorvo Jul 07 '16 at 13:51
  • I saw this page before I posted my question, it talks about PHPMailer script but I have my own classes and the `mail()` function works fine. Its just that I dont know the proper syntax to add a CC (another email) and content type / headers / syntax to add an attachment – Sabha Jul 07 '16 at 14:24
  • Have you tried putting this header to send an email copy? `$headers .= 'Cc: myboss@example.com' . "\r\n";` – Nick Jul 07 '16 at 14:43
  • Thanks for your input. I will try using this for the CC issue. Please let me know if you can suggest header or any syntax for sending attachment from the path i mentioned in my question. Thanks – Sabha Jul 07 '16 at 15:00

1 Answers1

1

Why not use PhpMailer?

Many PHP developers utilize email in their code. The only PHP function that supports this is the mail() function. However, it does not provide any assistance for making use of popular features such as HTML-based emails and attachments.

Here is an example using 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';
}
Nick
  • 1,032
  • 16
  • 27
  • Your solution may be good and even better than what I am using. However, I will then need to change a lot of things in lot of files. I thought there would be a specific syntax or specific headers required to send attachment and to add CC. Please let me know if you can manipulate my code to get a result. Thanks – Sabha Jul 07 '16 at 14:40
  • 1
    I understand, but in the long run is going to be better. The php mail() function has lots of limitations, specially not sending them via SMTP. Most of your emails will arrive to the spam folder or not arrive at all. – Nick Jul 07 '16 at 14:46
  • I understand the concern. I want to finish this asap because sending attachment is the only work left. I will use PhpMailer in the next project. Thanks for your advice. – Sabha Jul 07 '16 at 14:59