Solved

Implement PHP Mailer Code

Posted on 2014-04-11
5
740 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
ID: 39995141
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
ID: 39995924
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
ID: 39995936
@skullnobrains: the original code attached a csv file to the mail message.
0
 
LVL 26

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 39995972
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
ID: 40000291
This gave me enough to get it working. Thank you!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

810 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