使用php脚本发送邮件(我想以函数的形式)
我希望能够使用 php 发送邮件。我没有这方面的经验。我有一些脚本,例如 http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php。我想发送一封 html 邮件,该邮件需要使用 mysql 数据库中的数据形成。我的电子邮件地址存储在数据库中,想要设计一个脚本来从中获取地址并调用邮件函数以形成相应的 html 消息并发送 请帮助我,给我任何发送使用数据库数据形成的 html 邮件的方法,并且可以以函数的形式调用该邮件,以便我可以自动发送
我已经添加了我正在使用的代码,请告诉我其中的错误.. .当我运行此脚本时,一封带有空附件的空邮件被发送到我的 ID
<?php
//define the receiver of the email
$to = '[email protected]';
//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 <[email protected]>\r\nReply-To: [email protected]";
//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";
?>
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 USING 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 = '[email protected]';
//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 <[email protected]>\r\nReply-To: [email protected]";
//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";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该脚本已经是PHP,因此不需要“将其转换为PHP”。
要理解您遇到问题的代码,您需要了解输出缓冲区。
ob_start()
告诉 PHP,echo
输出的所有内容不应真正输出,而应放入隐藏缓冲区中,此后使用 ob_get_clean 检索其内容()。这将成为邮件的内容。因此,您在
ob_start()
和ob_get_clean()
之间回显的所有内容都将成为您的邮件内容。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 byecho
should not really be output but rather be put into a hidden buffer whose content thereafter is retrieved usingob_get_clean()
. That becomes the contents of the mail.So everything you echo between
ob_start()
andob_get_clean()
will be the contents of your mail.