send mail using a php script(i want to make it in a form of function)
i want to be able to send mails using php. i have no experience in this. i got some scripts like http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php. I want to send a html mail which needs to be formed using data from a mysql database. I have email addresses stored in a database and want to design a script to get addresses from it and call mailing function to form accordingly html message and send it Please help me, give me any method to send html mails formed using database data and which can be called in the form of a function so tht i can automate sending
I HAVE ADDED THE CODE I AM US开发者_C百科ING PLEASE TELL ME THE ERROR IN THIS...WEN I RUN THIS SCRIPT AN EMPTY MAIL WITH AN EMPTY ATTACHMNT IS SENT TO MY ID
<?php
//define the receiver of the email
$to = 'mymail@gmail.com';
//define the subject of the email
$subject = 'hi there';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: blah <contact@mydomain.com>\r\nReply-To: contact@mydomain.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
echo $random_hash;
echo '<br>Content-Type: text/plain; charset="iso-8859-1"
<br>Content-Transfer-Encoding: 7bit';
echo '<br>Hello World!!!
This is simple text email message.';
echo $random_hash;
echo '<br>Content-Type: text/html; charset="iso-8859-1"
<br>Content-Transfer-Encoding: 7bit ';
echo '
<br><span><iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2F89Deals%2F162906580388522&width=200&colorscheme=light&connections=6&stream=false&header=true&height=287" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:287px;" allowTransparency="true"></iframe></span>gfhjfjhfjhjjgkgk<br>
</span>
</td>
</table>
</body> ';
echo $random_hash;
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();;
//echo $headers." ";
echo $message;
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
The script already is PHP, so there is no need to "convert it to PHP".
To understand the code you had problems with, you need to understand the output buffer. ob_start()
tells PHP that everything that will be output by echo
should not really be output but rather be put into a hidden buffer whose content thereafter is retrieved using ob_get_clean()
. That becomes the contents of the mail.
So everything you echo between ob_start()
and ob_get_clean()
will be the contents of your mail.
精彩评论