Solved

Implement PHP Mailer Code

Posted on 2014-04-11
5
710 Views
Last Modified: 2014-04-14
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
Comment
Question by:chantalcookware
  • 2
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
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
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
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
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
@skullnobrains: the original code attached a csv file to the mail message.
0
 
LVL 26

Accepted Solution

by:
skullnobrains earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:chantalcookware
Comment Utility
This gave me enough to get it working. Thank you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is about my first experience with programming Arduino.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now