regarding email with attachment in php - php

Can any one hel me?? I am trying to send an email with attachment it sends the email but the email is send in encoded format I think the problem is with headers can any one give me the working code.. there are lots of ready made code available on net i tried all of them but none of them is working

You can use PHPMailer http://code.google.com/a/apache-extras.org/p/phpmailer/
Dowload Link : http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/detail?name=PHPMailer_5.2.1.zip&can=2&q=
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Thanks
:)

Related

PHPMailer - Print all files uploaded

I'm using the PHPMailer to send e-mails like a notice that new files has been uploaded. The PHPMailer looks like this to create the HTML e-mails:
$mail->Body = 'Hi!<br/>'
. '<br/>These files has been uploaded:<br/><br/>'
. '<b> ' . for($i = 0; $i < count($data['data']['metas']); $i++){$file = $data['data']['metas'][$i]["name"]; echo $file <br />;} . '<br/>'
This doesn't work at all, and I somehow understand why. However, I can't figure out to send in plain text in the e-mail what files that has been uploaded.
This code alone in PHP works perfectly, but not in the HTML e-mail sent from PHPMailer.
for($i = 0; $i < count($data['data']['metas']); $i++){$file = $data['data']['metas'][$i]["name"]; echo $file <br />;}
Any ideas how to solve this, so I can send a e-mail with all files uploaded?
The problem is in the way you are forming your $mail->Body string. Try the following;
$mail->Body = 'Hi!<br/><br/>These files has been uploaded:<br/><br/>';
for($i = 0; $i < count($data['data']['metas']); $i++) {
$mail->Body .= $data['data']['metas'][$i]["name"] . '</br>';
}
How send files with email:
require_once '../class.phpmailer.php';
error_reporting(E_ERROR & ~E_NOTICE | E_STRICT);
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
// Add files
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}

Remove mailed by in PHP mail using PHPMailer

I am using PHPMailer to send mail in my Php program. Email is working fine but it showing mailed by address in from area.How can i hide these mailed by in PHPMailer.and also via details from Email from area.
When i use php mail() function like below its removing mailed by details.But how can i do it in PHPMailer
mail('info#example.com', 'Subj', "Message", $headers, '-freturn#yourdomain.com')
Here is php mailer code
<?php
require_once 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
$body = "Heloooo";
try {
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('to#example.com', 'John Doe');
$mail->SetFrom('info#example.ae', 'Info');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
You can just remove the below two lines.
$mail->SetFrom('info#example.ae', 'Info');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
Then the mail details will show from as "Root User root#localhost".
But it will show from which server you are sending it.
Even you use the SMTP with phpmailer, you can do the same.

PHP mail from address

I am using phpmailer to send email from my online website.but when i sending it showing default server address in from address
<?php
require_once 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
$mail->AddAddress('to#domain.com', 'Jo');
$mail->SetFrom('info#mydomain.com', 'Info');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML('Helooo');
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
now it showing like
i want to replace red marked address to info#mydomain.com....how can i do that...this red marked address is default server address i think. i tried to set address using phpmailer from tag, but no change
try this to change from address
<?php
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: info#domain.com";
mail($to,$subject,$txt,$headers);
?>
It's very common for hosted email services (e.g. gmail) to not let you change the From address, or restrict you to pre-set aliases, not allowing arbitrary addresses.
It also looks like you've based your code on an obsolete example, and you're probably using an old version of PHPMailer, so get the latest version.

How to Add dynamic image on body of Email using phpmail

I am creating a email service which send different image to different person using phpmail.I can send mail and attachment as i require but when it comes to dynamically adding embedded image in body of mail.
I wont be able to achieve success.
<?php
require_once('class.phpmailer.php');
// multiple recipients
$to = $arr['Contact.Email']; // note the comma
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try{
$mail->SetFrom($arr['Contact.Email'], 'First Last');
$mail->AddAddress($arr['Contact.Email'], 'John Doe');
$mail->Subject = 'PHPMailer Test';
$mail->AddEmbeddedImage($arr['ContactId'].'.png', 'my-attach');
$mail->Body = 'Your <b>HTML</b> with an embedded Image: <img src=cid:my-attach\'> Here is an image!';
$mail->AddAttachment($arr['ContactId'].".png"); // this is a regular attachment (Not inline)
$mail->Send();
echo "Message Sent OK<p></p>\n";
}
catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
I can embed one image but when i try to send different image for different user i get an error.I searched everywhere but i did not get any satisfactorily answer.
any help will be appreciated.
You have an error at:
$mail->Body = 'Your <b>HTML</b> with an embedded Image: <img src=cid:my-attach\'> Here is an image!';
You need to skip the \' after attach.
You also need to set the message to html format by adding:
$mail->IsHTML(true);
Read more about embedding images in the docs here: http://phpmailer.worxware.com/?pg=tutorial

PHPMailer Not Sending Emails - Am I missing something simple?

I am using PHPMailer to send HTML emails. I first set the HTML body of the email using HTML and PHP variables after first calling ob_start.
Here's my code:
<?php
ob_start();
?>
..... a bunch of VALID HTML
<?
$body = ob_get_contents();
require_once('class.phpmailer.php');
// Send to Me
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport
try {
$mail->AddReplyTo('info#example.com', 'Company Name');
$mail->AddAddress('me#example.com', 'My Name');
$mail->SetFrom('info#example.com', 'Company Name');
$mail->Subject = "Contact Form Confirmation";
$mail->AltBody = "This is a contact form submitted through the main website.";
$mail->WordWrap = 50; // set word wrap
$mail->Body = $body;
$mail->IsHTML(true); // send as HTML
$mail->Send(); // Try to send email
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
// end try
For some reason, this line:
$mail->Body = $body;
causes the email not to send. If I replace it with:
$mail->Body = "This is a test email.";
the email sends perfectly.
If my HTML contained within $body is just a valid HTML page with CSS in the head, and no Javascript or anything, why won't the email send?
Is there another way to do this? Please help!!
Turns out, for some reason, the emails were not sending if they contained a fax number. Totally strange. I tested my entire markup, and after ONLY removing a 10 digit phone number, the emails finally went through. I'm not a newbie - literally that's the only text I removed and the emails sent fine.
HAS ANYONE SEEN THIS BEFORE??

Categories