【问题标题】:I need to create a mailto link from PHP code我需要从 PHP 代码创建一个 mailto 链接
【发布时间】:2015-11-21 04:28:46
【问题描述】:

好的,所以目前我的 html 表单收集数据并将其发布到 php 表单,然后创建并发送电子邮件(以下代码),但是现在我需要该表单来创建 mailto 链接,以便我可以将其发送到与我的 iphone6 不同的邮件帐户,有什么帮助吗???!!! :)

代码是:

<?php
ini_set( "SMTP", "localhost" );
ini_set( "smtp_port", "25" );

if ( isset( $_POST['caseref'] ) ) {

    $email_from = "dave@dave.com";
    $to = "dave@dave.com";
    $email_subject = "Arrival:  " . $_POST['caseref'];

    function died( $error ) {
        // error code here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error . "<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if ( !isset( $_POST['casreref'] ) ) {
        #died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $name = $_POST['name']; // required
    $caseref = $_POST['caseref']; // required
    $notes = $_POST['notes']; // required

    $error_message = "";

    $email_message .= "Incident Number: " . $caseref . "\n";
    $email_message .= "Arrival Date:" . date( " d/m/Y " );
    $email_message .= "\n";
    $email_message .= "Arrival Time:" . date( "  H:i  ", time() );
    $email_message .= "\n";
    $email_message .= "Engineer:  " . $name . " \n";
    $email_message .= "Engineers Notes:  " . $notes;
    $email_message .= "\n";
    $email_message .= "\n";
    $email_message .= "\n";


    $headers = 'From: ' . $email_from;
    mail( $to, $email_subject, $email_message, $headers );
    ?>
    <!-- include your own success html here -->
    <html>
        <title>Thank you!</title>

        Mailto link needs to go here

<?php
}
?>

谁能帮我解决这个问题?

电子邮件需要如下所示:

邮箱:davey@dave.com

主题:到达

正文:事故编号:XXXXXX

到货日期:20/11/2015

到达时间:14:41

工程师:戴夫

工程师笔记:

【问题讨论】:

  • 这是 HTML 101 的东西。
  • 这只是 html,而不是火箭科学。 echo '&lt;a href="mailto:...."&gt;blahblah&lt;/a&gt;';
  • 你能给我看看吗?我无法将变量(html 表单的输出)传递到 HTML 字符串中......对不起,我没有编写这段代码,我不是程序员!
  • echo '&lt;a href="mailto:...."&gt;*meh*&lt;/a&gt;';
  • 我认识 mailto 的人,但是如何将页面中的数据解析到 mailto 链接中?

标签: php html mailto


【解决方案1】:

正如我们一直在讨论的,您需要在mailto 链接中使用NVP。为了尊重换行符,您需要使用 urlencode()rawurlencode(),具体取决于电子邮件客户端以及它们尊重编码的方式。

$to = "dave@dave.com";
$caseref = "123";
$name = "Bob";
$notes = "Some notes";
$email_subject = "Arrival some data";
$email_message = "Incident Number: " . $caseref . "\n";
$email_message .= "Arrival Date:" . date("d/m/Y");
$email_message .= "\n";
$email_message .= "Arrival Time:" . date("H:i");
$email_message .= "\n";
$email_message .= "Engineer:  " . $name . " \n";
$email_message .= "Engineers Notes:  " . $notes;
$email_message .= "\n";
$email_message .= "\n";
$email_message .= "\n";

// Echo the mail to link
echo '<a href="mailto:'.$to.'?subject='.urlencode($email_subject).'&body='.urlencode($email_message).'">Mail to Link</a>';

// Echo the mail to link using the different encoding
echo '<a href="mailto:'.$to.'?subject='.rawurlencode($email_subject).'&body='.rawurlencode($email_message).'">Mail to Link</a>';

另请注意:我已从您的 date() 函数中删除了 time(),因为默认使用 time()...没有必要指定它。

【讨论】:

  • 事件+编号:+test Arrival+Date:+20/11/2015+ Arrival+Time:++17:00++ Engineer:++Dave+Hawkes Engineers+Notes:++On +site 快到了!我们如何删除 ++++
  • + 用于空格所在的位置,并且来自必需的函数urlencode()。当您单击该链接并在您的邮件应用程序中打开它时,它将删除编码。
  • 当我点击链接时它不会删除 +
  • 那很可能是邮件客户端。尝试用rawurlencode() 替换urlencode() 函数。
  • 抱歉回复晚了,我正在检查,很快就会通知你:)
【解决方案2】:

你可以试试这个吗?

   <!-- Mailto link needs to go here-->
<?php echo'<a href="mailto:'.$to.'" subject="'.$email_subject.'" body="'.$email_message.'>Send mail</a>';?>

【讨论】:

  • 这不是mailto 的工作方式......它是NVP 就像mailto:nowhere@mozilla.org?subject=Newsletter 取自docs
  • 我的错!请改用echo '&lt;a href="mailto:'.$to.'?subject='.$email_subject.'&amp;body='.$email_message.'"&gt;Send umail&lt;/a&gt;';
  • 不过,您的思路是对的,还有什么我们遗漏的吗?以上所有让我明白的是:TO:'.$to。'主题:'.$email_subject.'正文:'.$email_message.'
  • 你需要&lt;?php ?&gt;,就像&lt;?php echo '&lt;a href="mailto:'.$to.'?subject='.$email_subject.'&amp;body='.$email_message.'"&gt;Send umail&lt;/a&gt;'; ?&gt;一样
  • 这行得通,但它不会在正文中返回,所以现在看起来像这样: 事件编号:djh 到达日期:20/11/2015 到达时间:16:16 工程师:Dave Hawkes 工程师注释: 在现场但需要看起来像: 事故编号: djh 到达日期: 20/11/2015 到达时间: 16:16 工程师: Dave Hawkes 工程师备注: 现场
猜你喜欢
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2012-05-16
  • 2016-09-26
  • 1970-01-01
相关资源
最近更新 更多