Syntax for sending a email header attachment [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send emails with PHP using the PEAR Mail package with attachment
I want to send an email with PHP using a generated attachment (not a file on the server), I was curious about the syntax.
<?php
#require_once "Mail.php";
$from = "foo#me.com>";
$to = "blah#me.com>";
$subject = "Hi!";
$body = "Attached is the file\n\n";
$attachment = header( 'Content-Type: text/csv' );
$attachment = $attachment.header( 'Content-Disposition: attachment;filename=example.csv');
$attachment = 'transactionid,valid,fname,lname,email,studentid,status
"654","1","First Name","Last Name","Email#me.com","1000000"';
$body = $body.$attachment;
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = #Mail::factory('smtp', array('host' => $host,
'port' => $port,
'auth' => true,
'username' => $mail_username,
'password' => $mail_password));
$mail = #$smtp->send($to, $headers, $body);
if (#PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
EDIT: Please note, i know there are many examples of attaching a file from the server in an email, but I am asking about attaching a generated csv file (that is NOT on the server) to an email.

Start with How to send emails with PHP using the PEAR Mail package with attachment
Then replace the following:
if ($mime->addAttachment($file,'application/pdf')){
with
$file = 'transactionid,valid,fname,lname,email,studentid,status
"654","1","First Name","Last Name","Email#me.com","1000000"';
if ($mime->addAttachment($file,'text/csv', 'example.csv', false)){
see documentation at http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php

Related

Security issues when sending email through php [duplicate]

This question already has an answer here:
Security issues with Mail.php [duplicate]
(1 answer)
Closed 2 years ago.
I created a file to test sending emails by php using the Mail.php library, however when I require-once "Mail.php", the server returns the following error: ModSecurity: Access allowed (phase 1 ).
The php file only has this single line of code. How do I solve this?
The code I want to make is this:
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.gdom.net";
$username = "lfc hosted email";
$password = "email password";
$headers = array ('From' => $from,
'To' => $to,
'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>");
}

how to send attachment with pear smtp

I want to send a file with email using pear smtp. Email is successfully going to the email. Also an attachment is going with email but with noname and no content in it. When i download the attachment it has only one line "This is a multi-part message in MIME format...".
I didn't understand whats wrong with it. I have attached the code with this question that i am using to send the attachment with email. Please guys review once and let me know what's wrong with this code.
Thanks in advance.
require_once "Mail.php";
include('Mail/mime.php');
$from = "Dispatcher <server#mymailserver.com>";
$host = "smtp.mymailserver.com";
$username = "mailuser";
$password = "password";
$to = "email#example.com";
$subject = "Subject of the email";
$hdrs = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
);
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './test.txt';
$crlf = "rn";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
return 1;
}
$crlf means "carriage return, line feed" and should be that.
Use "\n" to make it work with e-mails.

Gmail and mac settings to send mail from a php script? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I want to send mail from my php script, but unable to find the configuration settings for mac and gmail.
My php script is:
$to ="sender email id";
$subject = "Confirmation";
$header = "";
$message = "";
$sentmail = mail($to,$subject,$message,$header);
if($sentmail)
{
echo "Your Confirmation link Has Been Sent To Your Email Address.<br>";
}
else
{
echo "Cannot send Confirmation link to your e-mail address<br>";
}
I have also try another code on my ubuntu machine using Mail.php, but it shows some authentification error. Here's the code:
include_once "Mail-1.2.0/Mail-1.2.0/Mail.php";
$from = '<sender#gmail.com>';
$to = '<receiver#gmail.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'sender#gmail.com',
'password' => 'xxxxx'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
Provide me some solution so that I can successfully mail from my php script.
Try changing your header on your first script to this:
$header = 'From: put from email address here' . "\r\n" .
'Reply-To: put from email address here' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

PHP Html Email using Pear HTML Table and Pear Email

I am facing quite strange issue, I've php script which sends out an email to the list of users containing HTML table(with the data) and CSV attachment of the same data. The problem is when that script to send out more then 10 emails, in some of the emails we get
--=_f3be233a9b22e88524e1539166c49be0 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=UTF-8
in email body and HTML table lost the formatting as well, however CSV data is absolutely fine in that email.
Basically the purpose of the script is to pick the data according to user subscribe information(database-table) and send it to the subscribe user early morning. Every night the database tables are updated with the new data, which is picked up by that PHP script and send in an email. Further that PHP script trigger by Windows Schedule task.
Below is the code of the application.
if($ix>0)
{
while(odbc_fetch_row($subRes2))
{
$s_email=odbc_result($subRes2, 1);
//echo $s_email;
$from = "Sender <alerts#xxx.com>";
$to = "$s_email";
$subject = "Information_DB:" . $arrayE . " Restored on: " . "-";
$host = "xxxinternal.xxxx.com";
$port = "25";
//disabled the username and password because currently sending unauthenticated email.
// $username = ""; //<> give errors
//$password = "";
//add the email headers 29082013 1008 - SMS
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => false));
//'username' => $username,
//'password' => $password));
$subject = "Results from query";
$crlf = "\n";
$mime_params = array(
'text_encoding' => '7bit',
'text_charset' => 'UTF-8',
'html_charset' => 'UTF-8',
'head_charset' => 'UTF-8'
);
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
//$mime->setTXTBody($body);
$mime->setHTMLBody($body);
$file='./wamp/www/' . $csv_filename;
$mime->addAttachment($csv_filename,'application/octet-stream');
$body = $mime->get($mime_params);
$headers = $mime->headers($headers);
// Sending the email
// $mail =& Mail::factory('mail');
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
// echo "<script>window.close()</script>";
}
else {
echo("<p>Message successfully sent!</p>");
}
}}}
else
{
$from = "Sender <alert#xxx.com>";
$to = "$s_email";
$subject = "Information_DB:" . $arrayE . " Restored on: " . "-";
//$host = "ssl://smtp.gmail.com";
$host = "xxxinternal.xxxx.com";
$port = "25";
//disabled the username and password because currently sending unauthenticated email.
// $username = ""; //<> give errors
//$password = "";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8');
?>
Unfortunately when we tested I could't seen this issue. But when its went UAT lots of user get this issue and it seems to be consistent with the database. For e.g. if user A is subscribe with Spain and Russian data, then consistently they experience the issue with Russian Data email (contains the mentioned headers in the email) however Spain data email is fine. In the live its probably sending around 100's of email daily.
I will appreciate if any of you please point me out what is going wrong as I am setting up the UTF-8 character set in an email mime setup but still getting this issue.
Regards
Shoaib
Most probably you are sending emails too fast that may occur in the result of spam/unwantd email use sleep(4) to create the 4 sec delay in next `mail.
The problem was with mime type, which require to code again. Basically the message I was sending was not encoded with UTF-8 which I did. This time I user Mail_mimePart instead of Mail_mime and it works treat.
That example was very very helpful
---ahttp://tiger-fish.com/comment/reply/133---
and some of the parameter explanation for Mail_mimePart can be found on below link
ahttp://pear.php.net/manual/en/package.mail.mail-mimepart.mail-mimepart.php---
How to attach the attachment using Mail_mimePart.
ahttp://pear.php.net/manual/en/package.mail.mail-mimepart.addsubpart.php---
Hope it helps.

PHP: How to send email with attachment using smtp settings?

I am sending emails successfully using following code. But now I want to attach a text file (example: test.txt) with email. Any Idea?
require_once "Mail.php";
$from = "Usman <from#example.com>";
$to = "Naveed <to#example.com>";
$subject = "subject";
$body = "";
$host = "smtp.gmail.com";
$username = "username";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'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>" );
}
Found this code as one of the first hits of the google://pear mail attachment search.
include('Mail.php');
include('Mail/mime.php');
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './files/example.zip';
$hdrs = array(
'From' => 'someone#domain.pl',
'To' => 'someone#domain.pl',
'Subject' => 'Test mime message'
);
$mime = new Mail_mime();
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file,'application/octet-stream');
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail', $params);
$mail->send('mail#domain.pl', $hdrs, $body);
If you additionally make use of the PHP PEAR Mail_Mime module it provides the appropriate handling and encoding to incorporate attachments as part of your email.
here is the code you're looking for:
<?php
require_once "Mail.php"; // PEAR Mail package
require_once ('Mail/mime.php'); // PEAR Mail_Mime packge
$from = "Robert Davis <robertdavis#pobox.com>";
$to = "Sam Hill <sam.hill#aol.com>";
$subject = 'Test mime message with an attachment';
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = 'Text version of email';// text and html versions of email.
$html = '<html><body>HTML version of email. <strong>This should be bold</strong></body> </html>';
$file = './sample.txt'; // attachment
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
//do not ever try to call these lines in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$host = "sasl.smtp.pobox.com";
$username = "robertdavis#pobox.com";
$password = "Kdu48Adi3";
$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>");
}
?>
Sending email with PHP always feels a little bit like a struggle. If you are able to use them I would recommend one of these two mail libraries for PHP:
PHP Mailer
Swiftmailer
You seem to be using the PEAR Mail package.
Take a look at the Mail_Mine object which does what you're trying to do and has an easy way of adding attachments (simply call addAttachments).
http://pear.php.net/manual/en/package.mail.mail-mime.php

Categories