?
Solved

Implement PHP Mailer Code

Posted on 2014-04-11
5
Medium Priority
?
808 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 35

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 27

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 35

Expert Comment

by:Dan Craciun
ID: 39995936
@skullnobrains: the original code attached a csv file to the mail message.
0
 
LVL 27

Accepted Solution

by:
skullnobrains earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

719 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