• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 853
  • Last Modified:

Implement PHP Mailer Code

Hello.
I have an existing PHP script which emails a file to select recipients using the PHP mailer on the host server. However, I need to change this to use an authenticated SMTP server. I have the code from the hosting company to add the server, but do not know how to merge it with my existing PHP script. I am attaching the new smtp mail script and the part of my script dealing with emailing the file.
Thank you for any help.
smtpmail.php
orders-report-cron-test.php
0
chantalcookware
Asked:
chantalcookware
  • 2
  • 2
1 Solution
 
Dan CraciunIT ConsultantCommented:
It's not PHPMailer.
It's PEAR::Mail.

You're going to need both Mail and PEAR::Mail_Mime to send attachments.

Here's a tutorial for sending emails with attachments using Mail_Mime: http://www.tuxradar.com/practicalphp/15/5/3

HTH,
Dan
0
 
skullnobrainsCommented:
try this func to replace mail
you'll need to remove all headers up to the boundary declaration (included)
... and send the rest of your 'headers' as the body because they're actually the body

<?php
require_once "Mail.php";

function authmail($to, $subject, $body){


$from = "Web Master <webmaster@example.com>";
$body = "This is a test email message";
 
$host = "mail.emailsrvr.com";
$username = "webmaster@example.com";
$password = "yourPassword";
 
$headers = array ('From' => $from,
 'MIME-Version' => '1.0',
'Content-Type' => "multipart/mixed; boundary=\"".$uid."\"",
  'Subject' => $subject);

$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
 
$mail = $smtp->send($to, $headers, $body);
 
if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}
}

Open in new window


note that
- my code is not good coding practice. more like a quick and dirty hack : the function contents should rather be sticked directly in your code where you call mail
- it is tryped directly in the forum so there may be a few typos
- the mail factury extension wants headers as an array : this is foolish since headers may be duplicated in a mail but is not a problem in your case
- there may be unexpected issues if the mailer tries to be smart and add headers of it's own or play with your body contents, or encodes it...
0
 
Dan CraciunIT ConsultantCommented:
@skullnobrains: the original code attached a csv file to the mail message.
0
 
skullnobrainsCommented:
yes. i only edited the code they sent so it can be called from the existing code (see first 3 lines of my post)

the rest of the headers, which are the actual body contain the csv file

the merged result would be something like this. i'm not using the function i created but rather the code inside because $uid would not be passed and it is less messy like this anyway

$header should be understood as $body while $headers are the actual headers

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $subject, $message) {
		$file = $path.$filename;
		$file_size = filesize($file);
		$handle = fopen($file, "r");
		$content = fread($handle, $file_size);
		fclose($handle);
		$content = chunk_split(base64_encode($content));
		$uid = md5(uniqid(time()));
		$name = basename($file);
		$header .= "This is a multi-part message in MIME format.\n";
		$header .= "--".$uid."\n";
		$header .= "Content-type:text/plain; charset=iso-8859-1\n";
		$header .= "Content-Transfer-Encoding: 7bit\n\n";
		$header .= $message."\n\n";
		$header .= "--".$uid."\n";
		$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\n"; // use diff. tyoes here
		$header .= "Content-Transfer-Encoding: base64\n";
		$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
		$header .= $content."\n\n";
		$header .= "--".$uid."--";


$from = "Web Master <webmaster@example.com>";
 
$host = "mail.emailsrvr.com";
$username = "webmaster@example.com";
$password = "yourPassword";
 
$headers = array ('From' => $from,
 'MIME-Version' => '1.0',
'Content-Type' => "multipart/mixed; boundary=\"".$uid."\"",
  'Subject' => $subject);

$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
 
$mail = $smtp->send($to, $headers, $header);
 
if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}


	}

Open in new window

0
 
chantalcookwareAuthor Commented:
This gave me enough to get it working. Thank you!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now